private void ResourceSimulation(Action action) { _lock = new AsyncLock(); //start n threads and have them obtain the lock and randomly wait, then verify var failure = new ManualResetEventSlim(false); _resource = new LimitedResource(() => { failure.Set(); }); var testCount = 20; _countdown = new CountdownEvent(testCount); for (int i = 0; i < testCount; ++i) { action(); } //MSTest does not support async test methods (apparently, but I could be wrong) //await Task.WhenAll(tasks); if (WaitHandle.WaitAny(new[] { _countdown.WaitHandle, failure.WaitHandle }) == 1) { Assert.Fail("More than one thread simultaneously accessed the underlying resource!"); } }
private void ResourceSimulation(Action action) { _lock = new AsyncLock(); // Start n threads and have them obtain the lock and randomly wait, then verify var failure = new ManualResetEventSlim(false); _resource = new LimitedResource(() => { failure.Set(); }); var testCount = 20; _countdown = new CountdownEvent(testCount); for (int i = 0; i < testCount; ++i) { action(); } if (WaitHandle.WaitAny(new[] { _countdown.WaitHandle, failure.WaitHandle }) == 1) { Assert.Fail("More than one thread simultaneously accessed the underlying resource!"); } }