public void ReadSmallerBuffers()
        {
            using (var stream = new BlockingMemoryStream())
            {
                stream.Write(new byte[] { 1, 2, 3, 4 }, 0, 4);
                stream.Write(new byte[] { 5, 6, 7, 8, 9 }, 0, 5);

                byte[] buffer = new byte[3];

                int count = stream.Read(buffer, 0, buffer.Length);
                Assert.Equal(3, count);
                Assert.Equal(1, buffer[0]);
                Assert.Equal(2, buffer[1]);
                Assert.Equal(3, buffer[2]);

                count = stream.Read(buffer, 0, buffer.Length);
                Assert.Equal(1, count);
                Assert.Equal(4, buffer[0]);

                count = stream.Read(buffer, 0, buffer.Length);
                Assert.Equal(3, count);
                Assert.Equal(5, buffer[0]);
                Assert.Equal(6, buffer[1]);
                Assert.Equal(7, buffer[2]);

                count = stream.Read(buffer, 0, buffer.Length);
                Assert.Equal(2, count);
                Assert.Equal(8, buffer[0]);
                Assert.Equal(9, buffer[1]);
            }
        }
예제 #2
0
        public void ReadSmallerBuffers()
        {
            using (var stream = new BlockingMemoryStream())
            {
                stream.Write(new byte[] { 1, 2, 3, 4 }, 0, 4);
                stream.Write(new byte[] { 5, 6, 7, 8, 9 }, 0, 5);

                byte[] buffer = new byte[3];

                int count = stream.Read(buffer, 0, buffer.Length);
                Assert.Equal(3, count);
                Assert.Equal(1, buffer[0]);
                Assert.Equal(2, buffer[1]);
                Assert.Equal(3, buffer[2]);

                count = stream.Read(buffer, 0, buffer.Length);
                Assert.Equal(1, count);
                Assert.Equal(4, buffer[0]);

                count = stream.Read(buffer, 0, buffer.Length);
                Assert.Equal(3, count);
                Assert.Equal(5, buffer[0]);
                Assert.Equal(6, buffer[1]);
                Assert.Equal(7, buffer[2]);

                count = stream.Read(buffer, 0, buffer.Length);
                Assert.Equal(2, count);
                Assert.Equal(8, buffer[0]);
                Assert.Equal(9, buffer[1]);
            }
        }
        public void ReadBiggerBuffer()
        {
            using (var stream = new BlockingMemoryStream())
            {
                stream.Write(new byte[] { 1, 2, 3 }, 0, 3);

                byte[] buffer = new byte[10];
                int count = stream.Read(buffer, 0, buffer.Length);
                Assert.Equal(3, count);
                Assert.Equal(1, buffer[0]);
                Assert.Equal(2, buffer[1]);
                Assert.Equal(3, buffer[2]);
            }
        }
예제 #4
0
        public void ReadBiggerBuffer()
        {
            using (var stream = new BlockingMemoryStream())
            {
                stream.Write(new byte[] { 1, 2, 3 }, 0, 3);

                byte[] buffer = new byte[10];
                int    count  = stream.Read(buffer, 0, buffer.Length);
                Assert.Equal(3, count);
                Assert.Equal(1, buffer[0]);
                Assert.Equal(2, buffer[1]);
                Assert.Equal(3, buffer[2]);
            }
        }
예제 #5
0
        public void TestReadBlocksUntilWrite()
        {
            using (var stream = new BlockingMemoryStream())
            {
                ManualResetEvent readerThreadExecuting  = new ManualResetEvent(false);
                bool             readerThreadSuccessful = false;

                Thread readerThread = new Thread(() =>
                {
                    byte[] buffer = new byte[10];
                    readerThreadExecuting.Set();
                    int count = stream.Read(buffer, 0, buffer.Length);

                    Assert.Equal(3, count);
                    Assert.Equal(1, buffer[0]);
                    Assert.Equal(2, buffer[1]);
                    Assert.Equal(3, buffer[2]);

                    readerThreadSuccessful = true;
                });

                readerThread.IsBackground = true;
                readerThread.Start();

                // ensure the thread is executing
                readerThreadExecuting.WaitOne();

                Assert.True(readerThread.IsAlive);

                // give it a little while to ensure it is blocking
                Thread.Sleep(10);
                Assert.True(readerThread.IsAlive);

                stream.Write(new byte[] { 1, 2, 3 }, 0, 3);

                Assert.True(readerThread.Join(1000));
                Assert.True(readerThreadSuccessful);
            }
        }
        public void TestReadBlocksUntilWrite()
        {
            using (var stream = new BlockingMemoryStream())
            {
                ManualResetEvent readerThreadExecuting = new ManualResetEvent(false);
                bool readerThreadSuccessful = false;

                Thread readerThread = new Thread(() =>
                {
                    byte[] buffer = new byte[10];
                    readerThreadExecuting.Set();
                    int count = stream.Read(buffer, 0, buffer.Length);

                    Assert.Equal(3, count);
                    Assert.Equal(1, buffer[0]);
                    Assert.Equal(2, buffer[1]);
                    Assert.Equal(3, buffer[2]);

                    readerThreadSuccessful = true;
                });

                readerThread.IsBackground = true;
                readerThread.Start();

                // ensure the thread is executing
                readerThreadExecuting.WaitOne();

                Assert.True(readerThread.IsAlive);
               
                // give it a little while to ensure it is blocking
                Thread.Sleep(10);
                Assert.True(readerThread.IsAlive);

                stream.Write(new byte[] { 1, 2, 3 }, 0, 3);

                Assert.True(readerThread.Join(1000));
                Assert.True(readerThreadSuccessful);
            }
        }