Example #1
0
        public void ReadWrite_TimeoutException()
        {
            bool timedout = false;
            var  name     = Guid.NewGuid().ToString();

            byte[] data     = new byte[1024];
            byte[] readData = new byte[1024];


            using (var buf = new BufferReadWrite(name, 1024))
                using (var buf2 = new BufferReadWrite(name))
                {
                    // Set a small timeout to speed up the test
                    buf2.ReadWriteTimeout = 0;

                    // Introduce possible deadlock by acquiring without releasing the write lock.
                    buf.AcquireWriteLock();

                    // We want the AcquireReadLock to fail
                    if (!buf2.AcquireReadLock(1))
                    {
                        try
                        {
                            // Read should timeout with TimeoutException because buf.ReleaseWriteLock has not been called
                            buf2.Read(readData);
                        }
                        catch (TimeoutException)
                        {
                            timedout = true;
                        }
                    }
                    Assert.AreEqual(true, timedout, "The TimeoutException was not thrown.");

                    // Remove the deadlock situation, by releasing the write lock...
                    buf.ReleaseWriteLock();
                    // ...and ensure that we can now read the data
                    if (buf.AcquireReadLock(1))
                    {
                        buf2.Read(readData);
                    }
                    else
                    {
                        Assert.Fail("Failed to acquire read lock after releasing write lock.");
                    }
                }
        }