public void DisposeThenWrite_ThrowsObjectDisposedException() { var stream = new BlockingMemoryStream(); stream.Dispose(); Assert.Throws <ObjectDisposedException>(() => stream.Write(new byte[1], 0, 1)); }
public void WriteAfterCompleteWriting_ThrowsInvalidOperationException() { using (var stream = new BlockingMemoryStream()) { stream.CompleteWriting(); Assert.Throws <InvalidOperationException>(() => stream.Write(new byte[1], 0, 1)); } }
public void WriteThenGetConsumingEnumerable_StopsBlockingWhenCompleteWriting() { var buffer = new byte[] { 1, 2, 3 }; using (var stream = new BlockingMemoryStream()) { stream.Write(buffer, 0, buffer.Length); var readToListTask = new TaskFactory().StartNew(() => stream.GetConsumingEnumerable().ToList()); Assert.IsFalse(readToListTask.Wait(millisecondsTimeout: 100)); stream.CompleteWriting(); Assert.IsTrue(readToListTask.Wait(millisecondsTimeout: 1000)); CollectionAssert.AreEqual(buffer, readToListTask.Result); } }
public void WriteThenRead_Timeout() { var buffer = new byte[] { 1, 2, 3 }; using (var stream = new BlockingMemoryStream()) { stream.Write(buffer, 0, 3); // If we try to read too much, then it should stop blocking and return once we hit the timout. var readBuffer = new byte[4]; var readTask = new TaskFactory().StartNew(() => stream.Read(readBuffer, offset: 0, count: 4, timeout: TimeSpan.FromMilliseconds(10), cancellationToken: CancellationToken.None) ); Assert.IsTrue(readTask.Wait(millisecondsTimeout: 100)); var readCount = readTask.Result; Assert.AreEqual(3, readCount); CollectionAssert.AreEqual(buffer, readBuffer.Take(3)); } }
public void ReceiveMessage_should_throw_a_FormatException_when_message_is_an_invalid_size( [Values(-1, 48000001)] int length, [Values(false, true)] bool async) { using (var stream = new BlockingMemoryStream()) { var bytes = BitConverter.GetBytes(length); stream.Write(bytes, 0, bytes.Length); stream.Seek(0, SeekOrigin.Begin); var encoderSelector = new ReplyMessageEncoderSelector <BsonDocument>(BsonDocumentSerializer.Instance); Exception exception; if (async) { _mockStreamFactory .Setup(f => f.CreateStreamAsync(_endPoint, CancellationToken.None)) .ReturnsAsync(stream); _subject.OpenAsync(CancellationToken.None).GetAwaiter().GetResult(); exception = Record .Exception(() => _subject.ReceiveMessageAsync(10, encoderSelector, _messageEncoderSettings, CancellationToken.None) .GetAwaiter() .GetResult()); } else { _mockStreamFactory.Setup(f => f.CreateStream(_endPoint, CancellationToken.None)) .Returns(stream); _subject.Open(CancellationToken.None); exception = Record.Exception(() => _subject.ReceiveMessage(10, encoderSelector, _messageEncoderSettings, CancellationToken.None)); } exception.Should().BeOfType <MongoConnectionException>(); var e = exception.InnerException.Should().BeOfType <FormatException>().Subject; e.Message.Should().Be("The size of the message is invalid."); } }
public void Write_TimeoutUntilRead() { var buffer = new byte[] { 1, 2, 3 }; using (var stream = new BlockingMemoryStream(boundedCapacity: 2)) { // Write one byte more than the capacity, should cause the write to block. var writeTask = new TaskFactory().StartNew(() => stream.Write(buffer, 0, 3) ); Assert.IsFalse(writeTask.Wait(millisecondsTimeout: 100)); // Read one byte var readByteTask = new TaskFactory().StartNew(() => stream.ReadByte() ); Assert.IsTrue(readByteTask.Wait(millisecondsTimeout: 1000)); Assert.AreEqual(1, readByteTask.Result); // Now there should be enough buffer space to finish the write task. Assert.IsTrue(writeTask.Wait(millisecondsTimeout: 1000)); } }
public void WriteThenReadToEnd_StopsBlockingWhenCompleteWriting() { var buffer = new byte[] { 1, 2, 3 }; using (var stream = new BlockingMemoryStream()) { stream.Write(buffer, 0, buffer.Length); // We can read a byte without hitting any blocking var readByteTask = new TaskFactory().StartNew(() => stream.ReadByte()); Assert.IsTrue(readByteTask.Wait(millisecondsTimeout: 1000)); Assert.AreEqual(1, readByteTask.Result); // If we try to read to the end, then we should block until we call CompleteWriting var readToEndTask = new TaskFactory().StartNew(() => StreamUtils.ReadToEnd(stream)); Assert.IsFalse(readToEndTask.Wait(millisecondsTimeout: 100)); stream.CompleteWriting(); Assert.IsTrue(readToEndTask.Wait(millisecondsTimeout: 1000)); CollectionAssert.AreEqual(buffer.Skip(1), readToEndTask.Result); } }
public SIPSorceryVT100Server(Customer customer) { m_customer = customer; Username = customer.CustomerUsername; AdminId = customer.AdminId; m_notificationsAddress = Guid.NewGuid().ToString(); try { m_publisher = Dependency.Resolve <ISIPMonitorPublisher>(); m_publisher.NotificationReady += NotificationReady; m_publisher.MonitorEventReady += MonitorEventAvailable; } catch (ApplicationException appExcp) { logger.Debug("Unable to resolve ISIPMonitorPublisher. " + appExcp.Message); } WriteWelcomeMessage(customer); OutStream.Write(Encoding.ASCII.GetBytes(FILTER_COMMAND_PROMPT)); ThreadPool.QueueUserWorkItem(delegate { Listen(); }); }