| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520 |
- // ReSharper disable AccessToDisposedClosure
- #nullable enable
- namespace ApplicationTests.Keyboard;
- /// <summary>
- /// Tests to verify that KeyboardImpl is thread-safe for concurrent access scenarios.
- /// </summary>
- public class KeyboardImplThreadSafetyTests
- {
- [Fact]
- public void AddCommand_ConcurrentAccess_NoExceptions ()
- {
- // Arrange
- var keyboard = new KeyboardImpl ();
- List<Exception> exceptions = [];
- const int NUM_THREADS = 10;
- const int OPERATIONS_PER_THREAD = 50;
- // Act
- List<Task> tasks = [];
- for (var i = 0; i < NUM_THREADS; i++)
- {
- tasks.Add (
- Task.Run (() =>
- {
- for (var j = 0; j < OPERATIONS_PER_THREAD; j++)
- {
- try
- {
- // AddKeyBindings internally calls AddCommand multiple times
- keyboard.AddKeyBindings ();
- }
- catch (InvalidOperationException)
- {
- // Expected - AddKeyBindings tries to add keys that already exist
- }
- catch (Exception ex)
- {
- exceptions.Add (ex);
- }
- }
- }));
- }
- #pragma warning disable xUnit1031 // Test methods should not use blocking task operations - intentional for stress testing
- Task.WaitAll (tasks.ToArray ());
- #pragma warning restore xUnit1031
- // Assert
- Assert.Empty (exceptions);
- keyboard.Dispose ();
- }
- [Fact]
- public void Dispose_WhileOperationsInProgress_NoExceptions ()
- {
- // Arrange
- IApplication? app = Application.Create ();
- app.Init ("fake");
- var keyboard = new KeyboardImpl { App = app };
- keyboard.AddKeyBindings ();
- List<Exception> exceptions = [];
- var continueRunning = true;
- // Act
- Task operationsTask = Task.Run (() =>
- {
- while (continueRunning)
- {
- try
- {
- keyboard.InvokeCommandsBoundToKey (Key.Q.WithCtrl);
- IEnumerable<KeyValuePair<Key, KeyBinding>> bindings = keyboard.KeyBindings.GetBindings ();
- int count = bindings.Count ();
- }
- catch (ObjectDisposedException)
- {
- // Expected - keyboard was disposed
- break;
- }
- catch (Exception ex)
- {
- exceptions.Add (ex);
- break;
- }
- }
- });
- // Give operations a chance to start
- Thread.Sleep (10);
- // Dispose while operations are running
- keyboard.Dispose ();
- continueRunning = false;
- #pragma warning disable xUnit1031 // Test methods should not use blocking task operations - intentional for stress testing
- operationsTask.Wait (TimeSpan.FromSeconds (2));
- #pragma warning restore xUnit1031
- // Assert
- Assert.Empty (exceptions);
- app.Dispose ();
- }
- [Fact]
- public void InvokeCommand_ConcurrentAccess_NoExceptions ()
- {
- // Arrange
- IApplication? app = Application.Create ();
- app.Init ("fake");
- var keyboard = new KeyboardImpl { App = app };
- keyboard.AddKeyBindings ();
- List<Exception> exceptions = [];
- const int NUM_THREADS = 10;
- const int OPERATIONS_PER_THREAD = 50;
- // Act
- List<Task> tasks = new ();
- for (var i = 0; i < NUM_THREADS; i++)
- {
- tasks.Add (
- Task.Run (() =>
- {
- for (var j = 0; j < OPERATIONS_PER_THREAD; j++)
- {
- try
- {
- var binding = new KeyBinding ([Command.Quit]);
- keyboard.InvokeCommand (Command.Quit, Key.Q.WithCtrl, binding);
- }
- catch (Exception ex)
- {
- exceptions.Add (ex);
- }
- }
- }));
- }
- #pragma warning disable xUnit1031 // Test methods should not use blocking task operations - intentional for stress testing
- Task.WaitAll (tasks.ToArray ());
- #pragma warning restore xUnit1031
- // Assert
- Assert.Empty (exceptions);
- keyboard.Dispose ();
- app.Dispose ();
- }
- [Fact]
- public void InvokeCommandsBoundToKey_ConcurrentAccess_NoExceptions ()
- {
- // Arrange
- IApplication? app = Application.Create ();
- app.Init ("fake");
- var keyboard = new KeyboardImpl { App = app };
- keyboard.AddKeyBindings ();
- List<Exception> exceptions = [];
- const int NUM_THREADS = 10;
- const int OPERATIONS_PER_THREAD = 50;
- // Act
- List<Task> tasks = [];
- for (var i = 0; i < NUM_THREADS; i++)
- {
- tasks.Add (
- Task.Run (() =>
- {
- for (var j = 0; j < OPERATIONS_PER_THREAD; j++)
- {
- try
- {
- keyboard.InvokeCommandsBoundToKey (Key.Q.WithCtrl);
- }
- catch (Exception ex)
- {
- exceptions.Add (ex);
- }
- }
- }));
- }
- #pragma warning disable xUnit1031 // Test methods should not use blocking task operations - intentional for stress testing
- Task.WaitAll (tasks.ToArray ());
- #pragma warning restore xUnit1031
- // Assert
- Assert.Empty (exceptions);
- keyboard.Dispose ();
- app.Dispose ();
- }
- [Fact]
- public void KeyBindings_ConcurrentAdd_NoExceptions ()
- {
- // Arrange
- var keyboard = new KeyboardImpl ();
- // Don't call AddKeyBindings here to avoid conflicts
- List<Exception> exceptions = [];
- const int NUM_THREADS = 10;
- const int OPERATIONS_PER_THREAD = 50;
- // Act
- List<Task> tasks = new ();
- for (var i = 0; i < NUM_THREADS; i++)
- {
- int threadId = i;
- tasks.Add (
- Task.Run (() =>
- {
- for (var j = 0; j < OPERATIONS_PER_THREAD; j++)
- {
- try
- {
- // Use unique keys per thread to avoid conflicts
- Key key = Key.F1 + threadId * OPERATIONS_PER_THREAD + j;
- keyboard.KeyBindings.Add (key, Command.Refresh);
- }
- catch (InvalidOperationException)
- {
- // Expected - duplicate key
- }
- catch (ArgumentException)
- {
- // Expected - invalid key
- }
- catch (Exception ex)
- {
- exceptions.Add (ex);
- }
- }
- }));
- }
- #pragma warning disable xUnit1031 // Test methods should not use blocking task operations - intentional for stress testing
- Task.WaitAll (tasks.ToArray ());
- #pragma warning restore xUnit1031
- // Assert
- Assert.Empty (exceptions);
- keyboard.Dispose ();
- }
- [Fact]
- public void KeyDown_KeyUp_Events_ConcurrentSubscription_NoExceptions ()
- {
- // Arrange
- var keyboard = new KeyboardImpl ();
- keyboard.AddKeyBindings ();
- List<Exception> exceptions = [];
- const int NUM_THREADS = 10;
- const int OPERATIONS_PER_THREAD = 20;
- var keyDownCount = 0;
- var keyUpCount = 0;
- // Act
- List<Task> tasks = new ();
- // Threads subscribing to events
- for (var i = 0; i < NUM_THREADS; i++)
- {
- tasks.Add (
- Task.Run (() =>
- {
- for (var j = 0; j < OPERATIONS_PER_THREAD; j++)
- {
- try
- {
- EventHandler<Key> handler = (s, e) => { Interlocked.Increment (ref keyDownCount); };
- keyboard.KeyDown += handler;
- keyboard.KeyDown -= handler;
- EventHandler<Key> upHandler = (s, e) => { Interlocked.Increment (ref keyUpCount); };
- keyboard.KeyUp += upHandler;
- keyboard.KeyUp -= upHandler;
- }
- catch (Exception ex)
- {
- exceptions.Add (ex);
- }
- }
- }));
- }
- #pragma warning disable xUnit1031 // Test methods should not use blocking task operations - intentional for stress testing
- Task.WaitAll (tasks.ToArray ());
- #pragma warning restore xUnit1031
- // Assert
- Assert.Empty (exceptions);
- keyboard.Dispose ();
- }
- [Fact]
- public void KeyProperty_Setters_ConcurrentAccess_NoExceptions ()
- {
- // Arrange
- var keyboard = new KeyboardImpl ();
- // Initialize once before concurrent access
- keyboard.AddKeyBindings ();
- List<Exception> exceptions = [];
- const int NUM_THREADS = 10;
- const int OPERATIONS_PER_THREAD = 20;
- // Act
- List<Task> tasks = new ();
- for (var i = 0; i < NUM_THREADS; i++)
- {
- int threadId = i;
- tasks.Add (
- Task.Run (() =>
- {
- for (var j = 0; j < OPERATIONS_PER_THREAD; j++)
- {
- try
- {
- // Cycle through different key combinations
- switch (j % 6)
- {
- case 0:
- keyboard.QuitKey = Key.Q.WithCtrl;
- break;
- case 1:
- keyboard.ArrangeKey = Key.F6.WithCtrl;
- break;
- case 2:
- keyboard.NextTabKey = Key.Tab;
- break;
- case 3:
- keyboard.PrevTabKey = Key.Tab.WithShift;
- break;
- case 4:
- keyboard.NextTabGroupKey = Key.F6;
- break;
- case 5:
- keyboard.PrevTabGroupKey = Key.F6.WithShift;
- break;
- }
- }
- catch (Exception ex)
- {
- exceptions.Add (ex);
- }
- }
- }));
- }
- #pragma warning disable xUnit1031 // Test methods should not use blocking task operations - intentional for stress testing
- Task.WaitAll (tasks.ToArray ());
- #pragma warning restore xUnit1031
- // Assert
- Assert.Empty (exceptions);
- keyboard.Dispose ();
- }
- [Fact]
- public void MixedOperations_ConcurrentAccess_NoExceptions ()
- {
- // Arrange
- IApplication? app = Application.Create ();
- app.Init ("fake");
- var keyboard = new KeyboardImpl { App = app };
- keyboard.AddKeyBindings ();
- List<Exception> exceptions = [];
- const int OPERATIONS_PER_THREAD = 30;
- // Act
- List<Task> tasks = new ();
- // Thread 1: Add bindings with unique keys
- tasks.Add (
- Task.Run (() =>
- {
- for (var j = 0; j < OPERATIONS_PER_THREAD; j++)
- {
- try
- {
- // Use high key codes to avoid conflicts
- var key = new Key ((KeyCode)((int)KeyCode.F20 + j));
- keyboard.KeyBindings.Add (key, Command.Refresh);
- }
- catch (InvalidOperationException)
- {
- // Expected - duplicate
- }
- catch (ArgumentException)
- {
- // Expected - invalid key
- }
- catch (Exception ex)
- {
- exceptions.Add (ex);
- }
- }
- }));
- // Thread 2: Invoke commands
- tasks.Add (
- Task.Run (() =>
- {
- for (var j = 0; j < OPERATIONS_PER_THREAD; j++)
- {
- try
- {
- keyboard.InvokeCommandsBoundToKey (Key.Q.WithCtrl);
- }
- catch (Exception ex)
- {
- exceptions.Add (ex);
- }
- }
- }));
- // Thread 3: Read bindings
- tasks.Add (
- Task.Run (() =>
- {
- for (var j = 0; j < OPERATIONS_PER_THREAD; j++)
- {
- try
- {
- IEnumerable<KeyValuePair<Key, KeyBinding>> bindings = keyboard.KeyBindings.GetBindings ();
- int count = bindings.Count ();
- Assert.True (count >= 0);
- }
- catch (Exception ex)
- {
- exceptions.Add (ex);
- }
- }
- }));
- // Thread 4: Change key properties
- tasks.Add (
- Task.Run (() =>
- {
- for (var j = 0; j < OPERATIONS_PER_THREAD; j++)
- {
- try
- {
- keyboard.QuitKey = j % 2 == 0 ? Key.Q.WithCtrl : Key.Esc;
- }
- catch (Exception ex)
- {
- exceptions.Add (ex);
- }
- }
- }));
- #pragma warning disable xUnit1031 // Test methods should not use blocking task operations - intentional for stress testing
- Task.WaitAll (tasks.ToArray ());
- #pragma warning restore xUnit1031
- // Assert
- Assert.Empty (exceptions);
- keyboard.Dispose ();
- app.Dispose ();
- }
- [Fact]
- public void RaiseKeyDownEvent_ConcurrentAccess_NoExceptions ()
- {
- // Arrange
- IApplication? app = Application.Create ();
- app.Init ("fake");
- var keyboard = new KeyboardImpl { App = app };
- keyboard.AddKeyBindings ();
- List<Exception> exceptions = [];
- const int NUM_THREADS = 5;
- const int OPERATIONS_PER_THREAD = 20;
- // Act
- List<Task> tasks = new ();
- for (var i = 0; i < NUM_THREADS; i++)
- {
- tasks.Add (
- Task.Run (() =>
- {
- for (var j = 0; j < OPERATIONS_PER_THREAD; j++)
- {
- try
- {
- keyboard.RaiseKeyDownEvent (Key.A);
- }
- catch (Exception ex)
- {
- exceptions.Add (ex);
- }
- }
- }));
- }
- #pragma warning disable xUnit1031 // Test methods should not use blocking task operations - intentional for stress testing
- Task.WaitAll (tasks.ToArray ());
- #pragma warning restore xUnit1031
- // Assert
- Assert.Empty (exceptions);
- keyboard.Dispose ();
- app.Dispose ();
- }
- }
|