From 7f183e7f59fc635fa99c9c36678bb078fb206da1 Mon Sep 17 00:00:00 2001 From: BDisp Date: Mon, 3 Aug 2020 14:46:55 +0100 Subject: [PATCH 1/2] Providing a simple test for Fixes #740. Multi thread toplevels. --- UnitTests/ViewTests.cs | 84 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/UnitTests/ViewTests.cs b/UnitTests/ViewTests.cs index 1e5f5eb70..12335a3a4 100644 --- a/UnitTests/ViewTests.cs +++ b/UnitTests/ViewTests.cs @@ -899,5 +899,89 @@ namespace Terminal.Gui { Application.Run (); Application.Shutdown (); } + + [Fact] + public void Multi_Thread_Toplevels () + { + Application.Init (new FakeDriver (), new NetMainLoop (() => FakeConsole.ReadKey (true))); + + var t = Application.Top; + var w = new Window (); + t.Add (w); + + int count = 0, count1 = 0, count2 = 0; + bool log = false, log1 = false, log2 = false; + + Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (100), (_) => { + count++; + if (count1 == 5) { + log1 = true; + } + if (count == 30) { + Assert.Equal (30, count); + Assert.Equal (20, count1); + Assert.Equal (10, count2); + + Assert.True (log); + Assert.True (log1); + Assert.True (log2); + + Application.RequestStop (); + return false; + } + return true; + }); + + t.Ready = () => { + FirstDialogToplevel (); + }; + + void FirstDialogToplevel () + { + var od = new OpenDialog { + Ready = () => { + SecoundDialogToplevel (); + } + }; + + Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (100), (_) => { + count1++; + if (count2 == 5) { + log2 = true; + } + if (count1 == 20) { + Assert.Equal (20, count1); + Application.RequestStop (); + return false; + } + return true; + }); + + Application.Run (od); + } + + void SecoundDialogToplevel () + { + var d = new Dialog (); + + Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (100), (_) => { + count2++; + if (count < 30) { + log = true; + } + if (count2 == 10) { + Assert.Equal (10, count2); + Application.RequestStop (); + return false; + } + return true; + }); + + Application.Run (d); + } + + Application.Run (); + Application.Shutdown (); + } } } From d5e6bea9bb91f048dbdb61f7510b465d803aa56c Mon Sep 17 00:00:00 2001 From: BDisp Date: Mon, 3 Aug 2020 15:24:12 +0100 Subject: [PATCH 2/2] Added more conditions to sure it act as expected. --- UnitTests/ViewTests.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/UnitTests/ViewTests.cs b/UnitTests/ViewTests.cs index 12335a3a4..9dd570375 100644 --- a/UnitTests/ViewTests.cs +++ b/UnitTests/ViewTests.cs @@ -911,12 +911,21 @@ namespace Terminal.Gui { int count = 0, count1 = 0, count2 = 0; bool log = false, log1 = false, log2 = false; + bool fromTopStillKnowFirstIsRunning = false; + bool fromTopStillKnowSecondIsRunning = false; + bool fromFirstStillKnowSecondIsRunning = false; Application.MainLoop.AddTimeout (TimeSpan.FromMilliseconds (100), (_) => { count++; if (count1 == 5) { log1 = true; } + if (count1 > 13 && count < 15) { + fromTopStillKnowFirstIsRunning = true; + } + if (count2 > 6 && count2 < 8) { + fromTopStillKnowSecondIsRunning = true; + } if (count == 30) { Assert.Equal (30, count); Assert.Equal (20, count1); @@ -926,6 +935,10 @@ namespace Terminal.Gui { Assert.True (log1); Assert.True (log2); + Assert.True (fromTopStillKnowFirstIsRunning); + Assert.True (fromTopStillKnowSecondIsRunning); + Assert.True (fromFirstStillKnowSecondIsRunning); + Application.RequestStop (); return false; } @@ -949,6 +962,9 @@ namespace Terminal.Gui { if (count2 == 5) { log2 = true; } + if (count2 > 3 && count2 < 5) { + fromFirstStillKnowSecondIsRunning = true; + } if (count1 == 20) { Assert.Equal (20, count1); Application.RequestStop ();