示例#1
0
        public void GoodPipe_WritesDataAndRaisesStateHealthy()
        {
            const string inputMessage = "FooBar";

            byte[] expectedData =
            {
                0x46, 0x6F, 0x6F, 0x42, 0x61, 0x72, (byte)'\n', // "FooBar\n"
                0x46, 0x6F, 0x6F, 0x42, 0x61, 0x72, (byte)'\n', // "FooBar\n"
                0x46, 0x6F, 0x6F, 0x42, 0x61, 0x72, (byte)'\n', // "FooBar\n"
            };

            string pipeName = Guid.NewGuid().ToString("N");

            // Capture event invocations
            Mock <IQueuedPipeStringWriterEventSink> eventSink = new Mock <IQueuedPipeStringWriterEventSink>();

            QueuedPipeStringWriter writer = new QueuedPipeStringWriter(
                () => new NamedPipeClientStream(".", pipeName, PipeDirection.Out),
                eventSink.Object);

            using (TestPipeReaderWorker pipeWorker = new TestPipeReaderWorker(pipeName, PipeTransmissionMode.Byte))
            {
                // Start the pipe reader worker first and wait until the pipe server has been stood-up
                // before starting the pipe writer/enqueuing messages because the writer does not wait
                // for the pipe to be ready to accept (it returns and drops messages immediately).
                pipeWorker.Start();
                pipeWorker.WaitForReadyToAccept();
                writer.Start();

                // Try to write some dummy data
                bool queueOk1 = writer.TryEnqueue(inputMessage);
                bool queueOk2 = writer.TryEnqueue(inputMessage);
                bool queueOk3 = writer.TryEnqueue(inputMessage);

                // Wait until we've received all the sent messages before shuting down
                // the pipe worker thread.
                pipeWorker.WaitForRecievedBytes(count: expectedData.Length);
                pipeWorker.Stop();
                writer.Stop();

                queueOk1.ShouldBeTrue();
                queueOk2.ShouldBeTrue();
                queueOk3.ShouldBeTrue();

                byte[] actualData = pipeWorker.GetReceivedDataSnapshot();
                CollectionAssert.AreEqual(expectedData, actualData);

                // Should only receive one 'healthy' state change per successfully written message
                eventSink.Verify(
                    x => x.OnStateChanged(writer, QueuedPipeStringWriterState.Healthy, It.IsAny <Exception>()),
                    Times.Once);
                eventSink.Verify(
                    x => x.OnStateChanged(writer, QueuedPipeStringWriterState.Stopped, It.IsAny <Exception>()),
                    Times.Once);
            }
        }
示例#2
0
        public void Stop_RaisesStateStopped()
        {
            // Capture event invocations
            Mock <IQueuedPipeStringWriterEventSink> eventSink = new Mock <IQueuedPipeStringWriterEventSink>();

            // createPipeFunc returns `null` since the test will never enqueue any messaged to write
            QueuedPipeStringWriter writer = new QueuedPipeStringWriter(
                () => null,
                eventSink.Object);

            // Try to write some dummy data
            writer.Start();
            writer.Stop();

            eventSink.Verify(
                x => x.OnStateChanged(writer, QueuedPipeStringWriterState.Stopped, null),
                Times.Once);
        }