Exemplo n.º 1
0
            public void SendToAllButCaller()
            {
                using (var host = new MemoryHost())
                {
                    host.MapConnection <FilteredConnection>("/filter");
                    var connection1 = new Client.Connection("http://foo/filter");
                    var connection2 = new Client.Connection("http://foo/filter");

                    var wh1 = new ManualResetEventSlim(initialState: false);
                    var wh2 = new ManualResetEventSlim(initialState: false);

                    connection1.Received += data => wh1.Set();
                    connection2.Received += data => wh2.Set();

                    connection1.Start(host).Wait();
                    connection2.Start(host).Wait();

                    connection1.Send("test").Wait();

                    Assert.False(wh1.WaitHandle.WaitOne(TimeSpan.FromSeconds(5)));
                    Assert.True(wh2.WaitHandle.WaitOne(TimeSpan.FromSeconds(5)));

                    connection1.Stop();
                    connection2.Stop();
                }
            }
Exemplo n.º 2
0
            public void SendToAllButCaller(HostType hostType, TransportType transportType)
            {
                using (var host = CreateHost(hostType, transportType))
                {
                    host.Initialize();

                    var connection1 = new Client.Connection(host.Url + "/filter");
                    var connection2 = new Client.Connection(host.Url + "/filter");

                    var wh1 = new ManualResetEventSlim(initialState: false);
                    var wh2 = new ManualResetEventSlim(initialState: false);

                    connection1.Received += data => wh1.Set();
                    connection2.Received += data => wh2.Set();

                    connection1.Start(host.Transport).Wait();
                    connection2.Start(host.Transport).Wait();

                    connection1.SendWithTimeout("test");

                    Assert.False(wh1.WaitHandle.WaitOne(TimeSpan.FromSeconds(5)));
                    Assert.True(wh2.WaitHandle.WaitOne(TimeSpan.FromSeconds(5)));

                    connection1.Stop();
                    connection2.Stop();
                }
            }
Exemplo n.º 3
0
            public void SendCanBeCalledAfterStateChangedEvent(HostType hostType, TransportType transportType)
            {
                using (var host = CreateHost(hostType, transportType))
                {
                    host.Initialize();

                    var connection = new Client.Connection(host.Url + "/multisend");
                    var results    = new List <string>();
                    connection.Received += data =>
                    {
                        results.Add(data);
                    };

                    connection.StateChanged += stateChange =>
                    {
                        if (stateChange.NewState == Client.ConnectionState.Connected)
                        {
                            connection.SendWithTimeout("");
                        }
                    };

                    connection.Start(host.Transport).Wait();

                    Thread.Sleep(TimeSpan.FromSeconds(5));

                    connection.Stop();

                    Debug.WriteLine(String.Join(", ", results));

                    Assert.Equal(4, results.Count);
                }
            }
Exemplo n.º 4
0
            public void GroupsReceiveMessages(HostType hostType, TransportType transportType)
            {
                using (var host = CreateHost(hostType, transportType))
                {
                    host.Initialize();

                    var connection = new Client.Connection(host.Url + "/groups");
                    var list       = new List <string>();
                    connection.Received += data =>
                    {
                        list.Add(data);
                    };

                    connection.Start(host.Transport).Wait();

                    // Join the group
                    connection.SendWithTimeout(new { type = 1, group = "test" });

                    // Sent a message
                    connection.SendWithTimeout(new { type = 3, group = "test", message = "hello to group test" });

                    // Leave the group
                    connection.SendWithTimeout(new { type = 2, group = "test" });

                    // Send a message
                    connection.SendWithTimeout(new { type = 3, group = "test", message = "goodbye to group test" });

                    Thread.Sleep(TimeSpan.FromSeconds(5));

                    connection.Stop();

                    Assert.Equal(1, list.Count);
                    Assert.Equal("hello to group test", list[0]);
                }
            }
Exemplo n.º 5
0
            public void SendRaisesOnReceivedFromAllEvents()
            {
                using (var host = new MemoryHost())
                {
                    host.MapConnection <MySendingConnection>("/multisend");

                    var connection = new Client.Connection("http://foo/multisend");
                    var results    = new List <string>();
                    connection.Received += data =>
                    {
                        results.Add(data);
                    };

                    connection.Start(host).Wait();
                    connection.Send("").Wait();

                    Thread.Sleep(TimeSpan.FromSeconds(5));

                    connection.Stop();

                    Debug.WriteLine(String.Join(", ", results));

                    Assert.Equal(4, results.Count);
                    Assert.Equal("OnConnectedAsync1", results[0]);
                    Assert.Equal("OnConnectedAsync2", results[1]);
                    Assert.Equal("OnReceivedAsync1", results[2]);
                    Assert.Equal("OnReceivedAsync2", results[3]);
                }
            }
Exemplo n.º 6
0
            // [InlineData(HostType.IIS, TransportType.LongPolling)]
            public void ThrownWebExceptionShouldBeUnwrapped(HostType hostType, TransportType transportType)
            {
                using (var host = CreateHost(hostType, transportType))
                {
                    host.Initialize();

                    var connection = new Client.Connection(host.Url + "/ErrorsAreFun");

                    // Expecting 404
                    var aggEx = Assert.Throws <AggregateException>(() => connection.Start(host.Transport).Wait());

                    connection.Stop();

                    using (var ser = aggEx.GetError())
                    {
                        if (hostType == HostType.IISExpress)
                        {
                            Assert.Equal(System.Net.HttpStatusCode.InternalServerError, ser.StatusCode);
                        }
                        else
                        {
                            Assert.Equal(System.Net.HttpStatusCode.NotFound, ser.StatusCode);
                        }

                        Assert.NotNull(ser.ResponseBody);
                        Assert.NotNull(ser.Exception);
                    }
                }
            }
Exemplo n.º 7
0
            public void SendRaisesOnReceivedFromAllEvents(HostType hostType, TransportType transportType)
            {
                using (var host = CreateHost(hostType, transportType))
                {
                    host.Initialize();

                    var connection = new Client.Connection(host.Url + "/multisend");
                    var results    = new List <string>();
                    connection.Received += data =>
                    {
                        results.Add(data);
                    };

                    connection.Start(host.Transport).Wait();
                    connection.SendWithTimeout("");

                    Thread.Sleep(TimeSpan.FromSeconds(5));

                    connection.Stop();

                    Debug.WriteLine(String.Join(", ", results));

                    Assert.Equal(4, results.Count);
                    Assert.Equal("OnConnectedAsync1", results[0]);
                    Assert.Equal("OnConnectedAsync2", results[1]);
                    Assert.Equal("OnReceivedAsync1", results[2]);
                    Assert.Equal("OnReceivedAsync2", results[3]);
                }
            }
Exemplo n.º 8
0
            public void GroupsReceiveMessages(HostType hostType, TransportType transportType)
            {
                using (var host = CreateHost(hostType, transportType))
                {
                    host.Initialize();

                    var connection = new Client.Connection(host.Url + "/groups");
                    var list = new List<string>();
                    connection.Received += data =>
                    {
                        list.Add(data);
                    };

                    connection.Start(host.Transport).Wait();

                    // Join the group
                    connection.SendWithTimeout(new { type = 1, group = "test" });

                    // Sent a message
                    connection.SendWithTimeout(new { type = 3, group = "test", message = "hello to group test" });

                    // Leave the group
                    connection.SendWithTimeout(new { type = 2, group = "test" });

                    // Send a message
                    connection.SendWithTimeout(new { type = 3, group = "test", message = "goodbye to group test" });

                    Thread.Sleep(TimeSpan.FromSeconds(5));

                    connection.Stop();

                    Assert.Equal(1, list.Count);
                    Assert.Equal("hello to group test", list[0]);
                }
            }
Exemplo n.º 9
0
            public void ReconnectDoesntFireAfterTimeOut(TransportType transportType, MessageBusType messageBusType)
            {
                using (var host = new MemoryHost())
                {
                    var conn = new MyReconnect();
                    host.Configure(app =>
                    {
                        var config = new ConnectionConfiguration
                        {
                            Resolver = new DefaultDependencyResolver()
                        };

                        UseMessageBus(messageBusType, config.Resolver);

                        app.MapSignalR <MyReconnect>("/endpoint", config);
                        var configuration = config.Resolver.Resolve <IConfigurationManager>();
                        configuration.DisconnectTimeout = TimeSpan.FromSeconds(6);
                        configuration.ConnectionTimeout = TimeSpan.FromSeconds(2);
                        configuration.KeepAlive         = null;

                        config.Resolver.Register(typeof(MyReconnect), () => conn);
                    });

                    var connection = new Client.Connection("http://foo/endpoint");
                    var transport  = CreateTransport(transportType, host);
                    connection.Start(transport).Wait();

                    Thread.Sleep(TimeSpan.FromSeconds(5));

                    connection.Stop();

                    Assert.Equal(0, conn.Reconnects);
                }
            }
Exemplo n.º 10
0
            public void ThrownWebExceptionShouldBeUnwrapped(HostType hostType, TransportType transportType)
            {
                using (var host = CreateHost(hostType, transportType))
                {
                    host.Initialize();

                    var connection = new Client.Connection(host.Url + "/ErrorsAreFun");

                    // Expecting 404
                    var aggEx = Assert.Throws<AggregateException>(() => connection.Start(host.Transport).Wait());

                    connection.Stop();

                    using (var ser = aggEx.GetError())
                    {
                        if (hostType == HostType.IISExpress)
                        {
                            Assert.Equal(System.Net.HttpStatusCode.InternalServerError, ser.StatusCode);
                        }
                        else
                        {
                            Assert.Equal(System.Net.HttpStatusCode.NotFound, ser.StatusCode);
                        }

                        Assert.NotNull(ser.ResponseBody);
                        Assert.NotNull(ser.Exception);
                    }
                }
            }
Exemplo n.º 11
0
            public void GroupsReceiveMessages()
            {
                using (var host = new MemoryHost())
                {
                    host.MapConnection <MyGroupConnection>("/groups");

                    var connection = new Client.Connection("http://foo/groups");
                    var list       = new List <string>();
                    connection.Received += data =>
                    {
                        list.Add(data);
                    };

                    connection.Start(host).Wait();

                    // Join the group
                    connection.Send(new { type = 1, group = "test" }).Wait();

                    // Sent a message
                    connection.Send(new { type = 3, group = "test", message = "hello to group test" }).Wait();

                    // Leave the group
                    connection.Send(new { type = 2, group = "test" }).Wait();

                    // Send a message
                    connection.Send(new { type = 3, group = "test", message = "goodbye to group test" }).Wait();

                    Thread.Sleep(TimeSpan.FromSeconds(5));

                    connection.Stop();

                    Assert.Equal(1, list.Count);
                    Assert.Equal("hello to group test", list[0]);
                }
            }
Exemplo n.º 12
0
            public void SendCanBeCalledAfterStateChangedEvent()
            {
                using (var host = new MemoryHost())
                {
                    host.MapConnection <MySendingConnection>("/multisend");

                    var connection = new Client.Connection("http://foo/multisend");
                    var results    = new List <string>();
                    connection.Received += data =>
                    {
                        results.Add(data);
                    };

                    connection.StateChanged += stateChange =>
                    {
                        if (stateChange.NewState == Client.ConnectionState.Connected)
                        {
                            connection.Send("").Wait();
                        }
                    };

                    connection.Start(host).Wait();

                    Thread.Sleep(TimeSpan.FromSeconds(5));

                    connection.Stop();

                    Debug.WriteLine(String.Join(", ", results));

                    Assert.Equal(4, results.Count);
                }
            }
Exemplo n.º 13
0
        public static IDisposable ClientGroupsSyncWithServerGroupsOnReconnectLongPolling()
        {
            var host = new MemoryHost();

            host.Configuration.KeepAlive = null;
            host.Configuration.ConnectionTimeout = TimeSpan.FromSeconds(5);
            host.Configuration.HeartbeatInterval = TimeSpan.FromSeconds(2);
            host.MapConnection<MyRejoinGroupConnection>("/groups");

            var connection = new Client.Connection("http://foo/groups");
            var inGroupOnReconnect = new List<bool>();
            var wh = new ManualResetEventSlim();

            connection.Received += message =>
            {
                Console.WriteLine(message);
                wh.Set();
            };

            connection.Reconnected += () =>
            {
                var inGroup = connection.Groups.Contains(typeof(MyRejoinGroupConnection).FullName + ".test");

                if (!inGroup)
                {
                    Debugger.Break();
                }

                inGroupOnReconnect.Add(inGroup);

                connection.Send(new { type = 3, group = "test", message = "Reconnected" }).Wait();
            };

            connection.Start(new Client.Transports.LongPollingTransport(host)).Wait();

            // Join the group
            connection.Send(new { type = 1, group = "test" }).Wait();

            Thread.Sleep(TimeSpan.FromSeconds(10));

            if (!wh.Wait(TimeSpan.FromSeconds(10)))
            {
                Debugger.Break();
            }

            Console.WriteLine(inGroupOnReconnect.Count > 0);
            Console.WriteLine(String.Join(", ", inGroupOnReconnect.Select(b => b.ToString())));

            connection.Stop();

            return host;
        }
Exemplo n.º 14
0
        public static IDisposable ClientGroupsSyncWithServerGroupsOnReconnectLongPolling()
        {
            var host = new MemoryHost();

            host.Configure(app =>
            {
                var config = new ConnectionConfiguration()
                {
                    Resolver = new DefaultDependencyResolver()
                };

                app.MapConnection <MyRejoinGroupConnection>("/groups", config);

                var configuration               = config.Resolver.Resolve <IConfigurationManager>();
                configuration.KeepAlive         = null;
                configuration.ConnectionTimeout = TimeSpan.FromSeconds(1);
            });

            var connection         = new Client.Connection("http://foo/groups");
            var inGroupOnReconnect = new List <bool>();
            var wh = new ManualResetEventSlim();

            connection.Received += message =>
            {
                Console.WriteLine(message);
                wh.Set();
            };

            connection.Reconnected += () =>
            {
                connection.Send(new { type = 3, group = "test", message = "Reconnected" }).Wait();
            };

            connection.Start(new Client.Transports.LongPollingTransport(host)).Wait();

            // Join the group
            connection.Send(new { type = 1, group = "test" }).Wait();

            Thread.Sleep(TimeSpan.FromSeconds(10));

            if (!wh.Wait(TimeSpan.FromSeconds(10)))
            {
                Debugger.Break();
            }

            Console.WriteLine(inGroupOnReconnect.Count > 0);
            Console.WriteLine(String.Join(", ", inGroupOnReconnect.Select(b => b.ToString())));

            connection.Stop();

            return(host);
        }
Exemplo n.º 15
0
        public static IDisposable ClientGroupsSyncWithServerGroupsOnReconnectLongPolling()
        {
            var host = new MemoryHost();

            host.Configuration.KeepAlive         = null;
            host.Configuration.ConnectionTimeout = TimeSpan.FromSeconds(5);
            host.Configuration.HeartbeatInterval = TimeSpan.FromSeconds(2);
            host.MapConnection <MyRejoinGroupConnection>("/groups");

            var connection         = new Client.Connection("http://foo/groups");
            var inGroupOnReconnect = new List <bool>();
            var wh = new ManualResetEventSlim();

            connection.Received += message =>
            {
                Console.WriteLine(message);
                wh.Set();
            };

            connection.Reconnected += () =>
            {
                var inGroup = connection.Groups.Contains(typeof(MyRejoinGroupConnection).FullName + ".test");

                if (!inGroup)
                {
                    Debugger.Break();
                }

                inGroupOnReconnect.Add(inGroup);

                connection.Send(new { type = 3, group = "test", message = "Reconnected" }).Wait();
            };

            connection.Start(new Client.Transports.LongPollingTransport(host)).Wait();

            // Join the group
            connection.Send(new { type = 1, group = "test" }).Wait();

            Thread.Sleep(TimeSpan.FromSeconds(10));

            if (!wh.Wait(TimeSpan.FromSeconds(10)))
            {
                Debugger.Break();
            }

            Console.WriteLine(inGroupOnReconnect.Count > 0);
            Console.WriteLine(String.Join(", ", inGroupOnReconnect.Select(b => b.ToString())));

            connection.Stop();

            return(host);
        }
Exemplo n.º 16
0
            public void SendWithSyncErrorThrows(HostType hostType, TransportType transportType)
            {
                using (var host = CreateHost(hostType, transportType))
                {
                    host.Initialize();

                    var connection = new Client.Connection(host.Url + "/sync-error");

                    connection.Start(host.Transport).Wait();

                    Assert.Throws <AggregateException>(() => connection.SendWithTimeout("test"));

                    connection.Stop();
                }
            }
Exemplo n.º 17
0
            public void EnvironmentIsAvailable(HostType hostType, TransportType transportType)
            {
                using (var host = CreateHost(hostType, transportType))
                {
                    host.Initialize();

                    var connection  = new Client.Connection(host.Url + "/items");
                    var connection2 = new Client.Connection(host.Url + "/items");

                    var results = new List <RequestItemsResponse>();
                    connection2.Received += data =>
                    {
                        var val = JsonConvert.DeserializeObject <RequestItemsResponse>(data);
                        if (!results.Contains(val))
                        {
                            results.Add(val);
                        }
                    };

                    connection.Start(host.Transport).Wait();
                    connection2.Start(host.Transport).Wait();

                    Thread.Sleep(TimeSpan.FromSeconds(2));

                    connection.SendWithTimeout(null);

                    Thread.Sleep(TimeSpan.FromSeconds(2));

                    connection.Stop();

                    Thread.Sleep(TimeSpan.FromSeconds(2));

                    Debug.WriteLine(String.Join(", ", results));

                    Assert.Equal(3, results.Count);
                    Assert.Equal("OnConnectedAsync", results[0].Method);
                    Assert.Equal(1, results[0].Keys.Length);
                    Assert.Equal("owin.environment", results[0].Keys[0]);
                    Assert.Equal("OnReceivedAsync", results[1].Method);
                    Assert.Equal(1, results[1].Keys.Length);
                    Assert.Equal("owin.environment", results[1].Keys[0]);
                    Assert.Equal("OnDisconnectAsync", results[2].Method);
                    Assert.Equal(1, results[2].Keys.Length);
                    Assert.Equal("owin.environment", results[2].Keys[0]);

                    connection2.Stop();
                }
            }
Exemplo n.º 18
0
            // [InlineData(HostType.IIS, TransportType.LongPolling)]
            public void ReconnectFiresAfterHostShutDown(HostType hostType, TransportType transportType)
            {
                using (var host = CreateHost(hostType, transportType))
                {
                    host.Initialize();

                    var connection = new Client.Connection(host.Url + "/my-reconnect");
                    connection.Start(host.Transport).Wait();

                    host.Shutdown();

                    Thread.Sleep(TimeSpan.FromSeconds(5));

                    Assert.Equal(Client.ConnectionState.Reconnecting, connection.State);

                    connection.Stop();
                }
            }
Exemplo n.º 19
0
        public void FarmDisconnectOnlyRaisesEventOnce()
        {
            // Each node shares the same bus but are indepenent servers
            var counters             = new SignalR.Infrastructure.PerformanceCounterManager();
            var configurationManager = new DefaultConfigurationManager();

            using (var bus = new MessageBus(new StringMinifier(), new TraceManager(), counters, configurationManager))
            {
                var nodeCount = 3;
                var nodes     = new List <ServerNode>();
                for (int i = 0; i < nodeCount; i++)
                {
                    nodes.Add(new ServerNode(bus));
                }

                var timeout = TimeSpan.FromSeconds(5);
                foreach (var node in nodes)
                {
                    node.Server.Configuration.HeartBeatInterval = timeout;
                    node.Server.Configuration.DisconnectTimeout = TimeSpan.Zero;
                    node.Server.MapConnection <FarmConnection>("/echo");
                }

                var loadBalancer = new LoadBalancer(nodes.Select(f => f.Server).ToArray());
                var transport    = new Client.Transports.LongPollingTransport(loadBalancer);

                var connection = new Client.Connection("http://goo/echo");

                connection.Start(transport).Wait();

                for (int i = 0; i < nodes.Count; i++)
                {
                    nodes[i].Broadcast(String.Format("From Node {0}: {1}", i, i + 1));
                    Thread.Sleep(TimeSpan.FromSeconds(1));
                }

                connection.Stop();

                Thread.Sleep(TimeSpan.FromTicks(timeout.Ticks * nodes.Count));

                Assert.Equal(1, nodes.Sum(n => n.Connection.DisconnectCount));
            }
        }
Exemplo n.º 20
0
            public void ThrownWebExceptionShouldBeUnwrapped()
            {
                var host = new MemoryHost();
                host.MapConnection<MyBadConnection>("/ErrorsAreFun");

                var connection = new Client.Connection("http://test/ErrorsAreFun");

                // Expecting 404
                var aggEx = Assert.Throws<AggregateException>(() => connection.Start(host).Wait());

                connection.Stop();

                using (var ser = aggEx.GetError())
                {
                    Assert.Equal(ser.StatusCode, HttpStatusCode.NotFound);
                    Assert.NotNull(ser.ResponseBody);
                    Assert.NotNull(ser.Exception);
                }
            }
Exemplo n.º 21
0
            public void ReconnectFiresAfterHostShutDown()
            {
                using (var host = new MemoryHost())
                {
                    var conn = new MyReconnect();
                    host.DependencyResolver.Register(typeof(MyReconnect), () => conn);
                    host.MapConnection <MyReconnect>("/endpoint");

                    var connection = new Client.Connection("http://foo/endpoint");
                    connection.Start(host).Wait();

                    host.Dispose();

                    Thread.Sleep(TimeSpan.FromSeconds(5));

                    Assert.Equal(Client.ConnectionState.Reconnecting, connection.State);

                    connection.Stop();
                }
            }
Exemplo n.º 22
0
            public void GroupsAreNotReadOnConnectedAsync(HostType hostType, TransportType transportType)
            {
                using (var host = CreateHost(hostType, transportType))
                {
                    host.Initialize();

                    var connection = new Client.Connection(host.Url + "/group-echo");
                    ((Client.IConnection)connection).Groups.Add(typeof(MyGroupEchoConnection).FullName + ".test");
                    connection.Received += data =>
                    {
                        Assert.False(true, "Unexpectedly received data");
                    };

                    connection.Start(host.Transport).Wait();

                    Thread.Sleep(TimeSpan.FromSeconds(5));

                    connection.Stop();
                }
            }
Exemplo n.º 23
0
            public void ThrownWebExceptionShouldBeUnwrapped()
            {
                var host = new MemoryHost();

                host.MapConnection <MyBadConnection>("/ErrorsAreFun");

                var connection = new Client.Connection("http://test/ErrorsAreFun");

                // Expecting 404
                var aggEx = Assert.Throws <AggregateException>(() => connection.Start(host).Wait());

                connection.Stop();

                using (var ser = aggEx.GetError())
                {
                    Assert.Equal(ser.StatusCode, HttpStatusCode.NotFound);
                    Assert.NotNull(ser.ResponseBody);
                    Assert.NotNull(ser.Exception);
                }
            }
Exemplo n.º 24
0
            public void ClientGroupsSyncWithServerGroupsOnReconnectWhenNotRejoiningGroups(HostType hostType, TransportType transportType)
            {
                using (var host = CreateHost(hostType, transportType))
                {
                    host.Initialize(keepAlive: null,
                                    connectionTimeout: 5,
                                    hearbeatInterval: 2);

                    var connection         = new Client.Connection(host.Url + "/groups");
                    var inGroupOnReconnect = new List <bool>();
                    var wh = new ManualResetEventSlim();

                    connection.Received += message =>
                    {
                        Assert.Equal("Reconnected", message);
                        inGroupOnReconnect.Add(!connection.Groups.Contains(typeof(MyGroupConnection).FullName + ".test"));
                        inGroupOnReconnect.Add(connection.Groups.Contains(typeof(MyGroupConnection).FullName + ".test2"));
                        wh.Set();
                    };

                    connection.Reconnected += () =>
                    {
                        connection.SendWithTimeout(new { type = 1, group = "test2" });
                        connection.SendWithTimeout(new { type = 3, group = "test2", message = "Reconnected" });
                    };

                    connection.Start(host.Transport).Wait();

                    // Join the group
                    connection.SendWithTimeout(new { type = 1, group = "test" });

                    // Force reconnect
                    Thread.Sleep(TimeSpan.FromSeconds(5));

                    Assert.True(wh.Wait(TimeSpan.FromSeconds(5)), "Client didn't receive message sent to test group.");
                    Assert.True(inGroupOnReconnect.Count > 0);
                    Assert.True(inGroupOnReconnect.All(b => b));

                    connection.Stop();
                }
            }
Exemplo n.º 25
0
            public void ReconnectFiresAfterTimeOutLongPolling()
            {
                using (var host = new MemoryHost())
                {
                    var conn = new MyReconnect();
                    host.Configuration.KeepAlive         = null;
                    host.Configuration.ConnectionTimeout = TimeSpan.FromSeconds(5);
                    host.Configuration.HeartBeatInterval = TimeSpan.FromSeconds(5);
                    host.DependencyResolver.Register(typeof(MyReconnect), () => conn);
                    host.MapConnection <MyReconnect>("/endpoint");

                    var connection = new Client.Connection("http://foo/endpoint");
                    connection.Start(new Client.Transports.LongPollingTransport(host)).Wait();

                    Thread.Sleep(TimeSpan.FromSeconds(15));

                    connection.Stop();

                    Assert.InRange(conn.Reconnects, 1, 4);
                }
            }
Exemplo n.º 26
0
            public void ClientGroupsSyncWithServerGroupsOnReconnect(HostType hostType, TransportType transportType)
            {
                using (var host = CreateHost(hostType, transportType))
                {
                    host.Initialize(keepAlive: 0,
                                    connectionTimeout: 5,
                                    hearbeatInterval: 2);

                    var connection = new Client.Connection(host.Url + "/rejoin-groups");
                    var inGroupOnReconnect = new List<bool>();
                    var wh = new ManualResetEventSlim();

                    connection.Received += message =>
                    {
                        Assert.Equal("Reconnected", message);
                        wh.Set();
                    };

                    connection.Reconnected += () =>
                    {
                        inGroupOnReconnect.Add(connection.Groups.Contains(typeof(MyRejoinGroupsConnection).FullName + ".test"));

                        connection.SendWithTimeout(new { type = 3, group = "test", message = "Reconnected" });
                    };

                    connection.Start(host.Transport).Wait();

                    // Join the group
                    connection.SendWithTimeout(new { type = 1, group = "test" });

                    // Force reconnect
                    Thread.Sleep(TimeSpan.FromSeconds(10));

                    Assert.True(wh.Wait(TimeSpan.FromSeconds(5)), "Client didn't receive message sent to test group.");
                    Assert.True(inGroupOnReconnect.Count > 0);
                    Assert.True(inGroupOnReconnect.All(b => b));

                    connection.Stop();
                }
            }
Exemplo n.º 27
0
            public void GroupsAreNotReadOnConnectedAsync()
            {
                using (var host = new MemoryHost())
                {
                    host.MapConnection <MyConnection>("/echo");

                    var connection = new Client.Connection("http://foo/echo");
                    connection.Groups = new List <string> {
                        typeof(MyConnection).FullName + ".test"
                    };
                    connection.Received += data =>
                    {
                        Assert.False(true, "Unexpectedly received data");
                    };

                    connection.Start(host).Wait();

                    Thread.Sleep(TimeSpan.FromSeconds(5));

                    connection.Stop();
                }
            }
Exemplo n.º 28
0
        public void DisconnectFiresForPersistentConnectionWhenClientGoesAway()
        {
            var host = new MemoryHost();
            host.MapConnection<MyConnection>("/echo");
            host.Configuration.DisconnectTimeout = TimeSpan.Zero;
            host.Configuration.HeartBeatInterval = TimeSpan.FromSeconds(5);
            var connectWh = new ManualResetEventSlim();
            var disconnectWh = new ManualResetEventSlim();
            host.DependencyResolver.Register(typeof(MyConnection), () => new MyConnection(connectWh, disconnectWh));
            var connection = new Client.Connection("http://foo/echo");

            // Maximum wait time for disconnect to fire (3 heart beat intervals)
            var disconnectWait = TimeSpan.FromTicks(host.Configuration.HeartBeatInterval.Ticks * 3);

            connection.Start(host).Wait();

            Assert.True(connectWh.Wait(TimeSpan.FromSeconds(10)), "Connect never fired");

            connection.Stop();

            Assert.True(disconnectWh.Wait(disconnectWait), "Disconnect never fired");
        }
Exemplo n.º 29
0
            public void GroupsRejoinedWhenOnRejoiningGroupsOverridden(HostType hostType, TransportType transportType)
            {
                using (var host = CreateHost(hostType, transportType))
                {
                    host.Initialize(keepAlive: null,
                                    connectionTimeout: 2,
                                    hearbeatInterval: 2);

                    var connection = new Client.Connection(host.Url + "/rejoin-groups");

                    var list = new List <string>();
                    connection.Received += data =>
                    {
                        list.Add(data);
                    };

                    connection.Start(host.Transport).Wait();

                    // Join the group
                    connection.SendWithTimeout(new { type = 1, group = "test" });

                    // Sent a message
                    connection.SendWithTimeout(new { type = 3, group = "test", message = "hello to group test" });

                    // Force Reconnect
                    Thread.Sleep(TimeSpan.FromSeconds(5));

                    // Send a message
                    connection.SendWithTimeout(new { type = 3, group = "test", message = "goodbye to group test" });

                    Thread.Sleep(TimeSpan.FromSeconds(5));

                    connection.Stop();

                    Assert.Equal(2, list.Count);
                    Assert.Equal("hello to group test", list[0]);
                    Assert.Equal("goodbye to group test", list[1]);
                }
            }
Exemplo n.º 30
0
            public void GroupsRejoinedWhenOnRejoiningGroupsOverridden()
            {
                using (var host = new MemoryHost())
                {
                    host.Configuration.KeepAlive         = null;
                    host.Configuration.ConnectionTimeout = TimeSpan.FromSeconds(2);
                    host.Configuration.HeartBeatInterval = TimeSpan.FromSeconds(2);
                    host.MapConnection <MyRejoinGroupConnection>("/groups");

                    var connection = new Client.Connection("http://foo/groups");
                    var list       = new List <string>();
                    connection.Received += data =>
                    {
                        list.Add(data);
                    };

                    connection.Start(host).Wait();

                    // Join the group
                    connection.Send(new { type = 1, group = "test" }).Wait();

                    // Sent a message
                    connection.Send(new { type = 3, group = "test", message = "hello to group test" }).Wait();

                    // Force Reconnect
                    Thread.Sleep(TimeSpan.FromSeconds(5));

                    // Send a message
                    connection.Send(new { type = 3, group = "test", message = "goodbye to group test" }).Wait();

                    Thread.Sleep(TimeSpan.FromSeconds(5));

                    connection.Stop();

                    Assert.Equal(2, list.Count);
                    Assert.Equal("hello to group test", list[0]);
                    Assert.Equal("goodbye to group test", list[1]);
                }
            }
Exemplo n.º 31
0
        private static void BroadcastFive(MemoryHost host)
        {
            var connection = new Client.Connection("http://samples/Raw-connection");

            connection.Error += e =>
            {
                Console.Error.WriteLine("========ERROR==========");
                Console.Error.WriteLine(e.GetBaseException().ToString());
                Console.Error.WriteLine("=======================");
            };

            connection.Start(new Client.Transports.ServerSentEventsTransport(host)).Wait();

            try
            {
                for (int i = 0; i < 5; i++)
                {
                    var payload = new
                    {
                        type  = MessageType.Broadcast,
                        value = "message " + i.ToString()
                    };

                    connection.Send(payload).Wait();
                }
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine("========ERROR==========");
                Console.Error.WriteLine(ex.GetBaseException().ToString());
                Console.Error.WriteLine("=======================");
            }
            finally
            {
                connection.Stop();
            }
        }
Exemplo n.º 32
0
        public void DisconnectFiresForPersistentConnectionWhenClientGoesAway()
        {
            using (var host = new MemoryHost())
            {
                host.MapConnection <MyConnection>("/echo");
                host.Configuration.DisconnectTimeout = TimeSpan.Zero;
                host.Configuration.HeartBeatInterval = TimeSpan.FromSeconds(5);
                var connectWh    = new ManualResetEventSlim();
                var disconnectWh = new ManualResetEventSlim();
                host.DependencyResolver.Register(typeof(MyConnection), () => new MyConnection(connectWh, disconnectWh));
                var connection = new Client.Connection("http://foo/echo");

                // Maximum wait time for disconnect to fire (3 heart beat intervals)
                var disconnectWait = TimeSpan.FromTicks(host.Configuration.HeartBeatInterval.Ticks * 3);

                connection.Start(host).Wait();

                Assert.True(connectWh.Wait(TimeSpan.FromSeconds(10)), "Connect never fired");

                connection.Stop();

                Assert.True(disconnectWh.Wait(disconnectWait), "Disconnect never fired");
            }
        }
Exemplo n.º 33
0
        public async Task DisconnectFiresForPersistentConnectionWhenClientCallsStop()
        {
            using (var host = new MemoryHost())
            {
                var connectWh     = new TaskCompletionSource <object>();
                var disconnectWh  = new TaskCompletionSource <object>();
                var dr            = new DefaultDependencyResolver();
                var configuration = dr.Resolve <IConfigurationManager>();

                host.Configure(app =>
                {
                    var config = new ConnectionConfiguration
                    {
                        Resolver = dr
                    };

                    app.MapSignalR <MyConnection>("/echo", config);

                    configuration.DisconnectTimeout = TimeSpan.FromSeconds(6);

                    dr.Register(typeof(MyConnection), () => new MyConnection(connectWh, disconnectWh));
                });
                var connection = new Client.Connection("http://foo/echo");

                // Maximum wait time for disconnect to fire (3 heart beat intervals)
                var disconnectWait = TimeSpan.FromTicks(configuration.HeartbeatInterval().Ticks * 3);

                await connection.Start(host);

                await connectWh.Task.OrTimeout(TimeSpan.FromSeconds(10));

                connection.Stop();

                await disconnectWh.Task.OrTimeout(disconnectWait);
            }
        }
Exemplo n.º 34
0
        public void DisconnectFiresForPersistentConnectionWhenClientGoesAway()
        {
            using (var host = new MemoryHost())
            {
                var connectWh     = new ManualResetEventSlim();
                var disconnectWh  = new ManualResetEventSlim();
                var dr            = new DefaultDependencyResolver();
                var configuration = dr.Resolve <IConfigurationManager>();

                host.Configure(app =>
                {
                    var config = new ConnectionConfiguration
                    {
                        Resolver = dr
                    };

                    app.MapSignalR <MyConnection>("/echo", config);

                    configuration.DisconnectTimeout = TimeSpan.FromSeconds(6);

                    dr.Register(typeof(MyConnection), () => new MyConnection(connectWh, disconnectWh));
                });
                var connection = new Client.Connection("http://foo/echo");

                // Maximum wait time for disconnect to fire (3 heart beat intervals)
                var disconnectWait = TimeSpan.FromTicks(configuration.HeartbeatInterval().Ticks * 3);

                connection.Start(host).Wait();

                Assert.True(connectWh.Wait(TimeSpan.FromSeconds(10)), "Connect never fired");

                connection.Stop();

                Assert.True(disconnectWh.Wait(disconnectWait), "Disconnect never fired");
            }
        }
Exemplo n.º 35
0
            public void SendToGroupFromOutsideOfConnection()
            {
                using (var host = new MemoryHost())
                {
                    IPersistentConnectionContext connectionContext = null;
                    host.Configure(app =>
                    {
                        var configuration = new ConnectionConfiguration
                        {
                            Resolver = new DefaultDependencyResolver()
                        };

                        app.MapConnection <FilteredConnection>("/echo", configuration);
                        connectionContext = configuration.Resolver.Resolve <IConnectionManager>().GetConnectionContext <FilteredConnection>();
                    });

                    var connection1 = new Client.Connection("http://foo/echo");

                    var wh1 = new ManualResetEventSlim(initialState: false);

                    connection1.Start(host).Wait();

                    connection1.Received += data =>
                    {
                        Assert.Equal("yay", data);
                        wh1.Set();
                    };

                    connectionContext.Groups.Add(connection1.ConnectionId, "Foo").Wait();
                    connectionContext.Groups.Send("Foo", "yay");

                    Assert.True(wh1.Wait(TimeSpan.FromSeconds(10)));

                    connection1.Stop();
                }
            }
Exemplo n.º 36
0
            public void SendToAllButCaller()
            {
                var host = new MemoryHost();
                host.MapConnection<FilteredConnection>("/filter");
                var connection1 = new Client.Connection("http://foo/filter");
                var connection2 = new Client.Connection("http://foo/filter");

                var wh1 = new ManualResetEventSlim(initialState: false);
                var wh2 = new ManualResetEventSlim(initialState: false);

                connection1.Received += data => wh1.Set();
                connection2.Received += data => wh2.Set();

                connection1.Start(host).Wait();
                connection2.Start(host).Wait();

                connection1.Send("test").Wait();

                Assert.False(wh1.WaitHandle.WaitOne(TimeSpan.FromSeconds(5)));
                Assert.True(wh2.WaitHandle.WaitOne(TimeSpan.FromSeconds(5)));

                connection1.Stop();
                connection2.Stop();
            }
Exemplo n.º 37
0
        public void FarmDisconnectOnlyRaisesEventOnce()
        {
            // Each node shares the same bus but are indepenent servers
            var counters = new SignalR.Infrastructure.PerformanceCounterManager();
            var configurationManager = new DefaultConfigurationManager();
            using (var bus = new MessageBus(new StringMinifier(), new TraceManager(), counters, configurationManager))
            {
                var nodeCount = 3;
                var nodes = new List<ServerNode>();
                for (int i = 0; i < nodeCount; i++)
                {
                    nodes.Add(new ServerNode(bus));
                }

                var timeout = TimeSpan.FromSeconds(5);
                foreach (var node in nodes)
                {
                    node.Server.Configuration.HeartbeatInterval = timeout;
                    node.Server.Configuration.DisconnectTimeout = TimeSpan.Zero;
                    node.Server.MapConnection<FarmConnection>("/echo");
                }

                var loadBalancer = new LoadBalancer(nodes.Select(f => f.Server).ToArray());
                var transport = new Client.Transports.LongPollingTransport(loadBalancer);

                var connection = new Client.Connection("http://goo/echo");

                connection.Start(transport).Wait();

                for (int i = 0; i < nodes.Count; i++)
                {
                    nodes[i].Broadcast(String.Format("From Node {0}: {1}", i, i + 1));
                    Thread.Sleep(TimeSpan.FromSeconds(1));
                }

                connection.Stop();

                Thread.Sleep(TimeSpan.FromTicks(timeout.Ticks * nodes.Count));

                Assert.Equal(1, nodes.Sum(n => n.Connection.DisconnectCount));
            }
        }
Exemplo n.º 38
0
        private static void BroadcastFive(MemoryHost host)
        {
            var connection = new Client.Connection("http://samples/Raw-connection");

            connection.Error += e =>
            {
                Console.Error.WriteLine("========ERROR==========");
                Console.Error.WriteLine(e.GetBaseException().ToString());
                Console.Error.WriteLine("=======================");
            };

            connection.Start(new Client.Transports.ServerSentEventsTransport(host)).Wait();

            try
            {
                for (int i = 0; i < 5; i++)
                {
                    var payload = new
                    {
                        type = MessageType.Broadcast,
                        value = "message " + i.ToString()
                    };

                    connection.Send(payload).Wait();
                }

            }
            catch (Exception ex)
            {
                Console.Error.WriteLine("========ERROR==========");
                Console.Error.WriteLine(ex.GetBaseException().ToString());
                Console.Error.WriteLine("=======================");
            }
            finally
            {
                connection.Stop();
            }
        }
Exemplo n.º 39
0
            public void SendCanBeCalledAfterStateChangedEvent(HostType hostType, TransportType transportType)
            {
                using (var host = CreateHost(hostType, transportType))
                {
                    host.Initialize();

                    var connection = new Client.Connection(host.Url + "/multisend");
                    var results = new List<string>();
                    connection.Received += data =>
                    {
                        results.Add(data);
                    };

                    connection.StateChanged += stateChange =>
                    {
                        if (stateChange.NewState == Client.ConnectionState.Connected)
                        {
                            connection.SendWithTimeout("");
                        }
                    };

                    connection.Start(host.Transport).Wait();

                    Thread.Sleep(TimeSpan.FromSeconds(5));

                    connection.Stop();

                    Debug.WriteLine(String.Join(", ", results));

                    Assert.Equal(4, results.Count);
                }
            }
Exemplo n.º 40
0
            public void SendToAllButCaller(HostType hostType, TransportType transportType)
            {
                using (var host = CreateHost(hostType, transportType))
                {
                    host.Initialize();

                    var connection1 = new Client.Connection(host.Url + "/filter");
                    var connection2 = new Client.Connection(host.Url + "/filter");

                    var wh1 = new ManualResetEventSlim(initialState: false);
                    var wh2 = new ManualResetEventSlim(initialState: false);

                    connection1.Received += data => wh1.Set();
                    connection2.Received += data => wh2.Set();

                    connection1.Start(host.Transport).Wait();
                    connection2.Start(host.Transport).Wait();

                    connection1.SendWithTimeout("test");

                    Assert.False(wh1.WaitHandle.WaitOne(TimeSpan.FromSeconds(5)));
                    Assert.True(wh2.WaitHandle.WaitOne(TimeSpan.FromSeconds(5)));

                    connection1.Stop();
                    connection2.Stop();
                }
            }
Exemplo n.º 41
0
            public void FallbackToLongPollingWorks(HostType hostType, TransportType transportType)
            {
                using (var host = CreateHost(hostType, transportType))
                {
                    host.Initialize();

                    var connection = new Client.Connection(host.Url + "/fall-back");

                    connection.Start(host.Transport).Wait();

                    Assert.Equal(connection.Transport.Name, "longPolling");

                    connection.Stop();
                }
            }
Exemplo n.º 42
0
            public void SendToGroupFromOutsideOfConnection()
            {
                using (var host = new MemoryHost())
                {
                    IPersistentConnectionContext connectionContext = null;
                    host.Configure(app =>
                    {
                        var configuration = new ConnectionConfiguration
                        {
                            Resolver = new DefaultDependencyResolver()
                        };

                        app.MapConnection<FilteredConnection>("/echo", configuration);
                        connectionContext = configuration.Resolver.Resolve<IConnectionManager>().GetConnectionContext<FilteredConnection>();
                    });

                    var connection1 = new Client.Connection("http://foo/echo");

                    var wh1 = new ManualResetEventSlim(initialState: false);

                    connection1.Start(host).Wait();

                    connection1.Received += data =>
                    {
                        Assert.Equal("yay", data);
                        wh1.Set();
                    };

                    connectionContext.Groups.Add(connection1.ConnectionId, "Foo").Wait();
                    connectionContext.Groups.Send("Foo", "yay");

                    Assert.True(wh1.Wait(TimeSpan.FromSeconds(10)));

                    connection1.Stop();
                }
            }
Exemplo n.º 43
0
        public static IDisposable ClientGroupsSyncWithServerGroupsOnReconnectLongPolling()
        {
            var host = new MemoryHost();

            host.Configure(app =>
            {
                var config = new ConnectionConfiguration()
                {
                    Resolver = new DefaultDependencyResolver()
                };

                app.MapConnection<MyRejoinGroupConnection>("/groups", config);

                var configuration = config.Resolver.Resolve<IConfigurationManager>();
                configuration.KeepAlive = null;
                configuration.ConnectionTimeout = TimeSpan.FromSeconds(1);
            });

            var connection = new Client.Connection("http://foo/groups");
            var inGroupOnReconnect = new List<bool>();
            var wh = new ManualResetEventSlim();

            connection.Received += message =>
            {
                Console.WriteLine(message);
                wh.Set();
            };

            connection.Reconnected += () =>
            {
                connection.Send(new { type = 3, group = "test", message = "Reconnected" }).Wait();
            };

            connection.Start(new Client.Transports.LongPollingTransport(host)).Wait();

            // Join the group
            connection.Send(new { type = 1, group = "test" }).Wait();

            Thread.Sleep(TimeSpan.FromSeconds(10));

            if (!wh.Wait(TimeSpan.FromSeconds(10)))
            {
                Debugger.Break();
            }

            Console.WriteLine(inGroupOnReconnect.Count > 0);
            Console.WriteLine(String.Join(", ", inGroupOnReconnect.Select(b => b.ToString())));

            connection.Stop();

            return host;
        }
Exemplo n.º 44
0
            public void GroupsRejoinedWhenOnRejoiningGroupsOverridden()
            {
                var host = new MemoryHost();
                host.Configuration.KeepAlive = null;
                host.Configuration.ConnectionTimeout = TimeSpan.FromSeconds(2);
                host.Configuration.HeartBeatInterval = TimeSpan.FromSeconds(2);
                host.MapConnection<MyRejoinGroupConnection>("/groups");

                var connection = new Client.Connection("http://foo/groups");
                var list = new List<string>();
                connection.Received += data =>
                {
                    list.Add(data);
                };

                connection.Start(host).Wait();

                // Join the group
                connection.Send(new { type = 1, group = "test" }).Wait();

                // Sent a message
                connection.Send(new { type = 3, group = "test", message = "hello to group test" }).Wait();

                // Force Reconnect
                Thread.Sleep(TimeSpan.FromSeconds(5));

                // Send a message
                connection.Send(new { type = 3, group = "test", message = "goodbye to group test" }).Wait();

                Thread.Sleep(TimeSpan.FromSeconds(5));

                connection.Stop();

                Assert.Equal(2, list.Count);
                Assert.Equal("hello to group test", list[0]);
                Assert.Equal("goodbye to group test", list[1]);
            }
Exemplo n.º 45
0
            public void GroupsReceiveMessages()
            {
                var host = new MemoryHost();
                host.MapConnection<MyGroupConnection>("/groups");

                var connection = new Client.Connection("http://foo/groups");
                var list = new List<string>();
                connection.Received += data =>
                {
                    list.Add(data);
                };

                connection.Start(host).Wait();

                // Join the group
                connection.Send(new { type = 1, group = "test" }).Wait();

                // Sent a message
                connection.Send(new { type = 3, group = "test", message = "hello to group test" }).Wait();

                // Leave the group
                connection.Send(new { type = 2, group = "test" }).Wait();

                // Send a message
                connection.Send(new { type = 3, group = "test", message = "goodbye to group test" }).Wait();

                Thread.Sleep(TimeSpan.FromSeconds(5));

                connection.Stop();

                Assert.Equal(1, list.Count);
                Assert.Equal("hello to group test", list[0]);
            }
Exemplo n.º 46
0
        public void ConnectionIdsCantBeUsedAsGroups()
        {
            using (var host = new MemoryHost())
            {
                IProtectedData protectedData = null;

                host.Configure(app =>
                {
                    var config = new ConnectionConfiguration
                    {
                        Resolver = new DefaultDependencyResolver()
                    };

                    app.MapConnection <MyConnection>("/echo", config);

                    protectedData = config.Resolver.Resolve <IProtectedData>();
                });

                var connection = new Client.Connection("http://memoryhost/echo");

                var connectionTcs = new TaskCompletionSource <string>();
                var spyTcs        = new TaskCompletionSource <string>();

                connection.Received += data =>
                {
                    connectionTcs.SetResult(data.Trim());
                };

                connection.Start(host).Wait();

                var tcs = new TaskCompletionSource <object>();
                EventSourceStreamReader reader = null;

                Task.Run(async() =>
                {
                    try
                    {
                        string url   = GetUrl(protectedData, connection);
                        var response = await host.Get(url);
                        reader       = new EventSourceStreamReader(connection, response.GetStream());

                        reader.Message = sseEvent =>
                        {
                            if (sseEvent.EventType == EventType.Data &&
                                sseEvent.Data != "initialized" &&
                                sseEvent.Data != "{}")
                            {
                                spyTcs.TrySetResult(sseEvent.Data);
                            }
                        };

                        reader.Start();
                        tcs.TrySetResult(null);
                    }
                    catch (Exception ex)
                    {
                        tcs.TrySetException(ex);
                    }
                });

                tcs.Task.Wait();

                connection.SendWithTimeout("STUFFF");

                Assert.True(connectionTcs.Task.Wait(TimeSpan.FromSeconds(5)));
                Assert.Equal("STUFFF", connectionTcs.Task.Result);
                Assert.False(spyTcs.Task.Wait(TimeSpan.FromSeconds(5)));

                connection.Stop();
            }
        }
Exemplo n.º 47
0
            public void ReconnectFiresAfterHostShutDown(HostType hostType, TransportType transportType)
            {
                using (var host = CreateHost(hostType, transportType))
                {
                    host.Initialize();

                    var connection = new Client.Connection(host.Url + "/my-reconnect");
                    connection.Start(host.Transport).Wait();

                    host.Shutdown();

                    Thread.Sleep(TimeSpan.FromSeconds(5));

                    Assert.Equal(Client.ConnectionState.Reconnecting, connection.State);

                    connection.Stop();
                }
            }
Exemplo n.º 48
0
            public void SendRaisesOnReceivedFromAllEvents(HostType hostType, TransportType transportType)
            {
                using (var host = CreateHost(hostType, transportType))
                {
                    host.Initialize();

                    var connection = new Client.Connection(host.Url + "/multisend");
                    var results = new List<string>();
                    connection.Received += data =>
                    {
                        results.Add(data);
                    };

                    connection.Start(host.Transport).Wait();
                    connection.SendWithTimeout("");

                    Thread.Sleep(TimeSpan.FromSeconds(5));

                    connection.Stop();

                    Debug.WriteLine(String.Join(", ", results));

                    Assert.Equal(4, results.Count);
                    Assert.Equal("OnConnectedAsync1", results[0]);
                    Assert.Equal("OnConnectedAsync2", results[1]);
                    Assert.Equal("OnReceivedAsync1", results[2]);
                    Assert.Equal("OnReceivedAsync2", results[3]);
                }
            }
Exemplo n.º 49
0
        public async Task DisconnectFiresForPersistentConnectionWhenClientCallsStop()
        {
            using (var host = new MemoryHost())
            {
                var connectWh = new AsyncManualResetEvent();
                var disconnectWh = new AsyncManualResetEvent();
                var dr = new DefaultDependencyResolver();
                var configuration = dr.Resolve<IConfigurationManager>();

                host.Configure(app =>
                {
                    var config = new ConnectionConfiguration
                    {
                        Resolver = dr
                    };

                    app.MapSignalR<MyConnection>("/echo", config);

                    configuration.DisconnectTimeout = TimeSpan.FromSeconds(6);

                    dr.Register(typeof(MyConnection), () => new MyConnection(connectWh, disconnectWh));
                });
                var connection = new Client.Connection("http://foo/echo");

                // Maximum wait time for disconnect to fire (3 heart beat intervals)
                var disconnectWait = TimeSpan.FromTicks(configuration.HeartbeatInterval().Ticks * 3);

                await connection.Start(host);

                Assert.True(await connectWh.WaitAsync(TimeSpan.FromSeconds(10)), "Connect never fired");

                connection.Stop();

                Assert.True(await disconnectWh.WaitAsync(disconnectWait), "Disconnect never fired");
            }
        }
Exemplo n.º 50
0
            public void EnvironmentIsAvailable(HostType hostType, TransportType transportType)
            {
                using (var host = CreateHost(hostType, transportType))
                {
                    host.Initialize();

                    var connection = new Client.Connection(host.Url + "/items");
                    var connection2 = new Client.Connection(host.Url + "/items");

                    var results = new List<RequestItemsResponse>();
                    connection2.Received += data =>
                    {
                        var val = JsonConvert.DeserializeObject<RequestItemsResponse>(data);
                        if (!results.Contains(val))
                        {
                            results.Add(val);
                        }
                    };

                    connection.Start(host.Transport).Wait();
                    connection2.Start(host.Transport).Wait();

                    Thread.Sleep(TimeSpan.FromSeconds(2));

                    connection.SendWithTimeout(null);

                    Thread.Sleep(TimeSpan.FromSeconds(2));

                    connection.Stop();

                    Thread.Sleep(TimeSpan.FromSeconds(2));

                    Debug.WriteLine(String.Join(", ", results));

                    Assert.Equal(3, results.Count);
                    Assert.Equal("OnConnectedAsync", results[0].Method);
                    Assert.Equal(1, results[0].Keys.Length);
                    Assert.Equal("owin.environment", results[0].Keys[0]);
                    Assert.Equal("OnReceivedAsync", results[1].Method);
                    Assert.Equal(1, results[1].Keys.Length);
                    Assert.Equal("owin.environment", results[1].Keys[0]);
                    Assert.Equal("OnDisconnectAsync", results[2].Method);
                    Assert.Equal(1, results[2].Keys.Length);
                    Assert.Equal("owin.environment", results[2].Keys[0]);

                    connection2.Stop();
                }
            }
Exemplo n.º 51
0
            public void SendWithSyncErrorThrows(HostType hostType, TransportType transportType)
            {
                using (var host = CreateHost(hostType, transportType))
                {
                    host.Initialize();

                    var connection = new Client.Connection(host.Url + "/sync-error");

                    connection.Start(host.Transport).Wait();

                    Assert.Throws<AggregateException>(() => connection.SendWithTimeout("test"));

                    connection.Stop();
                }
            }
Exemplo n.º 52
0
            public void ReconnectFiresAfterHostShutDown()
            {
                var host = new MemoryHost();
                var conn = new MyReconnect();
                host.DependencyResolver.Register(typeof(MyReconnect), () => conn);
                host.MapConnection<MyReconnect>("/endpoint");

                var connection = new Client.Connection("http://foo/endpoint");
                connection.Start(host).Wait();

                host.Dispose();

                Thread.Sleep(TimeSpan.FromSeconds(5));

                Assert.Equal(Client.ConnectionState.Reconnecting, connection.State);

                connection.Stop();
            }
Exemplo n.º 53
0
            public void ReconnectDoesntFireAfterTimeOut(TransportType transportType, MessageBusType messageBusType)
            {
                using (var host = new MemoryHost())
                {
                    var conn = new MyReconnect();
                    host.Configure(app =>
                    {
                        var config = new ConnectionConfiguration
                        {
                            Resolver = new DefaultDependencyResolver()
                        };

                        UseMessageBus(messageBusType, config.Resolver);

                        app.MapSignalR<MyReconnect>("/endpoint", config);
                        var configuration = config.Resolver.Resolve<IConfigurationManager>();
                        configuration.DisconnectTimeout = TimeSpan.FromSeconds(6);
                        configuration.ConnectionTimeout = TimeSpan.FromSeconds(2);
                        configuration.KeepAlive = null;

                        config.Resolver.Register(typeof(MyReconnect), () => conn);
                    });

                    var connection = new Client.Connection("http://foo/endpoint");
                    var transport = CreateTransport(transportType, host);
                    connection.Start(transport).Wait();

                    Thread.Sleep(TimeSpan.FromSeconds(5));

                    connection.Stop();

                    Assert.Equal(0, conn.Reconnects);
                }
            }
Exemplo n.º 54
0
            public void ReconnectFiresAfterTimeOutSSE()
            {
                var host = new MemoryHost();
                var conn = new MyReconnect();
                host.Configuration.KeepAlive = null;
                host.Configuration.ConnectionTimeout = TimeSpan.FromSeconds(5);
                host.Configuration.HeartBeatInterval = TimeSpan.FromSeconds(5);
                host.DependencyResolver.Register(typeof(MyReconnect), () => conn);
                host.MapConnection<MyReconnect>("/endpoint");

                var connection = new Client.Connection("http://foo/endpoint");
                connection.Start(new Client.Transports.ServerSentEventsTransport(host)).Wait();

                Thread.Sleep(TimeSpan.FromSeconds(15));

                connection.Stop();

                Assert.InRange(conn.Reconnects, 1, 4);
            }
Exemplo n.º 55
0
            public void GroupCanBeAddedAndMessagedOnConnected(HostType hostType, TransportType transportType)
            {
                using (var host = CreateHost(hostType, transportType))
                {
                    var wh = new ManualResetEventSlim();
                    host.Initialize();

                    var connection = new Client.Connection(host.Url + "/add-group");
                    connection.Received += data =>
                    {
                        Assert.Equal("hey", data);
                        wh.Set();
                    };

                    connection.Start(host.Transport).Wait();
                    connection.SendWithTimeout("");

                    Assert.True(wh.Wait(TimeSpan.FromSeconds(5)));

                    connection.Stop();
                }
            }
Exemplo n.º 56
0
            public void GroupsRejoinedWhenOnRejoiningGroupsOverridden(HostType hostType, TransportType transportType)
            {
                using (var host = CreateHost(hostType, transportType))
                {
                    host.Initialize(keepAlive: 0,
                                    connectionTimeout: 2,
                                    hearbeatInterval: 2);

                    var connection = new Client.Connection(host.Url + "/rejoin-groups");

                    var list = new List<string>();
                    connection.Received += data =>
                    {
                        list.Add(data);
                    };

                    connection.Start(host.Transport).Wait();

                    // Join the group
                    connection.SendWithTimeout(new { type = 1, group = "test" });

                    // Sent a message
                    connection.SendWithTimeout(new { type = 3, group = "test", message = "hello to group test" });

                    // Force Reconnect
                    Thread.Sleep(TimeSpan.FromSeconds(5));

                    // Send a message
                    connection.SendWithTimeout(new { type = 3, group = "test", message = "goodbye to group test" });

                    Thread.Sleep(TimeSpan.FromSeconds(5));

                    connection.Stop();

                    Assert.Equal(2, list.Count);
                    Assert.Equal("hello to group test", list[0]);
                    Assert.Equal("goodbye to group test", list[1]);
                }
            }
Exemplo n.º 57
0
            public void GroupsAreNotReadOnConnectedAsyncLongPolling()
            {
                var host = new MemoryHost();
                host.MapConnection<MyConnection>("/echo");

                var connection = new Client.Connection("http://foo/echo");
                connection.Groups = new List<string> { typeof(MyConnection).FullName + ".test" };
                connection.Received += data =>
                {
                    Assert.False(true, "Unexpectedly received data");
                };

                var transport = new Client.Transports.LongPollingTransport(host);
                connection.Start(transport).Wait();

                Thread.Sleep(TimeSpan.FromSeconds(5));

                connection.Stop();
            }
Exemplo n.º 58
0
            public void GroupsAreNotReadOnConnectedAsync(HostType hostType, TransportType transportType)
            {
                using (var host = CreateHost(hostType, transportType))
                {
                    host.Initialize();

                    var connection = new Client.Connection(host.Url + "/group-echo");
                    ((Client.IConnection)connection).Groups.Add(typeof(MyGroupEchoConnection).FullName + ".test");
                    connection.Received += data =>
                    {
                        Assert.False(true, "Unexpectedly received data");
                    };

                    connection.Start(host.Transport).Wait();

                    Thread.Sleep(TimeSpan.FromSeconds(5));

                    connection.Stop();
                }
            }
Exemplo n.º 59
0
            public void SendRaisesOnReceivedFromAllEvents()
            {
                var host = new MemoryHost();
                host.MapConnection<MySendingConnection>("/multisend");

                var connection = new Client.Connection("http://foo/multisend");
                var results = new List<string>();
                connection.Received += data =>
                {
                    results.Add(data);
                };

                connection.Start(host).Wait();
                connection.Send("").Wait();

                Thread.Sleep(TimeSpan.FromSeconds(5));

                connection.Stop();

                Debug.WriteLine(String.Join(", ", results));

                Assert.Equal(4, results.Count);
                Assert.Equal("OnConnectedAsync1", results[0]);
                Assert.Equal("OnConnectedAsync2", results[1]);
                Assert.Equal("OnReceivedAsync1", results[2]);
                Assert.Equal("OnReceivedAsync2", results[3]);
            }
Exemplo n.º 60
0
            public void ReconnectFiresAfterTimeOut(TransportType transportType)
            {
                using (var host = new MemoryHost())
                {
                    var conn = new MyReconnect();
                    host.Configure(app =>
                    {
                        var config = new ConnectionConfiguration
                        {
                            Resolver = new DefaultDependencyResolver()
                        };

                        app.MapConnection<MyReconnect>("/endpoint", config);
                        var configuration = config.Resolver.Resolve<IConfigurationManager>();
                        configuration.KeepAlive = 0;
                        configuration.ConnectionTimeout = TimeSpan.FromSeconds(5);
                        configuration.HeartbeatInterval = TimeSpan.FromSeconds(5);

                        config.Resolver.Register(typeof(MyReconnect), () => conn);
                    });

                    var connection = new Client.Connection("http://foo/endpoint");
                    var transport = CreateTransport(transportType, host);
                    connection.Start(transport).Wait();

                    Thread.Sleep(TimeSpan.FromSeconds(15));

                    connection.Stop();

                    Assert.InRange(conn.Reconnects, 1, 4);
                }
            }