From 8190f9103c03afc41e7cc08bc887e1c2383026c2 Mon Sep 17 00:00:00 2001 From: "Frank A. Krueger" Date: Wed, 25 Apr 2018 18:32:25 -0700 Subject: [PATCH] Add stress test of EventHandler Fixes #138 --- Tests/EventTargetStressTests.cs | 69 +++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 Tests/EventTargetStressTests.cs diff --git a/Tests/EventTargetStressTests.cs b/Tests/EventTargetStressTests.cs new file mode 100644 index 0000000..c77eeff --- /dev/null +++ b/Tests/EventTargetStressTests.cs @@ -0,0 +1,69 @@ +using System; + +#if NUNIT +using NUnit.Framework; +using TestClassAttribute = NUnit.Framework.TestFixtureAttribute; +using TestMethodAttribute = NUnit.Framework.TestCaseAttribute; +#else +using Microsoft.VisualStudio.TestTools.UnitTesting; +#endif + +using Ooui; +using System.Linq; +using System.Threading.Tasks; +using System.Diagnostics; + +namespace Tests +{ + [TestClass] + public class EventTargetStressTests + { + [TestMethod] + public void LotsOfThreads () + { + var input = new Input (); + Parallel.ForEach (Enumerable.Range (0, 100), _ => StressTestInput (input)); + } + + void StressTestInput (Input input) + { + var div = new Div (input); + + var changeCount = 0; + var clickCount = 0; + + void Input_Change (object sender, TargetEventArgs e) + { + changeCount++; + } + void Div_MessageSent (Message m) + { + if (m.Key == "click") + clickCount++; + } + + input.Change += Input_Change; + div.MessageSent += Div_MessageSent; + + var sw = new Stopwatch (); + sw.Start (); + while (sw.ElapsedMilliseconds < 1000) { + + var t = sw.Elapsed.ToString (); + input.Receive (Message.Event (input.Id, "change", t)); + + var b = new Button ("Click"); + input.AppendChild (b); + b.Send (Message.Event (b.Id, "click")); + input.RemoveChild (b); + } + + input.Change -= Input_Change; + div.RemoveChild (input); + div.MessageSent -= Div_MessageSent; + + Assert.IsTrue (changeCount > 0); + Assert.IsTrue (clickCount > 0); + } + } +}