public void TestSuccesfulConnection()
        {
            // setup
            Uri url = new Uri("http://test.com");
            CancellationTokenSource cts = new CancellationTokenSource();
            List<EventSourceState> states = new List<EventSourceState>();
            ServiceResponseMock response = new ServiceResponseMock(url, System.Net.HttpStatusCode.OK);
            WebRequesterFactoryMock factory = new WebRequesterFactoryMock(response);
            ManualResetEvent stateIsOpen = new ManualResetEvent(false);

            TestableEventSource es = new TestableEventSource(url, factory);
            es.StateChanged += (o, e) =>
            {
                states.Add(e.State);
                if (e.State == EventSourceState.OPEN)
                {
                    stateIsOpen.Set();
                    cts.Cancel();
                }
            };


            // act
            stateIsOpen.Reset();

            es.Start(cts.Token);

            stateIsOpen.WaitOne();

            // assert
            Assert.IsTrue(states.Count == 2);
            Assert.AreEqual(states[0], EventSourceState.CONNECTING);
            Assert.AreEqual(states[1], EventSourceState.OPEN);
        }
Пример #2
0
        private TestableEventSource SetupAndConnect()
        {
            // setup
            cts = new CancellationTokenSource();
            states = new List<EventSourceState>();
            response = new ServiceResponseMock(url, System.Net.HttpStatusCode.OK);
            factory = new WebRequesterFactoryMock(response);
            stateIsOpen = new ManualResetEvent(false);
            receivedMessages = new List<ServerSentEvent>();
            eventReceived = new ManualResetEvent(false);

            TestableEventSource es = new TestableEventSource(url, factory);
            es.StateChanged += (o, e) =>
            {
                states.Add(e.State);
                if (e.State == EventSourceState.OPEN)
                    stateIsOpen.Set();
            };
            es.EventReceived += (o, e) =>
            {
                receivedMessages.Add(e.Message);
                eventReceived.Set();
            };

            return es;
        }
Пример #3
0
        private TestableEventSource SetupAndConnect()
        {
            // setup
            cts              = new CancellationTokenSource();
            states           = new List <EventSourceState>();
            response         = new ServiceResponseMock(url, System.Net.HttpStatusCode.OK);
            factory          = new WebRequesterFactoryMock(response);
            stateIsOpen      = new ManualResetEvent(false);
            receivedMessages = new List <ServerSentEvent>();
            eventReceived    = new ManualResetEvent(false);

            TestableEventSource es = new TestableEventSource(url, factory);

            es.StateChanged += (o, e) =>
            {
                states.Add(e.State);
                if (e.State == EventSourceState.OPEN)
                {
                    stateIsOpen.Set();
                }
            };
            es.EventReceived += (o, e) =>
            {
                receivedMessages.Add(e.Message);
                eventReceived.Set();
            };

            return(es);
        }
        public void TestSuccesfulConnection()
        {
            // setup
            Uri url = new Uri("http://test.com");
            CancellationTokenSource cts         = new CancellationTokenSource();
            List <EventSourceState> states      = new List <EventSourceState>();
            ServiceResponseMock     response    = new ServiceResponseMock(url, System.Net.HttpStatusCode.OK);
            WebRequesterFactoryMock factory     = new WebRequesterFactoryMock(response);
            ManualResetEvent        stateIsOpen = new ManualResetEvent(false);

            TestableEventSource es = new TestableEventSource(url, factory);

            es.StateChanged += (o, e) =>
            {
                states.Add(e.State);
                if (e.State == EventSourceState.OPEN)
                {
                    stateIsOpen.Set();
                    cts.Cancel();
                }
            };


            // act
            stateIsOpen.Reset();

            es.Start(cts.Token);

            stateIsOpen.WaitOne();

            // assert
            Assert.IsTrue(states.Count == 2);
            Assert.AreEqual(states[0], EventSourceState.CONNECTING);
            Assert.AreEqual(states[1], EventSourceState.OPEN);
        }
Пример #5
0
        public void TestReConnectionAfterConnectionLost()
        {
            // setup
            Uri url = new Uri("http://test.com");
            CancellationTokenSource cts    = new CancellationTokenSource();
            List <EventSourceState> states = new List <EventSourceState>();
            ServiceResponseMock     serviceResponseMock = new ServiceResponseMock(url, System.Net.HttpStatusCode.OK);
            WebRequesterFactoryMock factory             = new WebRequesterFactoryMock(serviceResponseMock);
            ManualResetEvent        stateIsOpen         = new ManualResetEvent(false);
            ManualResetEvent        stateIsClosed       = new ManualResetEvent(false);

            TestableEventSource es = new TestableEventSource(url, factory);

            es.StateChanged += (o, e) =>
            {
                states.Add(e.State);
                if (e.State == EventSourceState.OPEN)
                {
                    stateIsClosed.Reset();
                    stateIsOpen.Set();
                }
                else if (e.State == EventSourceState.CLOSED)
                {
                    stateIsOpen.Reset();
                    stateIsClosed.Set();
                }
            };


            // act
            stateIsOpen.Reset();

            es.Start(cts.Token);

            stateIsOpen.WaitOne();
            states.Clear();

            serviceResponseMock.DistantConnectionClose();

            stateIsClosed.WaitOrThrow();
            stateIsOpen.WaitOrThrow();

            // assert
            Assert.AreEqual(states[0], EventSourceState.CLOSED);
            Assert.AreEqual(states[1], EventSourceState.CONNECTING);
            Assert.AreEqual(states[2], EventSourceState.OPEN);
        }
Пример #6
0
        public void TestReConnectionAfterConnectionLost()
        {
            // setup
            Uri url = new Uri("http://test.com");
            CancellationTokenSource cts = new CancellationTokenSource();
            List<EventSourceState> states = new List<EventSourceState>();
            ServiceResponseMock serviceResponseMock = new ServiceResponseMock(url, System.Net.HttpStatusCode.OK);
            WebRequesterFactoryMock factory = new WebRequesterFactoryMock(serviceResponseMock);
            ManualResetEvent stateIsOpen = new ManualResetEvent(false);
            ManualResetEvent stateIsClosed = new ManualResetEvent(false);

            TestableEventSource es = new TestableEventSource(url, factory);
            es.StateChanged += (o, e) =>
            {
                states.Add(e.State);
                if (e.State == EventSourceState.OPEN)
                {
                    stateIsClosed.Reset();
                    stateIsOpen.Set();
                }
                else if (e.State == EventSourceState.CLOSED)
                {
                    stateIsOpen.Reset();
                    stateIsClosed.Set();
                }
            };

            // act
            stateIsOpen.Reset();

            es.Start(cts.Token);

            stateIsOpen.WaitOne();
            states.Clear();

            serviceResponseMock.DistantConnectionClose();

            stateIsClosed.WaitOrThrow();
            stateIsOpen.WaitOrThrow();

            // assert
            Assert.AreEqual(states[0], EventSourceState.CLOSED);
            Assert.AreEqual(states[1], EventSourceState.CONNECTING);
            Assert.AreEqual(states[2], EventSourceState.OPEN);
        }
Пример #7
0
        public void TestSuccesfulConnectionWithHeaders()
        {
            // setup
            Uri url = new Uri("http://test.com");
            CancellationTokenSource cts         = new CancellationTokenSource();
            List <EventSourceState> states      = new List <EventSourceState>();
            ServiceResponseMock     response    = new ServiceResponseMock(url, System.Net.HttpStatusCode.OK);
            WebRequesterFactoryMock factory     = new WebRequesterFactoryMock(response);
            ManualResetEvent        stateIsOpen = new ManualResetEvent(false);

            var headers = new Dictionary <string, string>
            {
                { "x-key", "headerValue" }
            };

            TestableEventSource es = new TestableEventSource(url, factory, headers);

            es.StateChanged += (o, e) =>
            {
                states.Add(e.State);
                if (e.State == EventSourceState.OPEN)
                {
                    stateIsOpen.Set();
                    cts.Cancel();
                }
            };


            // act
            stateIsOpen.Reset();

            es.Start(cts.Token);

            stateIsOpen.WaitOne();

            // assert
            Assert.AreEqual(1, factory.WebRequesterMock.Response.Headers.Count);
            Assert.AreEqual("headerValue", factory.WebRequesterMock.Response.Headers["x-key"]);
        }
Пример #8
0
 public WebRequesterMock(ServiceResponseMock response)
 {
     this.Response = response;
 }
Пример #9
0
 public WebRequesterFactoryMock(ServiceResponseMock response)
 {
     this.WebRequesterMock = new WebRequesterMock(response);
 }
Пример #10
0
 public GetIsCalledEventArgs(ServiceResponseMock response)
 {
     ServerResponse = response;
 }
 public WebRequesterMock(ServiceResponseMock response)
 {
     this.Response = response;
 }
 public WebRequesterFactoryMock(ServiceResponseMock response)
 {
     this.WebRequesterMock = new WebRequesterMock(response);
 }
 public GetIsCalledEventArgs(ServiceResponseMock response)
 {
     ServerResponse = response;
 }