Ejemplo n.º 1
0
        public async Task When_response_exceeds_read_timeout_then_read_timeout_exception_occurs()
        {
            var commentSent = ":";

            var handler = new StubMessageHandler();

            handler.QueueStringResponse(commentSent);

            TimeSpan readTimeout = TimeSpan.FromSeconds(4);
            TimeSpan timeout     = readTimeout.Add(TimeSpan.FromSeconds(1));

            var evt = new StubEventSource(new Configuration(_uri, handler, readTimeout: readTimeout), (int)timeout.TotalMilliseconds);

            var exceptionMessage = string.Empty;

            try
            {
                evt.Error += (_, e) =>
                {
                    exceptionMessage = e.Exception.Message;
                    evt.Close();
                };

                await evt.StartAsync();
            }
            catch (TaskCanceledException tce) {}

            Assert.Contains(exceptionMessage, Resources.EventSourceService_Read_Timeout);
            Assert.True(evt.ReadyState == ReadyState.Shutdown);
        }
Ejemplo n.º 2
0
        public async Task When_response_does_not_exceed_read_timeout_then_expected_message_event_occurs()
        {
            var sse = "event: put\ndata: this is a test message\n\n";

            var handler = new StubMessageHandler();

            handler.QueueStringResponse(sse);
            handler.QueueResponse(new HttpResponseMessage(HttpStatusCode.NoContent));

            TimeSpan readTimeout = TimeSpan.FromSeconds(4);
            TimeSpan timeout     = readTimeout.Subtract(TimeSpan.FromSeconds(1));

            var evt = new StubEventSource(new Configuration(_uri, handler, readTimeout: readTimeout), (int)timeout.TotalMilliseconds);

            var wasMessageReceivedEventRaised = false;
            var eventName = "message";

            evt.MessageReceived += (_, e) =>
            {
                eventName = e.EventName;
                wasMessageReceivedEventRaised = true;

                evt.Close();
            };

            await evt.StartAsync();

            Assert.Equal("put", eventName);
            Assert.True(wasMessageReceivedEventRaised);
        }
Ejemplo n.º 3
0
        public async Task When_response_exceeds_read_timeout_then_read_timeout_exception_occurs()
        {
            var commentSent = ":";

            var handler = new StubMessageHandler();

            handler.QueueStringResponse(commentSent);

            TimeSpan readTimeout = TimeSpan.FromSeconds(4);
            TimeSpan timeout     = readTimeout.Add(TimeSpan.FromSeconds(1));

            var evt = new StubEventSource(new Configuration(_uri, handler, readTimeout: readTimeout), (int)timeout.TotalMilliseconds);

            var receiver = new ErrorReceiver(evt);

            receiver.CloseEventSourceOnError = true;

            await evt.StartAsync();

            Assert.NotNull(receiver.ErrorReceived);
            Assert.Contains(receiver.ErrorReceived.Message, Resources.EventSourceService_Read_Timeout);
            Assert.Equal(ReadyState.Closed, receiver.SourceStateReceived);
            Assert.Equal(ReadyState.Shutdown, evt.ReadyState);
        }