Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TestMultithreading.cs « UnitTests « Test - github.com/mono/mono-addins.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fd3cbcf65f35cd2c31ae78780dbec7bee4eaf698 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195

using System;
using System.IO;
using NUnit.Framework;
using Mono.Addins;
using SimpleApp;
using System.Threading;
using System.Diagnostics;
using System.Linq;

namespace UnitTests
{
	[TestFixture ()]
	public class TestMultiThreading: TestBase
	{
		public override void Setup ()
		{
			base.Setup ();
			GlobalInfoCondition.Value = "res";
		}

		[Test]
		public void ChildConditionAttributeMultithread ()
		{
			int threads = 50;
			using var testData = new TestData(threads);

			GlobalInfoCondition.Value = "";

			testData.StartThreads (RunChildConditionAttributeTest);

			for (int n = 0; n < 20; n++) {
				Console.WriteLine ("Step " + n);
				testData.CheckCounters (0, 10000);

				GlobalInfoCondition.Value = "foo";

				testData.CheckCounters (1, 1000);

				GlobalInfoCondition.Value = "";
			}
		}

		void RunChildConditionAttributeTest (int index, TestData data)
		{
			while (!data.Stopped) {
				var writers = AddinManager.GetExtensionObjects<IWriter> ("/SimpleApp/ConditionedWriters");
				data.Counters [index] = writers.Length;
			}
		}

		[Test]
		public void EnableDisableMultithread ()
		{
			int threads = 50;
			int steps = 20;

			using var testData = new TestData (threads);

			testData.StartThreads ((index, data) => {
				while (!data.Stopped) {
					var writers = AddinManager.GetExtensionObjects<IWriter> ("/SimpleApp/Writers");
					testData.Counters [index] = writers.Length;
				}
			});

			for (int n = 0; n < steps; n++) {
				Console.WriteLine ("Step " + n);

				testData.CheckCounters (4, 10000);

				var ainfo1 = AddinManager.Registry.GetAddin ("SimpleApp.HelloWorldExtension");
				ainfo1.Enabled = false;

				testData.CheckCounters (3, 1000);

				var ainfo2 = AddinManager.Registry.GetAddin ("SimpleApp.FileContentExtension");
				ainfo2.Enabled = false;

				testData.CheckCounters (2, 1000);

				ainfo1.Enabled = true;
				ainfo2.Enabled = true;
			}
		}

		[Test]
		public void EventsMultithread ()
		{
			int threads = 50;
			int steps = 20;

			var testData = new TestData (threads);

			GlobalInfoCondition.Value = "";

			testData.StartThreads (RunEventsMultithread);

			for (int n = 0; n < steps; n++) {
				Console.WriteLine ("Step " + n);
				testData.CheckCounters (0, 10000);

				GlobalInfoCondition.Value = "foo";

				testData.CheckCounters (1, 1000);

				GlobalInfoCondition.Value = "";
			}
		}

		void RunEventsMultithread (int index, TestData data)
		{
			while (!data.Stopped) {
				int count = 0;
				ExtensionNodeEventHandler handler = (s, args) => {
					count++;
				};
				AddinManager.AddExtensionNodeHandler("/SimpleApp/ConditionedWriters", handler);
				data.Counters [index] = count;
				AddinManager.RemoveExtensionNodeHandler ("/SimpleApp/ConditionedWriters", handler);
			}
		}

		[Test]
		public void ChildConditionWithContextMultithread ()
		{
			int threads = 50;
			using var testData = new TestData (threads);

			GlobalInfoCondition.Value = "";

			testData.StartThreads (RunChildConditionWithContextMultithread);

			for (int n = 0; n < 20; n++) {
				Console.WriteLine ("Step " + n);
				testData.CheckCounters (0, 10000);

				GlobalInfoCondition.Value = "foo";

				testData.CheckCounters (1, 1000);

				GlobalInfoCondition.Value = "";
			}
		}

		void RunChildConditionWithContextMultithread (int index, TestData data)
		{
			var ctx = AddinManager.CreateExtensionContext ();
			var writers = ctx.GetExtensionNode ("/SimpleApp/ConditionedWriters");
			while (!data.Stopped) {
				data.Counters [index] = writers.ChildNodes.Count;
			}
		}
		class TestData: IDisposable
		{
			int numThreads;

			public bool Stopped;
			public int [] Counters;

			public TestData (int numThreads)
			{
				this.numThreads = numThreads;
				Counters = new int [numThreads];
			}

			public void CheckCounters (int expectedResult, int timeout)
			{
				var sw = Stopwatch.StartNew ();
				do {
					if (Counters.All (c => c == expectedResult))
						return;
					Thread.Sleep (10);
				} while (sw.ElapsedMilliseconds < timeout);

				Assert.Fail ();
			}

			public void Dispose ()
			{
				Stopped = true;
			}

			public void StartThreads (Action<int, TestData> threadAction)
			{
				for (int n = 0; n < numThreads; n++) {
					Counters [n] = -1;
					var index = n;
					var t = new Thread (() => threadAction(index, this));
					t.Start ();
				}
			}
		}
	}
}