From 0dfadd0c01de57fb1b846e5ac6af6c3160b9ce74 Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Tue, 29 Aug 2023 07:29:12 +0200 Subject: [PATCH] [tests] Try to make CoreServices.FSEventStreamTest behave better. (#18844) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Try to make CoreServices.FSEventStreamTest better by using shorter duration locks, and re-checking success condition after timing out in case there’s a race condition. Also improve exception reporting/logging a bit. Ref: https://github.com/xamarin/maccore/issues/2630 --- .../CoreServices/FSEventStreamTest.cs | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/tests/monotouch-test/CoreServices/FSEventStreamTest.cs b/tests/monotouch-test/CoreServices/FSEventStreamTest.cs index 0b124f8b4c..fa9eac0df7 100644 --- a/tests/monotouch-test/CoreServices/FSEventStreamTest.cs +++ b/tests/monotouch-test/CoreServices/FSEventStreamTest.cs @@ -134,8 +134,12 @@ namespace MonoTouchFixtures.CoreServices { Task.Run (CreateFilesAndWaitForFSEventsThread) .ContinueWith (task => { isWorking = false; - if (task.Exception is not null) - _exceptions.Add (task.Exception); + if (task.Exception is not null) { + if (task.Exception is AggregateException ae) + _exceptions.AddRange (ae.InnerExceptions); + else + _exceptions.Add (task.Exception); + } }); while (isWorking) @@ -144,6 +148,10 @@ namespace MonoTouchFixtures.CoreServices { Invalidate (); if (_exceptions.Count > 0) { + Console.WriteLine ($"Got {_exceptions.Count} exceptions:"); + for (var e = 0; e < _exceptions.Count; e++) { + Console.WriteLine ($" #{e + 1}: {_exceptions [e].ToString ().Replace ("\n", "\n ")}"); + } if (_exceptions.Count > 1) throw new AggregateException (_exceptions); else @@ -199,11 +207,8 @@ namespace MonoTouchFixtures.CoreServices { createdThenRemovedFileCount = _createdThenRemovedFiles.Count; } - if (!_monitor.WaitOne (s_testTimeout)) - throw new TimeoutException ( - $"test has timed out at {s_testTimeout.TotalSeconds}s; " + - "increase the timeout or reduce the number of files created. " + - $"Created directories: {createdDirCount} Created files: {createdFileCount} Removed files: {removedFileCount} Created then removed files: {createdThenRemovedFileCount}"); + + var timedOut = !_monitor.WaitOne (s_testTimeout); lock (_monitor) { if (_createdDirectories.Count == 0 && @@ -211,15 +216,22 @@ namespace MonoTouchFixtures.CoreServices { _removedFiles.Count == _createdThenRemovedFiles.Count) break; } + + if (timedOut) + throw new TimeoutException ( + $"test has timed out at {s_testTimeout.TotalSeconds}s; " + + "increase the timeout or reduce the number of files created. " + + $"Created directories: {createdDirCount} Created files: {createdFileCount} Removed files: {removedFileCount} Created then removed files: {createdThenRemovedFileCount}"); } } protected override void OnEvents (FSEvent [] events) { try { - lock (_monitor) { - foreach (var evnt in events) + foreach (var evnt in events) { + lock (_monitor) { HandleEvent (evnt); + } } } catch (Exception e) { _exceptions.Add (e);