Exemplo n.º 1
0
        public void MessageReceived_RaisedForMultipleMessagesWithBlockingStream()
        {
            var json = "{\"RequestId\":\"de08f561-50c1-4816-adc3-73d2c283d8cf\",\"Type\":\"Request\",\"Method\":\"Handshake\",\"Payload\":{\"ProtocolVersion\":\"3.0.0\",\"MinimumProtocolVersion\":\"1.0.0\"}}\r\n{\"RequestId\":\"e2db1e2d-0282-45c4-9004-b096e221230d\",\"Type\":\"Response\",\"Method\":\"Handshake\",\"Payload\":{\"ResponseCode\":0,\"ProtocolVersion\":\"2.0.0\"}}\r\n";

            using (var receivedEvent = new ManualResetEventSlim(initialState: false))
                using (var cancellationTokenSource = new CancellationTokenSource())
                    using (var stream = new MemoryStream())
                        using (var readWriteSemaphore = new SemaphoreSlim(initialCount: 1, maxCount: 1))
                            using (var dataWrittenEvent = new ManualResetEventSlim(initialState: false))
                                using (var outboundStream = new SimulatedWriteOnlyFileStream(stream, readWriteSemaphore, dataWrittenEvent, cancellationTokenSource.Token))
                                    using (var inboundStream = new SimulatedReadOnlyFileStream(stream, readWriteSemaphore, dataWrittenEvent, cancellationTokenSource.Token))
                                        using (var reader = new SimulatedStreamReader(inboundStream))
                                            using (var receiver = new StandardInputReceiver(reader))
                                            {
                                                var messages = new List <Message>();

                                                receiver.MessageReceived += (object sender, MessageEventArgs e) =>
                                                {
                                                    messages.Add(e.Message);

                                                    if (messages.Count == 2)
                                                    {
                                                        receivedEvent.Set();
                                                    }
                                                };

                                                var bytes = Encoding.UTF8.GetBytes(json);

                                                outboundStream.Write(bytes, offset: 0, count: bytes.Length);

                                                receiver.Connect();

                                                receivedEvent.Wait();
                                            }
        }
Exemplo n.º 2
0
        public void MessageReceived_RaisedForSingleMessageWithBlockingStream(string json, string requestId, MessageType type, MessageMethod method, string payload)
        {
            using (var receivedEvent = new ManualResetEventSlim(initialState: false))
                using (var cancellationTokenSource = new CancellationTokenSource())
                    using (var stream = new MemoryStream())
                        using (var readWriteSemaphore = new SemaphoreSlim(initialCount: 1, maxCount: 1))
                            using (var dataWrittenEvent = new ManualResetEventSlim(initialState: false))
                                using (var outboundStream = new SimulatedWriteOnlyFileStream(stream, readWriteSemaphore, dataWrittenEvent, cancellationTokenSource.Token))
                                    using (var inboundStream = new SimulatedReadOnlyFileStream(stream, readWriteSemaphore, dataWrittenEvent, cancellationTokenSource.Token))
                                        using (var reader = new SimulatedStreamReader(inboundStream))
                                            using (var receiver = new StandardInputReceiver(reader))
                                            {
                                                Message message = null;

                                                receiver.MessageReceived += (object sender, MessageEventArgs e) =>
                                                {
                                                    message = e.Message;

                                                    receivedEvent.Set();
                                                };

                                                var bytes = Encoding.UTF8.GetBytes(json);

                                                outboundStream.Write(bytes, offset: 0, count: bytes.Length);

                                                receiver.Connect();

                                                receivedEvent.Wait();

                                                Assert.Equal(requestId, message.RequestId);
                                                Assert.Equal(type, message.Type);
                                                Assert.Equal(method, message.Method);
                                                Assert.Equal(payload, message.Payload?.ToString(Formatting.None));
                                            }
        }
Exemplo n.º 3
0
        public void MessageReceived_RaisedForSingleMessageInChunksWithBlockingStream()
        {
            var json      = "{\"RequestId\":\"a\",\"Type\":\"Progress\",\"Method\":\"None\",\"Payload\":{\"d\":\"e\"}}\r\n";
            var requestId = "a";
            var type      = MessageType.Progress;
            var method    = MessageMethod.None;
            var payload   = "{\"d\":\"e\"}";

            using (var receivedEvent = new ManualResetEventSlim(initialState: false))
                using (var cancellationTokenSource = new CancellationTokenSource())
                    using (var stream = new MemoryStream())
                        using (var readWriteSemaphore = new SemaphoreSlim(initialCount: 1, maxCount: 1))
                            using (var dataWrittenEvent = new ManualResetEventSlim(initialState: false))
                                using (var outboundStream = new SimulatedWriteOnlyFileStream(stream, readWriteSemaphore, dataWrittenEvent, cancellationTokenSource.Token))
                                    using (var inboundStream = new SimulatedReadOnlyFileStream(stream, readWriteSemaphore, dataWrittenEvent, cancellationTokenSource.Token))
                                        using (var reader = new SimulatedStreamReader(inboundStream))
                                            using (var receiver = new StandardInputReceiver(reader))
                                            {
                                                Message message = null;

                                                receiver.MessageReceived += (object sender, MessageEventArgs e) =>
                                                {
                                                    message = e.Message;

                                                    receivedEvent.Set();
                                                };

                                                var bytes = Encoding.UTF8.GetBytes(json);

                                                for (var offset = 0; offset < bytes.Length; offset += 10)
                                                {
                                                    var count = Math.Min(bytes.Length - offset, 10);

                                                    outboundStream.Write(bytes, offset, count);
                                                }

                                                receiver.Connect();

                                                receivedEvent.Wait();

                                                Assert.Equal(requestId, message.RequestId);
                                                Assert.Equal(type, message.Type);
                                                Assert.Equal(method, message.Method);
                                                Assert.Equal(payload, message.Payload.ToString(Formatting.None));
                                            }
        }