示例#1
0
        public void DisposeTest()
        {
            MemorySlab slab = new MemorySlab(blockSize * 3, null);

            ManagedBuffer target = GetNewBuffer(slab);

            target.Dispose();

            //Call Dispose again, shouldn't cause any exceptions to be thrown
            target.Dispose();

            //Try to work with disposed object. should throw exceptions
            {
                bool exceptionThrown = false;

                try
                {
                    target.FillWith(new byte[] { 0, 1, 2 });
                }
                catch (ObjectDisposedException)
                {
                    exceptionThrown = true;
                }

                Assert.IsTrue(exceptionThrown);
            }

            {
                bool exceptionThrown = false;

                try
                {
                    target.CopyTo(new byte[] { 0, 1, 2 });
                }
                catch (ObjectDisposedException)
                {
                    exceptionThrown = true;
                }

                Assert.IsTrue(exceptionThrown);
            }

            {
                bool exceptionThrown = false;

                try
                {
                    target.GetSegments();
                }
                catch (ObjectDisposedException)
                {
                    exceptionThrown = true;
                }

                Assert.IsTrue(exceptionThrown);
            }
        }
示例#2
0
        public void TestDispose()
        {
            const int maxBufferPoolSize = 2 << 32;
            const int maxBufferSize     = 2 << 16;
            var       bufferManager     = BufferManager.CreateBufferManager(maxBufferPoolSize, maxBufferSize);

            var len = 13;
            var buf = bufferManager.TakeBuffer(len);

            var managedBuffer = new ManagedBuffer(buf, len, bufferManager);

            managedBuffer.Dispose();
            managedBuffer.Dispose();
        }
示例#3
0
        public void LengthTest()
        {
            MemorySlab slab = new MemorySlab(blockSize * 3, null);

            ManagedBuffer target = GetNewBuffer(slab);

            Assert.AreEqual <long>(blockSize, target.Size);
            target.Dispose();
        }
示例#4
0
        public void IsDisposedTest()
        {
            MemorySlab slab = new MemorySlab(blockSize * 3, null);

            ManagedBuffer target = GetNewBuffer(slab);

            Assert.IsFalse(target.IsDisposed);
            target.Dispose();
            Assert.IsTrue(target.IsDisposed);
        }
示例#5
0
        public void TestFinalise()
        {
            const int maxBufferPoolSize = 2 << 32;
            const int maxBufferSize     = 2 << 16;
            var       bufferManager     = BufferManager.CreateBufferManager(maxBufferPoolSize, maxBufferSize);

            const int len = 13;
            var       buf = bufferManager.TakeBuffer(len);

            var managedBuffer = new ManagedBuffer(buf, len, bufferManager);

            managedBuffer.Dispose();
            managedBuffer = null;

            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
示例#6
0
        public void NewBufferIsFreshTest()
        {
            MemorySlab slab = new MemorySlab(blockSize * 3, null);

            //Save some random junk into a buffer and dispose it.
            {
                ManagedBuffer target1     = GetNewBuffer(slab);
                byte[]        SourceArray = GetRandomizedByteArray(blockSize);
                target1.FillWith(SourceArray);
                target1.Dispose();
            }

            //Get a fresh buffer and confirm that it's zero-filled.
            {
                ManagedBuffer target2   = GetNewBuffer(slab);
                byte[]        DestArray = new byte[blockSize];
                target2.CopyTo(DestArray);
                Assert.IsTrue(ArraysMatch(DestArray, new byte[blockSize]));
                target2.Dispose();
            }
        }
示例#7
0
 public void TestBuffer()
 {
     var buffer = default(ManagedBuffer);
      // invalid constructor
      try
      {
     new ManagedBuffer(null, 0);
     Assert.Fail("Expected: Exception");
      }
      catch (AssertFailedException) { throw; }
      catch { }
      try
      {
     new ManagedBuffer(null, 8192);
     Assert.Fail("Expected: Exception");
      }
      catch (AssertFailedException) { throw; }
      catch { }
      try
      {
     new ManagedBuffer(manager, 0);
     Assert.Fail("Expected: Exception");
      }
      catch (AssertFailedException) { throw; }
      catch { }
      try
      {
     new ManagedBuffer(manager, -1);
     Assert.Fail("Expected: Exception");
      }
      catch (AssertFailedException) { throw; }
      catch { }
      try
      {
     new ManagedBuffer(null, default(ArraySegment<Byte>));
     Assert.Fail("Expected: Exception");
      }
      catch (AssertFailedException) { throw; }
      catch { }
      try
      {
     new ManagedBuffer(null, new ArraySegment<Byte>(new Byte[8192]));
     Assert.Fail("Expected: Exception");
      }
      catch (AssertFailedException) { throw; }
      catch { }
      try
      {
     new ManagedBuffer(manager, default(ArraySegment<Byte>));
     Assert.Fail("Expected: Exception");
      }
      catch (AssertFailedException) { throw; }
      catch { }
      // size constructor
      buffer = new ManagedBuffer(manager, 8192);
      Assert.IsNotNull(buffer.Raw);
      Assert.IsNotNull(buffer.Data.Array);
      Assert.AreEqual(buffer.Offset, 0);
      Assert.AreEqual(buffer.Data.Offset, 0);
      Assert.AreEqual(buffer.Length, 8192);
      Assert.AreEqual(buffer.Data.Count, 8192);
      buffer.Dispose();
      Assert.IsNull(buffer.Raw);
      Assert.IsNull(buffer.Data.Array);
      Assert.AreEqual(buffer.Offset, 0);
      Assert.AreEqual(buffer.Data.Offset, 0);
      Assert.AreEqual(buffer.Length, 0);
      Assert.AreEqual(buffer.Data.Count, 0);
      // buffer constructor
      buffer = new ManagedBuffer(manager, new ArraySegment<Byte>(manager.TakeBuffer(8192)));
      Assert.IsNotNull(buffer.Raw);
      Assert.IsNotNull(buffer.Data.Array);
      Assert.AreEqual(buffer.Offset, 0);
      Assert.AreEqual(buffer.Data.Offset, 0);
      Assert.AreEqual(buffer.Length, 8192);
      Assert.AreEqual(buffer.Data.Count, 8192);
      buffer.Dispose();
      Assert.IsNull(buffer.Raw);
      Assert.IsNull(buffer.Data.Array);
      Assert.AreEqual(buffer.Offset, 0);
      Assert.AreEqual(buffer.Data.Offset, 0);
      Assert.AreEqual(buffer.Length, 0);
      Assert.AreEqual(buffer.Data.Count, 0);
      // multiple disposal
      buffer.Dispose();
      buffer.Dispose();
 }