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

github.com/mono/mono-addins.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLluis Sanchez <llsan@microsoft.com>2022-09-14 17:45:28 +0300
committerLluis Sanchez <llsan@microsoft.com>2022-09-14 17:45:28 +0300
commitfcd6b0324ceaad72978fc95d7df1ef70c492d658 (patch)
treeab74762ea98462775683a8f7fa0ffd854980d231 /Test/UnitTests
parentbca58a65e72d3bc418c1dc5cdc5dd63c911b2eea (diff)
Fix threading issue
Split the context transaction class in two classes, one for extension context and one for add-in engine (which is a context by itself). In this was there is no danger of providing a context transaction to an add-in engine method and expect it to work. Added method for stating an engine transaction from a context transaction. Fixes tests.
Diffstat (limited to 'Test/UnitTests')
-rw-r--r--Test/UnitTests/TestMultithreading.cs121
-rw-r--r--Test/UnitTests/UnitTests.csproj1
2 files changed, 57 insertions, 65 deletions
diff --git a/Test/UnitTests/TestMultithreading.cs b/Test/UnitTests/TestMultithreading.cs
index f827788..f4eb63a 100644
--- a/Test/UnitTests/TestMultithreading.cs
+++ b/Test/UnitTests/TestMultithreading.cs
@@ -87,94 +87,85 @@ namespace UnitTests
}
}
- int EnableDisableStress_totalAdd;
- int EnableDisableStress_totalRemove;
- int EnableDisableStress_nodesCount;
- int EnableDisableStress_minCount;
- int EnableDisableStress_maxCount;
-
[Test]
public void EventsMultithread()
{
int threads = 50;
- int addinsLoaded = 0;
- int addinsUnloaded = 0;
+ int totalAdded = 0;
+ int totalRemoved = 0;
+ int nodesCount = 0;
+ int minCount = 0;
+ int maxCount = 0;
var node = AddinManager.GetExtensionNode("/SimpleApp/Writers");
- try
- {
- EnableDisableStress_nodesCount = 0;
+ nodesCount = 0;
- node.ExtensionNodeChanged += Node_ExtensionNodeChanged;
+ node.ExtensionNodeChanged += (s, args) =>
+ {
+ if (args.Change == ExtensionChange.Add)
+ {
+ nodesCount++;
+ totalAdded++;
+ }
+ else
+ {
+ nodesCount--;
+ totalRemoved++;
+ }
- Assert.AreEqual(4, EnableDisableStress_nodesCount);
+ if (nodesCount < minCount)
+ minCount = nodesCount;
+ if (nodesCount > maxCount)
+ maxCount = nodesCount;
+ };
- EnableDisableStress_minCount = 4;
- EnableDisableStress_maxCount = 4;
- EnableDisableStress_totalAdd = 0;
- EnableDisableStress_totalRemove = 0;
+ Assert.AreEqual(4, nodesCount);
- var ainfo1 = AddinManager.Registry.GetAddin("SimpleApp.HelloWorldExtension");
- var ainfo2 = AddinManager.Registry.GetAddin("SimpleApp.FileContentExtension");
+ minCount = 4;
+ maxCount = 4;
+ totalAdded = 0;
+ totalRemoved = 0;
- AddinManager.AddinLoaded += (s,a) => addinsLoaded++;
- AddinManager.AddinUnloaded += (s,a) => addinsUnloaded++;
+ var ainfo1 = AddinManager.Registry.GetAddin("SimpleApp.HelloWorldExtension");
+ var ainfo2 = AddinManager.Registry.GetAddin("SimpleApp.FileContentExtension");
- using var enablers = new TestData(threads);
+ using var enablers = new TestData(threads);
- enablers.StartThreads((index, data) =>
+ enablers.StartThreads((index, data) =>
+ {
+ var random = new Random(10000 + index);
+ int iterations = 100;
+ while (--iterations > 0)
{
- var random = new Random(10000 + index);
- while (!data.Stopped)
+ var action = random.Next(4);
+ switch (action)
{
- 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;
- }
+ case 0: ainfo1.Enabled = false; break;
+ case 1: ainfo1.Enabled = true; break;
+ case 2: ainfo2.Enabled = false; break;
+ case 3: ainfo2.Enabled = true; break;
}
- });
- Thread.Sleep(3000);
- }
- finally
- {
- node.ExtensionNodeChanged -= Node_ExtensionNodeChanged;
- }
-
- // If all events have been sent correctly, the node count should have never gone below 2 and over 4.
+ }
+ data.Counters[index] = 1;
+ });
- Assert.That(EnableDisableStress_minCount, Is.AtLeast(2));
- Assert.That(EnableDisableStress_maxCount, Is.AtMost(4));
+ // Wait for the threads to do the work. 5 seconds should be enough
+ enablers.CheckCounters(1, 5000);
- // Every time one of these add-ins is enabled, a new node is added (likewise when removed), so
- // the total count of nodes added must match the number of times the addins were enabled.
+ // Go back to the initial status
- Assert.AreEqual(EnableDisableStress_totalAdd, addinsLoaded);
- Assert.AreEqual(EnableDisableStress_totalRemove, addinsUnloaded);
- }
+ ainfo1.Enabled = true;
+ ainfo2.Enabled = true;
- private void Node_ExtensionNodeChanged (object sender, ExtensionNodeEventArgs args)
- {
- if (args.Change == ExtensionChange.Add)
- {
- EnableDisableStress_nodesCount++;
- EnableDisableStress_totalAdd++;
- }
- else
- {
- EnableDisableStress_nodesCount--;
- EnableDisableStress_totalRemove++;
- }
+ // If all events have been sent correctly, the node count should have never gone below 2 and over 4.
- if (EnableDisableStress_nodesCount < EnableDisableStress_minCount)
- EnableDisableStress_minCount = EnableDisableStress_nodesCount;
- if (EnableDisableStress_nodesCount > EnableDisableStress_maxCount)
- EnableDisableStress_maxCount = EnableDisableStress_nodesCount;
+ 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]
diff --git a/Test/UnitTests/UnitTests.csproj b/Test/UnitTests/UnitTests.csproj
index aae8ac1..d7f43ca 100644
--- a/Test/UnitTests/UnitTests.csproj
+++ b/Test/UnitTests/UnitTests.csproj
@@ -30,6 +30,7 @@
<Reference Include="System.Core" />
<PackageReference Include="NUnit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
+ <PackageReference Include="Microsoft.Net.Test.Sdk" Version="17.3.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Mono.Addins\Mono.Addins.csproj" />