public void PumpLargeDataSetThroughTinyBuffer()
 {
     using (UnmanagedCircularBuffer ncb = UnmanagedCircularBuffer.Create(memoryStart, UnmanagedCircularBuffer.SynchronizationOverheadSize + 3)) {
         byte[] data  = new byte[10000];
         byte[] data2 = new byte[10000];
         for (int i = 0; i < data.Length; i++)
         {
             data[i] = unchecked ((byte)(i + 1));
         }
         DoParallel(
             delegate {
             using (Stream w = ncb.CreateWritingStream()) {
                 w.Write(data, 0, data.Length);
             }
         },
             delegate {
             using (Stream r = ncb.CreateReadingStream()) {
                 int count = 0;
                 while (count < data2.Length)
                 {
                     count += r.Read(data2, count, data2.Length - count);
                 }
             }
         });
         Assert.AreEqual(data, data2);
     }
 }
 public void WriteAndReadFromBuffer()
 {
     using (UnmanagedCircularBuffer ncb = UnmanagedCircularBuffer.Create(memoryStart, memoryLength)) {
         using (Stream ws = ncb.CreateWritingStream()) {
             using (Stream rs = ncb.CreateReadingStream()) {
                 ws.WriteByte(0x42);
                 Assert.AreEqual(0x42, rs.ReadByte());
             }
         }
     }
 }
 public void WriteToFullBufferCausesTimeout()
 {
     using (UnmanagedCircularBuffer ncb = UnmanagedCircularBuffer.Create(memoryStart, UnmanagedCircularBuffer.SynchronizationOverheadSize + 3)) {
         using (Stream s = ncb.CreateWritingStream()) {
             s.WriteTimeout = 100;
             try {
                 s.WriteByte(1);
                 s.WriteByte(2);
             } catch (TimeoutException) {
                 Assert.Fail("The first two calls should work");
             }
             s.WriteByte(3);
         }
     }
 }
        public void WriteAndReadFromBufferUsingMemoryMappedFile()
        {
            using (MemoryMappedFile mmf1 = MemoryMappedFile.CreateSharedMemory("Local\\TestMemory", 1024)) {
                using (UnmanagedMemory view1 = mmf1.MapView(0, 1024)) {
                    using (UnmanagedCircularBuffer ncb = UnmanagedCircularBuffer.Create(view1.Start, (int)view1.Length)) {
                        using (Stream ws = ncb.CreateWritingStream()) {
                            ws.WriteByte(0x42);
                        }

                        using (MemoryMappedFile mmf2 = MemoryMappedFile.Open("Local\\TestMemory")) {
                            using (UnmanagedMemory view2 = mmf1.MapView(0, 1024)) {
                                Assert.AreNotEqual(view1.Start, view2.Start);
                                using (UnmanagedCircularBuffer ncb2 = UnmanagedCircularBuffer.Open(view2.Start)) {
                                    using (Stream rs = ncb2.CreateReadingStream()) {
                                        Assert.AreEqual(0x42, rs.ReadByte());
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }