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: 64a560a68c69d2e45a56f6b6834dc77f93a2e24f (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251

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

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 = "";

			// Threads check the number of nodes in /SimpleApp/ConditionedWriters, which changes
			// from 0 to 1 as the GlobalInfoCondition condition changes

			testData.StartThreads (RunChildConditionAttributeTest);

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

				GlobalInfoCondition.Value = "foo";

				testData.CheckCounters (1, 10000);

				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) {
					LoadAll(AddinManager.GetExtensionNodes<ItemSetNode>("/SimpleApp/ItemTree"));
					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, 10000);

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

				testData.CheckCounters (2, 10000);

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

		void LoadAll(IEnumerable<ExtensionNode> nodes)
		{
			foreach (var n in nodes.OfType<ItemSetNode>())
				LoadAll(n.GetChildNodes());
		}

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

			int totalAdded = 0;
			int totalRemoved = 0;
			int nodesCount = 0;
			int minCount = 0;
			int maxCount = 0;

			var node = AddinManager.GetExtensionNode("/SimpleApp/Writers");

			nodesCount = 0;

			node.ExtensionNodeChanged += (s, args) =>
			{
				if (args.Change == ExtensionChange.Add)
				{
					nodesCount++;
					totalAdded++;
				}
				else
				{
					nodesCount--;
					totalRemoved++;
				}

				if (nodesCount < minCount)
					minCount = nodesCount;
				if (nodesCount > maxCount)
					maxCount = nodesCount;
			};

			Assert.AreEqual(4, nodesCount);

			minCount = 4;
			maxCount = 4;
			totalAdded = 0;
			totalRemoved = 0;

			var ainfo1 = AddinManager.Registry.GetAddin("SimpleApp.HelloWorldExtension");
			var ainfo2 = AddinManager.Registry.GetAddin("SimpleApp.FileContentExtension");

			using var enablers = new TestData(threads);

			enablers.StartThreads((index, data) =>
			{
				var random = new Random(10000 + index);
				int iterations = 100;
				while (--iterations > 0)
				{
					var action = random.Next(4);
					switch (action)
					{
						case 0: ainfo1.Enabled = false; break;
						case 1: ainfo1.Enabled = true; break;
						case 2: ainfo2.Enabled = false; break;
						case 3: ainfo2.Enabled = true; break;
					}
				}
				data.Counters[index] = 1;
			});

			// Wait for the threads to do the work. 5 seconds should be enough
			enablers.CheckCounters(1, 20000);

			// Go back to the initial status

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

			// If all events have been sent correctly, the node count should have never gone below 2 and over 4.

			Assert.That(nodesCount, Is.EqualTo(4));
			Assert.That(totalAdded, Is.AtLeast(100));
			Assert.That(totalAdded, Is.EqualTo(totalRemoved));
			Assert.That(minCount, Is.AtLeast(2));
			Assert.That(maxCount, Is.AtMost(4));
		}

		[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, 10000);

				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.GetChildNodes().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 ("Expected " + expectedResult);
			}

			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 ();
				}
			}
		}
	}
}