コード例 #1
0
        public async Task ConsumeOutputException()
        {
            // Setup: Create a mock message writer
            var mw = new Mock <MessageWriter>(Stream.Null);

            mw.Setup(o => o.WriteMessage(It.IsAny <Message>())).Returns(Task.FromResult(true));

            // If: I start the output consumption thread with a completed blocking collection
            var jh = new JsonRpcHost(GetChannelBase(null, mw.Object).Object);

            jh.outputQueue.CompleteAdding();
            await jh.ConsumeOutput().WithTimeout(TimeSpan.FromSeconds(1));

            // Then: The message writer should not have been called
            mw.Verify(o => o.WriteMessage(It.IsAny <Message>()), Times.Never);
        }
コード例 #2
0
        public async Task ConsumeOutputCancelled()
        {
            // NOTE: This test validates that the blocking collection breaks out when cancellation is requested

            // Setup: Create a mock message writer
            var mw = new Mock <MessageWriter>(Stream.Null);

            mw.Setup(o => o.WriteMessage(It.IsAny <Message>())).Returns(Task.FromResult(true));

            // If:
            // ... I start the output consumption thread
            var  jh = new JsonRpcHost(GetChannelBase(null, mw.Object).Object);
            Task consumeOuputTask = jh.ConsumeOutput();

            // ... and I stop the thread via cancellation and wait for completion
            jh.cancellationTokenSource.Cancel();
            await consumeOuputTask.WithTimeout(TimeSpan.FromSeconds(1));

            // Then: The message writer should not have been called
            mw.Verify(o => o.WriteMessage(It.IsAny <Message>()), Times.Never);
        }
コード例 #3
0
        public async Task ConsumeOutput()
        {
            // Setup:
            // ... Create a mock message writer
            var mw = new Mock <MessageWriter>(Stream.Null);

            mw.Setup(o => o.WriteMessage(CommonObjects.ResponseMessage)).Returns(Task.FromResult(true));

            // ... Create the JSON RPC host and add an item to the output queue
            var jh = new JsonRpcHost(GetChannelBase(null, mw.Object).Object);

            jh.outputQueue.Add(CommonObjects.ResponseMessage);
            jh.outputQueue.CompleteAdding();        // This will cause the thread to stop after processing the items

            // If: I start the output consumption thread
            Task consumeOutputTask = jh.ConsumeOutput();
            await consumeOutputTask.WithTimeout(TimeSpan.FromSeconds(1));

            // Then: The message writer should have been called once
            mw.Verify(o => o.WriteMessage(CommonObjects.ResponseMessage), Times.Once);
        }