public void MultipleDispose_DoesntThrow() { var m = new SingleGlobalInstanceMutex("test_00"); m.Dispose(); m.Dispose(); m.Dispose(); }
public void MultipleDispose_DoesntThrow() { var m = new SingleGlobalInstanceMutex("test_00"); m.Invoking(x => x.Dispose()).Should().NotThrow(); m.Invoking(x => x.Dispose()).Should().NotThrow(); m.Invoking(x => x.Dispose()).Should().NotThrow(); }
public void TestSynchronization_WithMutexWrapper() { // Arrange const string mutexName = "sonarsource.scannerformsbuild.test1"; var oneMinute = TimeSpan.FromMinutes(1); var steps = new List <int>(10); var t1 = new Thread(() => { steps.Add(101); using (var m = new SingleGlobalInstanceMutex(mutexName, oneMinute)) { steps.Add(102); } steps.Add(103); }); var t2 = new Thread(() => { try { new SingleGlobalInstanceMutex(mutexName, oneMinute); steps.Add(201); Thread.Sleep(oneMinute); steps.Add(202); } catch (ThreadAbortException) { Thread.Sleep(500); steps.Add(203); } }); var t3 = new Thread(() => { steps.Add(301); using (var m = new SingleGlobalInstanceMutex(mutexName, oneMinute)) { Thread.Sleep(500); steps.Add(302); } steps.Add(303); }); // Act & Assert t1.Start(); WaitForStep(steps, 103); steps.Should().BeEquivalentTo(new[] { 101, 102, 103 }); t2.Start(); WaitForStep(steps, 201); steps.Should().BeEquivalentTo(new[] { 101, 102, 103, 201 }); t3.Start(); WaitForStep(steps, 301); steps.Should().BeEquivalentTo(new[] { 101, 102, 103, 201, 301 }); t2.Abort(); WaitForStep(steps, 203); steps.Should().BeEquivalentTo(new[] { 101, 102, 103, 201, 301, 203 }); WaitForStep(steps, 303); steps.Should().BeEquivalentTo(new[] { 101, 102, 103, 201, 301, 203, 302, 303 }); }
public void TestSynchronization_WithMutexWrapper() { // Arrange const string mutexName = "sonarsource.scannerformsbuild.test1"; var oneMinute = TimeSpan.FromMinutes(1); var steps = new List <int>(10); var cancel = new CancellationTokenSource(); var t1 = new Thread(() => { steps.Add(101); using (var m = new SingleGlobalInstanceMutex(mutexName, oneMinute)) { steps.Add(102); } steps.Add(103); }); var t2 = new Thread(() => { try { new SingleGlobalInstanceMutex(mutexName, oneMinute); steps.Add(201); Task.Delay(oneMinute, cancel.Token).Wait(); steps.Add(202); } catch (AggregateException aggEx) when(aggEx.InnerException is TaskCanceledException) { Thread.Sleep(500); steps.Add(203); } }); var t3 = new Thread(() => { steps.Add(301); using (var m = new SingleGlobalInstanceMutex(mutexName, oneMinute)) { Thread.Sleep(500); steps.Add(302); } steps.Add(303); }); // Act & Assert t1.Start(); WaitForStep(steps, 103); steps.Should().BeEquivalentTo(new[] { 101, 102, 103 }); t2.Start(); WaitForStep(steps, 201); steps.Should().BeEquivalentTo(new[] { 101, 102, 103, 201 }); t3.Start(); WaitForStep(steps, 301); steps.Should().BeEquivalentTo(new[] { 101, 102, 103, 201, 301 }); cancel.Cancel(); WaitForStep(steps, 203); steps.Should().BeEquivalentTo(new[] { 101, 102, 103, 201, 301, 203 }); WaitForStep(steps, 303); steps.Should().BeEquivalentTo(new[] { 101, 102, 103, 201, 301, 203, 302, 303 }); }