Exemplo n.º 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();
 }
Exemplo n.º 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);
        }
Exemplo n.º 3
0
 public void TestMonitorWithInvalidSyncObject()
 {
     this.TestWithException <ArgumentNullException>(() =>
     {
         using var monitor = SynchronizedBlock.Lock(null);
     },
                                                    replay: true);
 }
Exemplo n.º 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());
 }
Exemplo n.º 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.");
     }
 }
Exemplo n.º 6
0
        public void TestMonitorWithInvalidWaitState()
        {
            this.TestWithException <SynchronizationLockException>(() =>
            {
                SynchronizedBlock monitor;
                using (monitor = SynchronizedBlock.Lock(new object()))
                {
                }

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