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); } }
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(); }
public void LengthTest() { MemorySlab slab = new MemorySlab(blockSize * 3, null); ManagedBuffer target = GetNewBuffer(slab); Assert.AreEqual <long>(blockSize, target.Size); target.Dispose(); }
public void IsDisposedTest() { MemorySlab slab = new MemorySlab(blockSize * 3, null); ManagedBuffer target = GetNewBuffer(slab); Assert.IsFalse(target.IsDisposed); target.Dispose(); Assert.IsTrue(target.IsDisposed); }
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(); }
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(); } }
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(); }