예제 #1
0
 internal void ReentrantWait()
 {
     Debug.WriteLine("Entering lock on task {0}.", GetCurrentTaskId());
     using var monitor = SynchronizedBlock.Lock(this.SyncObject);
     Debug.WriteLine("Entered lock on task {0}.", GetCurrentTaskId());
     this.DoWait();
 }
예제 #2
0
        public void TestMonitorWithInvalidUsage()
        {
            this.TestWithError(async() =>
            {
                try
                {
                    var monitor = SynchronizedBlock.Lock(new object());
                    // We yield to make sure the execution is asynchronous.
                    await Task.Yield();
                    monitor.Pulse();

                    // We do not dispose inside a using statement, because the `SynchronizationLockException`
                    // will trigger the disposal, which will fail because an await statement is not allowed
                    // inside a synchronized block. The C# compiler normally prevents it when using the lock
                    // statement, but we cannot prevent it when directly using the mock.
                    monitor.Dispose();
                }
                catch (SynchronizationLockException)
                {
                    Specification.Assert(false, "Expected exception thrown.");
                }
            },
                               expectedError: "Expected exception thrown.",
                               replay: true);
        }
예제 #3
0
 public void TestMonitorWithInvalidSyncObject()
 {
     this.TestWithException <ArgumentNullException>(() =>
     {
         using var monitor = SynchronizedBlock.Lock(null);
     },
                                                    replay: true);
 }
예제 #4
0
 internal void DoWait()
 {
     using var monitor = SynchronizedBlock.Lock(this.SyncObject);
     Debug.WriteLine("Re-entered lock from the same task {0}.", GetCurrentTaskId());
     Debug.WriteLine("Task {0} is now waiting...", GetCurrentTaskId());
     this.Wait();
     Debug.WriteLine("Task {0} received the signal.", GetCurrentTaskId());
 }
예제 #5
0
 internal void Wait()
 {
     using var monitor = SynchronizedBlock.Lock(this.SyncObject);
     while (!this.Signalled)
     {
         bool result = monitor.Wait();
         Assert.True(result, "Wait returned false.");
     }
 }
예제 #6
0
        public void TestMonitorWithInvalidWaitState()
        {
            this.TestWithException <SynchronizationLockException>(() =>
            {
                SynchronizedBlock monitor;
                using (monitor = SynchronizedBlock.Lock(new object()))
                {
                }

                monitor.Wait();
            },
                                                                  replay: true);
        }
예제 #7
0
 internal void DoLock()
 {
     using var monitor = SynchronizedBlock.Lock(this.SyncObject);
     Debug.WriteLine("Re-entered lock from the same task {0}.", GetCurrentTaskId());
 }
예제 #8
0
 internal void Signal()
 {
     using var monitor = SynchronizedBlock.Lock(this.SyncObject);
     this.Signalled    = true;
     monitor.Pulse();
 }