Esempio n. 1
0
        public static IDisposable RunConnectDisconnect(bool scaleout, int nodes = 1, int connections = 1000)
        {
            IHttpClient client;
            IDisposable disposable = TryGetClient(scaleout, nodes, out client);

            for (int i = 0; i < connections; i++)
            {
                var connection = new Client.Hubs.HubConnection("http://foo");
                var proxy      = connection.CreateHubProxy("EchoHub");
                var wh         = new ManualResetEventSlim(false);

                proxy.On("echo", _ => wh.Set());

                try
                {
                    connection.Start(client).Wait();

                    proxy.Invoke("Echo", "foo").Wait();

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

            return(disposable);
        }
Esempio n. 2
0
        public void DisconnectFiresForHubsWhenConnectionGoesAway()
        {
            using (var host = new MemoryHost())
            {
                host.MapHubs();
                host.Configuration.DisconnectTimeout = TimeSpan.Zero;
                host.Configuration.HeartBeatInterval = TimeSpan.FromSeconds(5);
                var connectWh    = new ManualResetEventSlim();
                var disconnectWh = new ManualResetEventSlim();
                host.DependencyResolver.Register(typeof(MyHub), () => new MyHub(connectWh, disconnectWh));
                var connection = new Client.Hubs.HubConnection("http://foo/");

                connection.CreateHubProxy("MyHub");

                // 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");
            }
        }
Esempio n. 3
0
        public void GuidTest()
        {
            using (var host = new MemoryHost())
            {
                host.MapHubs();
                var connection = new Client.Hubs.HubConnection("http://site/");

                var hub = connection.CreateHubProxy("demo");

                var wh = new ManualResetEvent(false);

                hub.On <Guid>("TestGuid", id =>
                {
                    Assert.NotNull(id);
                    wh.Set();
                });

                connection.Start(host).Wait();

                hub.Invoke("TestGuid").Wait();

                Assert.True(wh.WaitOne(TimeSpan.FromSeconds(5)));
                connection.Stop();
            }
        }
Esempio n. 4
0
        public void AuthenticatedAndAuthorizedUserCanInvokeMethodsInHubsAuthorizedWithRoles()
        {
            using (var host = new MemoryHost())
            {
                host.MapHubs();
                var connection = new Client.Hubs.HubConnection("http://foo/");

                host.User = new GenericPrincipal(new GenericIdentity("test"), new string[] { "Admin" });

                var hub = connection.CreateHubProxy("AdminAuthHub");
                var wh = new ManualResetEvent(false);
                hub.On<string, string>("invoked", (id, time) =>
                {
                    Assert.NotNull(id);
                    wh.Set();
                });

                connection.Start(host).Wait();

                hub.InvokeWithTimeout("InvokedFromClient");

                Assert.True(wh.WaitOne(TimeSpan.FromSeconds(3)));
                connection.Stop();
            }
        }
Esempio n. 5
0
        public void UnauthenticatedUserCannotInvokeMethodsWhenAuthenticationRequiredGlobally()
        {
            using (var host = new MemoryHost())
            {
                host.MapHubs();
                var connection = new Client.Hubs.HubConnection("http://foo/");

                host.HubPipeline.RequireAuthentication();
                host.User = new GenericPrincipal(new GenericIdentity(""), new string[] { });

                var hub = connection.CreateHubProxy("NoAuthHub");
                var wh  = new ManualResetEvent(false);
                hub.On <string, string>("invoked", (id, time) =>
                {
                    Assert.NotNull(id);
                    wh.Set();
                });

                connection.Start(host).Wait();

                Assert.Throws <AggregateException>(() => hub.Invoke("InvokedFromClient").Wait());

                Assert.False(wh.WaitOne(TimeSpan.FromSeconds(3)));
                connection.Stop();
            }
        }
Esempio n. 6
0
        public void AddingToMultipleGroups(HostType hostType, TransportType transportType)
        {
            using (var host = CreateHost(hostType, transportType))
            {
                host.Initialize();
                int max = 10;

                var countDown  = new CountDownRange <int>(Enumerable.Range(0, max));
                var connection = new Client.Hubs.HubConnection(host.Url);
                var proxy      = connection.CreateHubProxy("MultGroupHub");

                proxy.On <User>("onRoomJoin", user =>
                {
                    Assert.True(countDown.Mark(user.Index));
                });

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

                for (int i = 0; i < max; i++)
                {
                    var user = new User {
                        Index = i, Name = "tester", Room = "test" + i
                    };
                    proxy.InvokeWithTimeout("login", user);
                    proxy.InvokeWithTimeout("joinRoom", user);
                }

                Assert.True(countDown.Wait(TimeSpan.FromSeconds(30)), "Didn't receive " + max + " messages. Got " + (max - countDown.Count) + " missed " + String.Join(",", countDown.Left.Select(i => i.ToString())));

                connection.Stop();
            }
        }
Esempio n. 7
0
        public void BasicAuthCredentialsFlow(TransportType transportType)
        {
            using (var host = new OwinTestHost())
            {
                Debug.Listeners.Clear();

                host.Start<BasicAuthApplication>();

                var connection = new Client.Hubs.HubConnection(host.Url);

                var hub = connection.CreateHubProxy("demo");

                hub["name"] = "test";

                connection.Credentials = new System.Net.NetworkCredential("user", "password");

                connection.Start(CreateTransport(transportType)).Wait();

                var result = hub.InvokeWithTimeout<string>("ReadStateValue");

                Assert.Equal("test", result);

                connection.Stop();
            }
        }
Esempio n. 8
0
        public void CancelledGenericTask(HostType hostType, TransportType transportType)
        {
            using (var host = CreateHost(hostType, transportType))
            {
                host.Initialize();
                var connection = new Client.Hubs.HubConnection(host.Url);
                var tcs        = new TaskCompletionSource <object>();

                var hub = connection.CreateHubProxy("demo");
                connection.Start(host.Transport).Wait();

                hub.Invoke("CancelledGenericTask").ContinueWith(tcs);

                try
                {
                    tcs.Task.Wait(TimeSpan.FromSeconds(10));
                    Assert.True(false, "Didn't fault");
                }
                catch (Exception)
                {
                }

                connection.Stop();
            }
        }
Esempio n. 9
0
        public void ChangeHubUrl()
        {
            using (var host = new MemoryHost())
            {
                host.MapHubs("/foo");
                var connection = new Client.Hubs.HubConnection("http://site/foo", useDefaultUrl: false);

                var hub = connection.CreateHubProxy("demo");

                var wh = new ManualResetEventSlim(false);

                hub.On("signal", id =>
                {
                    Assert.NotNull(id);
                    wh.Set();
                });

                connection.Start(host).Wait();

                hub.InvokeWithTimeout("DynamicTask");

                Assert.True(wh.Wait(TimeSpan.FromSeconds(10)));
                connection.Stop();
            }
        }
Esempio n. 10
0
        public void SendToSelf(HostType hostType, TransportType transportType)
        {
            using (var host = CreateHost(hostType, transportType))
            {
                host.Initialize();

                var connection1 = new Client.Hubs.HubConnection(host.Url);
                var connection2 = new Client.Hubs.HubConnection(host.Url);

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

                var hub1 = connection1.CreateHubProxy("SendToSome");
                var hub2 = connection2.CreateHubProxy("SendToSome");

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

                hub1.On("send", wh1.Set);
                hub2.On("send", wh2.Set);

                hub1.InvokeWithTimeout("SendToSelf");

                Assert.True(wh1.Wait(TimeSpan.FromSeconds(10)));
                Assert.False(wh2.Wait(TimeSpan.FromSeconds(5)));

                connection1.Stop();
                connection2.Stop();
            }
        }
Esempio n. 11
0
        public void SendToAllFromOutsideOfHub()
        {
            using (var host = new MemoryHost())
            {
                host.MapHubs();
                var connection1 = new Client.Hubs.HubConnection("http://foo/");
                var connection2 = new Client.Hubs.HubConnection("http://foo/");
                var hubContext  = host.ConnectionManager.GetHubContext("SendToSome");

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

                var hub1 = connection1.CreateHubProxy("SendToSome");
                var hub2 = connection2.CreateHubProxy("SendToSome");

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

                hub1.On("send", wh1.Set);
                hub2.On("send", wh2.Set);

                hubContext.Clients.All.send();

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

                connection1.Stop();
                connection2.Stop();
            }
        }
Esempio n. 12
0
        public void AddingToMultipleGroups(HostType hostType, TransportType transportType)
        {
            using (var host = CreateHost(hostType, transportType))
            {
                host.Initialize();
                int max = 10;

                var countDown = new CountDownRange<int>(Enumerable.Range(0, max));
                var connection = new Client.Hubs.HubConnection(host.Url);
                var proxy = connection.CreateHubProxy("MultGroupHub");

                proxy.On<User>("onRoomJoin", user =>
                {
                    Assert.True(countDown.Mark(user.Index));
                });

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

                for (int i = 0; i < max; i++)
                {
                    var user = new User { Index = i, Name = "tester", Room = "test" + i };
                    proxy.InvokeWithTimeout("login", user);
                    proxy.InvokeWithTimeout("joinRoom", user);
                }

                Assert.True(countDown.Wait(TimeSpan.FromSeconds(30)), "Didn't receive " + max + " messages. Got " + (max - countDown.Count) + " missed " + String.Join(",", countDown.Left.Select(i => i.ToString())));

                connection.Stop();
            }
        }
Esempio n. 13
0
        public void DisconnectFiresForHubsWhenConnectionGoesAway()
        {
            using (var host = new MemoryHost())
            {
                host.MapHubs();
                host.Configuration.DisconnectTimeout = TimeSpan.Zero;
                host.Configuration.HeartbeatInterval = TimeSpan.FromSeconds(5);
                var connectWh = new ManualResetEventSlim();
                var disconnectWh = new ManualResetEventSlim();
                host.DependencyResolver.Register(typeof(MyHub), () => new MyHub(connectWh, disconnectWh));
                var connection = new Client.Hubs.HubConnection("http://foo/");

                connection.CreateHubProxy("MyHub");

                // 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");
            }
        }
Esempio n. 14
0
        public void CancelledGenericTask(HostType hostType, TransportType transportType)
        {
            using (var host = CreateHost(hostType, transportType))
            {
                host.Initialize();
                var connection = new Client.Hubs.HubConnection(host.Url);
                var tcs = new TaskCompletionSource<object>();

                var hub = connection.CreateHubProxy("demo");
                connection.Start(host.Transport).Wait();

                hub.Invoke("CancelledGenericTask").ContinueWith(tcs);

                try
                {
                    tcs.Task.Wait(TimeSpan.FromSeconds(10));
                    Assert.True(false, "Didn't fault");
                }
                catch (Exception)
                {

                }

                connection.Stop();
            }
        }
Esempio n. 15
0
        public static IDisposable RunConnectDisconnect(int connections)
        {
            var host = new MemoryHost();

            host.MapHubs();

            for (int i = 0; i < connections; i++)
            {
                var connection = new Client.Hubs.HubConnection("http://foo");
                var proxy = connection.CreateHubProxy("EchoHub");
                var wh = new ManualResetEventSlim(false);

                proxy.On("echo", _ => wh.Set());

                try
                {
                    connection.Start(host).Wait();

                    proxy.Invoke("Echo", "foo").Wait();

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

            return host;
        }
Esempio n. 16
0
        public static IDisposable RunConnectDisconnect(int connections)
        {
            var host = new MemoryHost();

            host.MapHubs();

            for (int i = 0; i < connections; i++)
            {
                var connection = new Client.Hubs.HubConnection("http://foo");
                var proxy      = connection.CreateHubProxy("EchoHub");
                var wh         = new ManualResetEventSlim(false);

                proxy.On("echo", _ => wh.Set());

                try
                {
                    connection.Start(host).Wait();

                    proxy.Invoke("Echo", "foo").Wait();

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

            return(host);
        }
Esempio n. 17
0
        public void UnauthorizedUserCannotInvokeMethodsInHubsAuthorizedSpecifyingUserAndRole()
        {
            using (var host = new MemoryHost())
            {
                host.MapHubs();
                var connection = new Client.Hubs.HubConnection("http://foo/");

                host.User = new GenericPrincipal(new GenericIdentity("test"), new string[] { "User", "Admin" });

                var hub = connection.CreateHubProxy("UserAndRoleAuthHub");
                var wh  = new ManualResetEvent(false);
                hub.On <string, string>("invoked", (id, time) =>
                {
                    Assert.NotNull(id);
                    wh.Set();
                });

                connection.Start(host).Wait();

                Assert.Throws <AggregateException>(() => hub.Invoke("InvokedFromClient").Wait());

                Assert.False(wh.WaitOne(TimeSpan.FromSeconds(3)));
                connection.Stop();
            }
        }
Esempio n. 18
0
        public void SendToSelf()
        {
            var host = new MemoryHost();

            host.MapHubs();
            var connection1 = new Client.Hubs.HubConnection("http://foo/");
            var connection2 = new Client.Hubs.HubConnection("http://foo/");

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

            var hub1 = connection1.CreateHubProxy("SendToSome");
            var hub2 = connection2.CreateHubProxy("SendToSome");

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

            hub1.On("send", wh1.Set);
            hub2.On("send", wh2.Set);

            hub1.Invoke("SendToSelf").Wait();

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

            connection1.Stop();
            connection2.Stop();
        }
Esempio n. 19
0
        public void AuthenticatedAndAuthorizedUserCanInvokeAuthorizedHubMethods()
        {
            using (var host = new MemoryHost())
            {
                host.MapHubs();
                var connection = new Client.Hubs.HubConnection("http://foo/");

                host.User = new GenericPrincipal(new GenericIdentity("test"), new string[] { "User", "Admin" });

                var hub = connection.CreateHubProxy("InvokeAuthHub");
                var wh  = new ManualResetEvent(false);
                hub.On <string, string>("invoked", (id, time) =>
                {
                    Assert.NotNull(id);
                    wh.Set();
                });

                connection.Start(host).Wait();

                hub.Invoke("InvokedFromClient").Wait();

                Assert.True(wh.WaitOne(TimeSpan.FromSeconds(3)));
                connection.Stop();
            }
        }
Esempio n. 20
0
        public void AuthenticatedAndAuthorizedUserCanInvokeMethodsInHubsAuthorizedSpecifyingUserAndRole()
        {
            using (var host = new MemoryHost())
            {
                host.Configure(app =>
                {
                    var configuration = new HubConfiguration
                    {
                        Resolver = new DefaultDependencyResolver()
                    };

                    WithUser(app, new GenericPrincipal(new GenericIdentity("User"), new string[] { "Admin" }));
                    app.MapHubs("/signalr", configuration);
                });

                var connection = new Client.Hubs.HubConnection("http://foo/");

                var hub = connection.CreateHubProxy("UserAndRoleAuthHub");
                var wh = new ManualResetEvent(false);
                hub.On<string, string>("invoked", (id, time) =>
                {
                    Assert.NotNull(id);
                    wh.Set();
                });

                connection.Start(host).Wait();

                hub.InvokeWithTimeout("InvokedFromClient");

                Assert.True(wh.WaitOne(TimeSpan.FromSeconds(3)));
                connection.Stop();
            }
        }
Esempio n. 21
0
        public void JoinAndSendToGroupRenamedHub(HostType hostType, TransportType transportType)
        {
            using (var host = CreateHost(hostType, transportType))
            {
                host.Initialize(enableAutoRejoiningGroups: true);

                var connection = new Client.Hubs.HubConnection(host.Url);
                var wh         = new ManualResetEventSlim();

                var hub = connection.CreateHubProxy("groupChat");

                hub.On("send", wh.Set);

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

                hub.InvokeWithTimeout("Join", "Foo");

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

                hub.InvokeWithTimeout("Send", "Foo", "new test");

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

                connection.Stop();
            }
        }
Esempio n. 22
0
        public void CreatedHubsGetDisposed()
        {
            var mockHubs = new List <Mock <IHub> >();

            using (var host = new MemoryHost())
            {
                host.DependencyResolver.Register(typeof(IHub), () =>
                {
                    var mockHub = new Mock <IHub>()
                    {
                        CallBase = true
                    };

                    mockHubs.Add(mockHub);
                    return(mockHub.Object);
                });
                host.MapHubs();
                var connection = new Client.Hubs.HubConnection("http://foo/");

                var hub = connection.CreateHubProxy("demo");

                connection.Start(host).Wait();

                var result = hub.InvokeWithTimeout <string>("ReadStateValue");

                foreach (var mockDemoHub in mockHubs)
                {
                    mockDemoHub.Verify(d => d.Dispose(), Times.Once());
                }

                connection.Stop();
            }
        }
Esempio n. 23
0
        private static void RunOne(MemoryHost host)
        {
            var connection = new Client.Hubs.HubConnection("http://foo");
            var proxy      = connection.CreateHubProxy("HubWithGroups");
            var wh         = new ManualResetEventSlim(false);

            proxy.On <int>("Do", i => wh.Set());

            try
            {
                connection.Start(host).Wait();

                proxy.Invoke("Join", "foo").Wait();

                proxy.Invoke("Send", "foo", 0).Wait();

                if (!wh.Wait(TimeSpan.FromSeconds(10)))
                {
                    Debugger.Break();
                }
            }
            finally
            {
                connection.Stop();
            }
        }
Esempio n. 24
0
        public void ChangeHubUrl()
        {
            using (var host = new MemoryHost())
            {
                host.MapHubs("/foo");
                var connection = new Client.Hubs.HubConnection("http://site/foo", useDefaultUrl: false);

                var hub = connection.CreateHubProxy("demo");

                var wh = new ManualResetEventSlim(false);

                hub.On("signal", id =>
                {
                    Assert.NotNull(id);
                    wh.Set();
                });

                connection.Start(host).Wait();

                hub.Invoke("DynamicTask").Wait();

                Assert.True(wh.Wait(TimeSpan.FromSeconds(10)));
                connection.Stop();
            }
        }
Esempio n. 25
0
        public void GuidTest(HostType hostType, TransportType transportType)
        {
            using (var host = CreateHost(hostType, transportType))
            {
                host.Initialize();
                var connection = new Client.Hubs.HubConnection(host.Url);

                var hub = connection.CreateHubProxy("demo");

                var wh = new ManualResetEventSlim(false);

                hub.On <Guid>("TestGuid", id =>
                {
                    Assert.NotNull(id);
                    wh.Set();
                });

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

                hub.InvokeWithTimeout("TestGuid");

                Assert.True(wh.Wait(TimeSpan.FromSeconds(10)));
                connection.Stop();
            }
        }
Esempio n. 26
0
        public void ReturningNullFromReconnectAccepted()
        {
            var mockHub = new Mock <SomeHub>()
            {
                CallBase = true
            };

            mockHub.Setup(h => h.OnReconnected()).Returns <Task>(null).Verifiable();

            using (var host = new MemoryHost())
            {
                host.HubPipeline.EnableAutoRejoiningGroups();
                host.Configuration.KeepAlive         = null;
                host.Configuration.ConnectionTimeout = TimeSpan.FromSeconds(1);
                host.Configuration.HeartbeatInterval = TimeSpan.FromSeconds(1);
                host.DependencyResolver.Register(typeof(SomeHub), () => mockHub.Object);
                host.MapHubs();
                var connection = new Client.Hubs.HubConnection("http://foo");

                var hub = connection.CreateHubProxy("SomeHub");
                connection.Start(host).Wait();

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

                hub.InvokeWithTimeout("AllFoo");

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

                connection.Stop();

                mockHub.Verify();
            }
        }
Esempio n. 27
0
        public void RejoiningGroupsOnlyReceivesGroupsBelongingToHub()
        {
            var logRejoiningGroups = new LogRejoiningGroupsModule();

            using (var host = new MemoryHost())
            {
                host.HubPipeline.AddModule(logRejoiningGroups);

                host.Configuration.KeepAlive         = null;
                host.Configuration.ConnectionTimeout = TimeSpan.FromSeconds(1);
                host.Configuration.HeartbeatInterval = TimeSpan.FromSeconds(1);
                host.MapHubs();

                var connection = new Client.Hubs.HubConnection("http://foo");
                var proxy      = connection.CreateHubProxy("MultGroupHub");
                var proxy2     = connection.CreateHubProxy("MultGroupHub2");

                connection.Start(host).Wait();

                var user = new User {
                    Name = "tester"
                };
                proxy.InvokeWithTimeout("login", user);
                proxy2.InvokeWithTimeout("login", user);

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

                proxy.InvokeWithTimeout("joinRoom", user);
                proxy2.InvokeWithTimeout("joinRoom", user);

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

                Assert.True(logRejoiningGroups.GroupsRejoined["MultGroupHub"].Contains("foo"));
                Assert.True(logRejoiningGroups.GroupsRejoined["MultGroupHub"].Contains("tester"));
                Assert.False(logRejoiningGroups.GroupsRejoined["MultGroupHub"].Contains("foo2"));
                Assert.False(logRejoiningGroups.GroupsRejoined["MultGroupHub"].Contains("tester2"));
                Assert.True(logRejoiningGroups.GroupsRejoined["MultGroupHub2"].Contains("foo2"));
                Assert.True(logRejoiningGroups.GroupsRejoined["MultGroupHub2"].Contains("tester2"));
                Assert.False(logRejoiningGroups.GroupsRejoined["MultGroupHub2"].Contains("foo"));
                Assert.False(logRejoiningGroups.GroupsRejoined["MultGroupHub2"].Contains("tester"));

                connection.Stop();
            }
        }
Esempio n. 28
0
        public static IDisposable BrodcastFromServer()
        {
            var         host    = new MemoryHost();
            IHubContext context = null;

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

                app.MapHubs(config);

                var configuration = config.Resolver.Resolve <IConfigurationManager>();
                // The below effectively sets the heartbeat interval to five seconds.
                configuration.KeepAlive = TimeSpan.FromSeconds(10);

                var connectionManager = config.Resolver.Resolve <IConnectionManager>();
                context = connectionManager.GetHubContext("EchoHub");
            });

            var cancellationTokenSource = new CancellationTokenSource();

            var thread = new Thread(() =>
            {
                while (!cancellationTokenSource.IsCancellationRequested)
                {
                    context.Clients.All.echo();
                }
            });

            thread.Start();

            var connection = new Client.Hubs.HubConnection("http://foo");
            var proxy      = connection.CreateHubProxy("EchoHub");

            try
            {
                connection.Start(host).Wait();

                Thread.Sleep(1000);
            }
            finally
            {
                connection.Stop();
            }

            return(new DisposableAction(() =>
            {
                cancellationTokenSource.Cancel();

                thread.Join();

                host.Dispose();
            }));
        }
Esempio n. 29
0
        public static IDisposable BrodcastFromServer()
        {
            var host = new MemoryHost();
            IHubContext context = null;

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

                app.MapHubs(config);

                var configuration = config.Resolver.Resolve<IConfigurationManager>();
                // The below effectively sets the heartbeat interval to five seconds.
                configuration.KeepAlive = TimeSpan.FromSeconds(10);

                var connectionManager = config.Resolver.Resolve<IConnectionManager>();
                context = connectionManager.GetHubContext("EchoHub");
            });

            var cancellationTokenSource = new CancellationTokenSource();

            var thread = new Thread(() =>
            {
                while (!cancellationTokenSource.IsCancellationRequested)
                {
                    context.Clients.All.echo();
                }
            });

            thread.Start();

            var connection = new Client.Hubs.HubConnection("http://foo");
            var proxy = connection.CreateHubProxy("EchoHub");

            try
            {
                connection.Start(host).Wait();

                Thread.Sleep(1000);
            }
            finally
            {
                connection.Stop();
            }

            return new DisposableAction(() =>
            {
                cancellationTokenSource.Cancel();

                thread.Join();

                host.Dispose();
            });
        }
Esempio n. 30
0
        public void HubGroupsRejoinWhenAutoRejoiningGroupsEnabled()
        {
            var host = new MemoryHost();

            host.HubPipeline.EnableAutoRejoiningGroups();
            host.Configuration.KeepAlive         = null;
            host.Configuration.ConnectionTimeout = TimeSpan.FromSeconds(1);
            host.Configuration.HeartBeatInterval = TimeSpan.FromSeconds(1);
            host.MapHubs();
            int max = 10;

            var countDown = new CountDownRange <int>(Enumerable.Range(0, max));
            var countDownAfterReconnect = new CountDownRange <int>(Enumerable.Range(max, max));
            var connection = new Client.Hubs.HubConnection("http://foo");
            var proxy      = connection.CreateHubProxy("MultGroupHub");

            proxy.On <User>("onRoomJoin", u =>
            {
                if (u.Index < max)
                {
                    Assert.True(countDown.Mark(u.Index));
                }
                else
                {
                    Assert.True(countDownAfterReconnect.Mark(u.Index));
                }
            });

            connection.Start(host).Wait();

            var user = new User {
                Name = "tester"
            };

            proxy.Invoke("login", user).Wait();

            for (int i = 0; i < max; i++)
            {
                user.Index = i;
                proxy.Invoke("joinRoom", user).Wait();
            }

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

            for (int i = max; i < 2 * max; i++)
            {
                user.Index = i;
                proxy.Invoke("joinRoom", user).Wait();
            }

            Assert.True(countDown.Wait(TimeSpan.FromSeconds(30)), "Didn't receive " + max + " messages. Got " + (max - countDown.Count) + " missed " + String.Join(",", countDown.Left.Select(i => i.ToString())));
            Assert.True(countDownAfterReconnect.Wait(TimeSpan.FromSeconds(30)), "Didn't receive " + max + " messages. Got " + (max - countDownAfterReconnect.Count) + " missed " + String.Join(",", countDownAfterReconnect.Left.Select(i => i.ToString())));

            connection.Stop();
        }
Esempio n. 31
0
        public void HubGroupsRejoinWhenAutoRejoiningGroupsEnabled(HostType hostType, TransportType transportType)
        {
            using (var host = CreateHost(hostType, transportType))
            {
                host.Initialize(keepAlive: null,
                                connectionTimeout: 5,
                                hearbeatInterval: 2,
                                enableAutoRejoiningGroups: true);

                int max = 10;

                var countDown = new CountDownRange <int>(Enumerable.Range(0, max));
                var countDownAfterReconnect = new CountDownRange <int>(Enumerable.Range(max, max));
                var connection = new Client.Hubs.HubConnection(host.Url);
                var proxy      = connection.CreateHubProxy("MultGroupHub");

                proxy.On <User>("onRoomJoin", u =>
                {
                    if (u.Index < max)
                    {
                        Assert.True(countDown.Mark(u.Index));
                    }
                    else
                    {
                        Assert.True(countDownAfterReconnect.Mark(u.Index));
                    }
                });

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

                var user = new User {
                    Name = "tester"
                };
                proxy.InvokeWithTimeout("login", user);

                for (int i = 0; i < max; i++)
                {
                    user.Index = i;
                    proxy.InvokeWithTimeout("joinRoom", user);
                }

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

                for (int i = max; i < 2 * max; i++)
                {
                    user.Index = i;
                    proxy.InvokeWithTimeout("joinRoom", user);
                }

                Assert.True(countDown.Wait(TimeSpan.FromSeconds(30)), "Didn't receive " + max + " messages. Got " + (max - countDown.Count) + " missed " + String.Join(",", countDown.Left.Select(i => i.ToString())));
                Assert.True(countDownAfterReconnect.Wait(TimeSpan.FromSeconds(30)), "Didn't receive " + max + " messages. Got " + (max - countDownAfterReconnect.Count) + " missed " + String.Join(",", countDownAfterReconnect.Left.Select(i => i.ToString())));

                connection.Stop();
            }
        }
Esempio n. 32
0
        public static IDisposable StressGroups(int max = 100)
        {
            using (var host = new MemoryHost())
            {
                host.HubPipeline.EnableAutoRejoiningGroups();
                host.MapHubs();
                host.Configuration.HeartBeatInterval = TimeSpan.FromSeconds(5);
                host.Configuration.KeepAlive         = TimeSpan.FromSeconds(5);

                var countDown  = new CountDownRange <int>(Enumerable.Range(0, max));
                var connection = new Client.Hubs.HubConnection("http://foo");
                var proxy      = connection.CreateHubProxy("HubWithGroups");

                proxy.On <int>("Do", i =>
                {
                    if (!countDown.Mark(i))
                    {
                        Debugger.Break();
                    }
                });

                try
                {
                    connection.Start(host).Wait();

                    proxy.Invoke("Join", "foo").Wait();

                    for (int i = 0; i < max; i++)
                    {
                        proxy.Invoke("Send", "foo", i).Wait();
                    }

                    proxy.Invoke("Leave", "foo").Wait();

                    for (int i = max + 1; i < max + 50; i++)
                    {
                        proxy.Invoke("Send", "foo", i).Wait();
                    }

                    if (!countDown.Wait(TimeSpan.FromSeconds(10)))
                    {
                        Console.WriteLine("Didn't receive " + max + " messages. Got " + (max - countDown.Count) + " missed " + String.Join(",", countDown.Left.Select(i => i.ToString())));
                        Debugger.Break();
                    }
                }
                finally
                {
                    connection.Stop();
                }
            }

            return(new DisposableAction(() => { }));
        }
Esempio n. 33
0
        public void VerifyOwinContext(HostType hostType, TransportType transportType)
        {
            using (var host = CreateHost(hostType, transportType))
            {
                host.Initialize();

                var connection  = new Client.Hubs.HubConnection(host.Url);
                var connection2 = new Client.Hubs.HubConnection(host.Url);

                var hub  = connection.CreateHubProxy("MyItemsHub");
                var hub1 = connection2.CreateHubProxy("MyItemsHub");

                var results = new List <RequestItemsResponse>();
                hub1.On <RequestItemsResponse>("update", result =>
                {
                    if (!results.Contains(result))
                    {
                        results.Add(result);
                    }
                });

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

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

                hub1.InvokeWithTimeout("GetItems");

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

                connection.Stop();

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

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

                Assert.Equal(3, results.Count);
                Assert.Equal("OnConnected", results[0].Method);
                Assert.Equal(1, results[0].Keys.Length);
                Assert.Equal("owin.environment", results[0].Keys[0]);
                Assert.Equal("GetItems", results[1].Method);
                Assert.Equal(1, results[1].Keys.Length);
                Assert.Equal("owin.environment", results[1].Keys[0]);
                Assert.Equal("OnDisconnected", results[2].Method);
                Assert.Equal(1, results[2].Keys.Length);
                Assert.Equal("owin.environment", results[2].Keys[0]);

                connection2.Stop();
            }
        }
Esempio n. 34
0
        public void ComplexPersonState()
        {
            using (var host = new MemoryHost())
            {
                host.MapHubs();
                var connection = new Client.Hubs.HubConnection("http://site/");

                var hub = connection.CreateHubProxy("demo");

                var wh = new ManualResetEvent(false);

                connection.Start(host).Wait();

                var person = new SignalR.Samples.Hubs.DemoHub.DemoHub.Person
                {
                    Address = new SignalR.Samples.Hubs.DemoHub.DemoHub.Address
                    {
                        Street = "Redmond",
                        Zip    = "98052"
                    },
                    Age  = 25,
                    Name = "David"
                };

                var     person1 = hub.Invoke <SignalR.Samples.Hubs.DemoHub.DemoHub.Person>("ComplexType", person).Result;
                var     person2 = hub.GetValue <SignalR.Samples.Hubs.DemoHub.DemoHub.Person>("person");
                JObject obj     = ((dynamic)hub).person;
                var     person3 = obj.ToObject <SignalR.Samples.Hubs.DemoHub.DemoHub.Person>();

                Assert.NotNull(person1);
                Assert.NotNull(person2);
                Assert.NotNull(person3);
                Assert.Equal("David", person1.Name);
                Assert.Equal("David", person2.Name);
                Assert.Equal("David", person3.Name);
                Assert.Equal(25, person1.Age);
                Assert.Equal(25, person2.Age);
                Assert.Equal(25, person3.Age);
                Assert.Equal("Redmond", person1.Address.Street);
                Assert.Equal("Redmond", person2.Address.Street);
                Assert.Equal("Redmond", person3.Address.Street);
                Assert.Equal("98052", person1.Address.Zip);
                Assert.Equal("98052", person2.Address.Zip);
                Assert.Equal("98052", person3.Address.Zip);

                connection.Stop();
            }
        }
Esempio n. 35
0
        public void ComplexPersonState(HostType hostType, TransportType transportType)
        {
            using (var host = CreateHost(hostType, transportType))
            {
                host.Initialize();
                var connection = new Client.Hubs.HubConnection(host.Url);

                var hub = connection.CreateHubProxy("demo");

                var wh = new ManualResetEvent(false);

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

                var person = new SignalR.Samples.Hubs.DemoHub.DemoHub.Person
                {
                    Address = new SignalR.Samples.Hubs.DemoHub.DemoHub.Address
                    {
                        Street = "Redmond",
                        Zip    = "98052"
                    },
                    Age  = 25,
                    Name = "David"
                };

                var     person1 = hub.InvokeWithTimeout <SignalR.Samples.Hubs.DemoHub.DemoHub.Person>("ComplexType", person);
                var     person2 = hub.GetValue <SignalR.Samples.Hubs.DemoHub.DemoHub.Person>("person");
                JObject obj     = ((dynamic)hub).person;
                var     person3 = obj.ToObject <SignalR.Samples.Hubs.DemoHub.DemoHub.Person>();

                Assert.NotNull(person1);
                Assert.NotNull(person2);
                Assert.NotNull(person3);
                Assert.Equal("David", person1.Name);
                Assert.Equal("David", person2.Name);
                Assert.Equal("David", person3.Name);
                Assert.Equal(25, person1.Age);
                Assert.Equal(25, person2.Age);
                Assert.Equal(25, person3.Age);
                Assert.Equal("Redmond", person1.Address.Street);
                Assert.Equal("Redmond", person2.Address.Street);
                Assert.Equal("Redmond", person3.Address.Street);
                Assert.Equal("98052", person1.Address.Zip);
                Assert.Equal("98052", person2.Address.Zip);
                Assert.Equal("98052", person3.Address.Zip);

                connection.Stop();
            }
        }
Esempio n. 36
0
        public void UnsupportedOverloads(HostType hostType, TransportType transportType)
        {
            using (var host = CreateHost(hostType, transportType))
            {
                host.Initialize();
                var connection = new Client.Hubs.HubConnection(host.Url);

                var hub = connection.CreateHubProxy("demo");

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

                TestUtilities.AssertAggregateException <InvalidOperationException>(() => hub.InvokeWithTimeout("UnsupportedOverload", 13177), "'UnsupportedOverload' method could not be resolved.");

                connection.Stop();
            }
        }
Esempio n. 37
0
        public void UnsupportedOverloads()
        {
            var host = new MemoryHost();

            host.MapHubs();
            var connection = new Client.Hubs.HubConnection("http://foo/");

            var hub = connection.CreateHubProxy("demo");

            connection.Start(host).Wait();

            var ex = Assert.Throws <InvalidOperationException>(() => hub.Invoke("UnsupportedOverload", 13177).Wait());

            Assert.Equal("'UnsupportedOverload' method could not be resolved.", ex.GetBaseException().Message);
            connection.Stop();
        }
Esempio n. 38
0
        public void GenericTaskWithContinueWith()
        {
            var host = new MemoryHost();

            host.MapHubs();
            var connection = new Client.Hubs.HubConnection("http://foo/");

            var hub = connection.CreateHubProxy("demo");

            connection.Start(host).Wait();

            int result = hub.Invoke <int>("GenericTaskWithContinueWith").Result;

            Assert.Equal(4, result);
            connection.Stop();
        }
Esempio n. 39
0
        public void GetValueFromServer()
        {
            var host = new MemoryHost();

            host.MapHubs();
            var connection = new Client.Hubs.HubConnection("http://foo/");

            var hub = connection.CreateHubProxy("demo");

            connection.Start(host).Wait();

            var result = hub.Invoke <int>("GetValue").Result;

            Assert.Equal(10, result);
            connection.Stop();
        }
Esempio n. 40
0
        public void ComplexPersonState()
        {
            var host = new MemoryHost();
            host.MapHubs();
            var connection = new Client.Hubs.HubConnection("http://site/");

            var hub = connection.CreateHubProxy("demo");

            var wh = new ManualResetEvent(false);

            connection.Start(host).Wait();

            var person = new SignalR.Samples.Hubs.DemoHub.DemoHub.Person
            {
                Address = new SignalR.Samples.Hubs.DemoHub.DemoHub.Address
                {
                    Street = "Redmond",
                    Zip = "98052"
                },
                Age = 25,
                Name = "David"
            };

            var person1 = hub.Invoke<SignalR.Samples.Hubs.DemoHub.DemoHub.Person>("ComplexType", person).Result;
            var person2 = hub.GetValue<SignalR.Samples.Hubs.DemoHub.DemoHub.Person>("person");
            JObject obj = ((dynamic)hub).person;
            var person3 = obj.ToObject<SignalR.Samples.Hubs.DemoHub.DemoHub.Person>();

            Assert.NotNull(person1);
            Assert.NotNull(person2);
            Assert.NotNull(person3);
            Assert.Equal("David", person1.Name);
            Assert.Equal("David", person2.Name);
            Assert.Equal("David", person3.Name);
            Assert.Equal(25, person1.Age);
            Assert.Equal(25, person2.Age);
            Assert.Equal(25, person3.Age);
            Assert.Equal("Redmond", person1.Address.Street);
            Assert.Equal("Redmond", person2.Address.Street);
            Assert.Equal("Redmond", person3.Address.Street);
            Assert.Equal("98052", person1.Address.Zip);
            Assert.Equal("98052", person2.Address.Zip);
            Assert.Equal("98052", person3.Address.Zip);

            connection.Stop();
        }
Esempio n. 41
0
        public void DisconnectFiresForHubsWhenConnectionGoesAway()
        {
            using (var host = new MemoryHost())
            {
                var dr = new DefaultDependencyResolver();
                var configuration = dr.Resolve<IConfigurationManager>();

                var connectWh = new ManualResetEventSlim();
                var disconnectWh = new ManualResetEventSlim();
                host.Configure(app =>
                {
                    var config = new HubConfiguration
                    {
                        Resolver = dr
                    };

                    app.MapHubs("/signalr", config);

                    configuration.DisconnectTimeout = TimeSpan.FromSeconds(6);
                    dr.Register(typeof(MyHub), () => new MyHub(connectWh, disconnectWh));
                });

                var connection = new Client.Hubs.HubConnection("http://foo/");

                connection.CreateHubProxy("MyHub");

                // 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");
            }
        }
Esempio n. 42
0
        public void ReturningNullFromReconnectAccepted()
        {
            var mockHub = new Mock<SomeHub>() { CallBase = true };
            mockHub.Setup(h => h.OnReconnected()).Returns<Task>(null).Verifiable();

            using (var host = new MemoryHost())
            {
                host.HubPipeline.EnableAutoRejoiningGroups();
                host.Configuration.KeepAlive = null;
                host.Configuration.ConnectionTimeout = TimeSpan.FromSeconds(1);
                host.Configuration.HeartbeatInterval = TimeSpan.FromSeconds(1);
                host.DependencyResolver.Register(typeof(SomeHub), () => mockHub.Object);
                host.MapHubs();
                var connection = new Client.Hubs.HubConnection("http://foo");

                var hub = connection.CreateHubProxy("SomeHub");
                connection.Start(host).Wait();

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

                hub.InvokeWithTimeout("AllFoo");

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

                connection.Stop();

                mockHub.Verify();
            }
        }
Esempio n. 43
0
        public void SendToSelf(HostType hostType, TransportType transportType)
        {
            using (var host = CreateHost(hostType, transportType))
            {
                host.Initialize();

                var connection1 = new Client.Hubs.HubConnection(host.Url);
                var connection2 = new Client.Hubs.HubConnection(host.Url);

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

                var hub1 = connection1.CreateHubProxy("SendToSome");
                var hub2 = connection2.CreateHubProxy("SendToSome");

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

                hub1.On("send", wh1.Set);
                hub2.On("send", wh2.Set);

                hub1.InvokeWithTimeout("SendToSelf");

                Assert.True(wh1.Wait(TimeSpan.FromSeconds(10)));
                Assert.False(wh2.Wait(TimeSpan.FromSeconds(5)));

                connection1.Stop();
                connection2.Stop();
            }
        }
Esempio n. 44
0
        public void RejoiningGroupsOnlyReceivesGroupsBelongingToHub()
        {
            var logRejoiningGroups = new LogRejoiningGroupsModule();
            using (var host = new MemoryHost())
            {
                host.HubPipeline.AddModule(logRejoiningGroups);

                host.Configuration.KeepAlive = null;
                host.Configuration.ConnectionTimeout = TimeSpan.FromSeconds(1);
                host.Configuration.HeartbeatInterval = TimeSpan.FromSeconds(1);
                host.MapHubs();

                var connection = new Client.Hubs.HubConnection("http://foo");
                var proxy = connection.CreateHubProxy("MultGroupHub");
                var proxy2 = connection.CreateHubProxy("MultGroupHub2");

                connection.Start(host).Wait();

                var user = new User { Name = "tester" };
                proxy.InvokeWithTimeout("login", user);
                proxy2.InvokeWithTimeout("login", user);

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

                proxy.InvokeWithTimeout("joinRoom", user);
                proxy2.InvokeWithTimeout("joinRoom", user);

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

                Assert.True(logRejoiningGroups.GroupsRejoined["MultGroupHub"].Contains("foo"));
                Assert.True(logRejoiningGroups.GroupsRejoined["MultGroupHub"].Contains("tester"));
                Assert.False(logRejoiningGroups.GroupsRejoined["MultGroupHub"].Contains("foo2"));
                Assert.False(logRejoiningGroups.GroupsRejoined["MultGroupHub"].Contains("tester2"));
                Assert.True(logRejoiningGroups.GroupsRejoined["MultGroupHub2"].Contains("foo2"));
                Assert.True(logRejoiningGroups.GroupsRejoined["MultGroupHub2"].Contains("tester2"));
                Assert.False(logRejoiningGroups.GroupsRejoined["MultGroupHub2"].Contains("foo"));
                Assert.False(logRejoiningGroups.GroupsRejoined["MultGroupHub2"].Contains("tester"));

                connection.Stop();
            }
        }
Esempio n. 45
0
        public void CreatedHubsGetDisposed()
        {
            var mockHubs = new List<Mock<IHub>>();

            using (var host = new MemoryHost())
            {
                host.DependencyResolver.Register(typeof(IHub), () =>
                {
                    var mockHub = new Mock<IHub>() { CallBase = true };

                    mockHubs.Add(mockHub);
                    return mockHub.Object;
                });
                host.MapHubs();
                var connection = new Client.Hubs.HubConnection("http://foo/");

                var hub = connection.CreateHubProxy("demo");

                connection.Start(host).Wait();

                var result = hub.InvokeWithTimeout<string>("ReadStateValue");

                foreach (var mockDemoHub in mockHubs)
                {
                    mockDemoHub.Verify(d => d.Dispose(), Times.Once());
                }

                connection.Stop();
            }
        }
Esempio n. 46
0
        public void CreateProxyAfterConnectionStartsThrows(HostType hostType, TransportType transportType)
        {
            using (var host = CreateHost(hostType, transportType))
            {
                host.Initialize();
                var connection = new Client.Hubs.HubConnection(host.Url);

                try
                {
                    connection.Start(host.Transport).Wait();
                    Assert.Throws<InvalidOperationException>(() => connection.CreateHubProxy("demo"));
                }
                finally
                {
                    connection.Stop();
                }
            }
        }
Esempio n. 47
0
        public void ComplexPersonState(HostType hostType, TransportType transportType)
        {
            using (var host = CreateHost(hostType, transportType))
            {
                host.Initialize();
                var connection = new Client.Hubs.HubConnection(host.Url);

                var hub = connection.CreateHubProxy("demo");

                var wh = new ManualResetEvent(false);

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

                var person = new SignalR.Samples.Hubs.DemoHub.DemoHub.Person
                {
                    Address = new SignalR.Samples.Hubs.DemoHub.DemoHub.Address
                    {
                        Street = "Redmond",
                        Zip = "98052"
                    },
                    Age = 25,
                    Name = "David"
                };

                var person1 = hub.InvokeWithTimeout<SignalR.Samples.Hubs.DemoHub.DemoHub.Person>("ComplexType", person);
                var person2 = hub.GetValue<SignalR.Samples.Hubs.DemoHub.DemoHub.Person>("person");
                JObject obj = ((dynamic)hub).person;
                var person3 = obj.ToObject<SignalR.Samples.Hubs.DemoHub.DemoHub.Person>();

                Assert.NotNull(person1);
                Assert.NotNull(person2);
                Assert.NotNull(person3);
                Assert.Equal("David", person1.Name);
                Assert.Equal("David", person2.Name);
                Assert.Equal("David", person3.Name);
                Assert.Equal(25, person1.Age);
                Assert.Equal(25, person2.Age);
                Assert.Equal(25, person3.Age);
                Assert.Equal("Redmond", person1.Address.Street);
                Assert.Equal("Redmond", person2.Address.Street);
                Assert.Equal("Redmond", person3.Address.Street);
                Assert.Equal("98052", person1.Address.Zip);
                Assert.Equal("98052", person2.Address.Zip);
                Assert.Equal("98052", person3.Address.Zip);

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

                var connection = new Client.Hubs.HubConnection(host.Url, "a=b");

                var hub = connection.CreateHubProxy("CustomQueryHub");

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

                var result = hub.InvokeWithTimeout<string>("GetQueryString", "a");

                Assert.Equal("b", result);

                connection.Stop();
            }
        }
Esempio n. 49
0
        public void TaskWithException(HostType hostType, TransportType transportType)
        {
            using (var host = CreateHost(hostType, transportType))
            {
                host.Initialize();
                var connection = new Client.Hubs.HubConnection(host.Url);

                var hub = connection.CreateHubProxy("demo");

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

                var ex = Assert.Throws<AggregateException>(() => hub.InvokeWithTimeout("TaskWithException"));

                Assert.IsType<InvalidOperationException>(ex.GetBaseException());
                Assert.Contains("System.Exception", ex.GetBaseException().Message);
                connection.Stop();
            }
        }
Esempio n. 50
0
        public void SendToSpecificClientFromOutsideOfHub()
        {
            using (var host = new MemoryHost())
            {
                host.MapHubs();
                var connection1 = new Client.Hubs.HubConnection("http://foo/");
                var hubContext = host.ConnectionManager.GetHubContext("SendToSome");

                var wh1 = new ManualResetEventSlim(initialState: false);

                var hub1 = connection1.CreateHubProxy("SendToSome");

                connection1.Start(host).Wait();

                hub1.On("send", wh1.Set);

                hubContext.Clients.Client(connection1.ConnectionId).send();

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

                connection1.Stop();
            }
        }
Esempio n. 51
0
        public void GenericTaskWithContinueWith(HostType hostType, TransportType transportType)
        {
            using (var host = CreateHost(hostType, transportType))
            {
                host.Initialize();
                var connection = new Client.Hubs.HubConnection(host.Url);

                var hub = connection.CreateHubProxy("demo");

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

                int result = hub.InvokeWithTimeout<int>("GenericTaskWithContinueWith");

                Assert.Equal(4, result);
                connection.Stop();
            }
        }
Esempio n. 52
0
        public void DynamicInvokeTest(HostType hostType, TransportType transportType)
        {
            using (var host = CreateHost(hostType, transportType))
            {
                host.Initialize();
                var connection = new Client.Hubs.HubConnection(host.Url);

                string callback = @"!!!|\CallMeBack,,,!!!";

                var hub = connection.CreateHubProxy("demo");

                var wh = new ManualResetEventSlim(false);

                hub.On(callback, () => wh.Set());

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

                hub.InvokeWithTimeout("DynamicInvoke", callback);

                Assert.True(wh.Wait(TimeSpan.FromSeconds(10)));
                connection.Stop();
            }
        }
Esempio n. 53
0
        public void SettingState(HostType hostType, TransportType transportType)
        {
            using (var host = CreateHost(hostType, transportType))
            {
                host.Initialize();
                var connection = new Client.Hubs.HubConnection(host.Url);

                var hub = connection.CreateHubProxy("demo");
                connection.Start(host.Transport).Wait();

                var result = hub.InvokeWithTimeout<string>("SetStateValue", "test");

                Assert.Equal("test", result);
                Assert.Equal("test", hub["Company"]);

                connection.Stop();
            }
        }
Esempio n. 54
0
        public void GuidTest(HostType hostType, TransportType transportType)
        {
            using (var host = CreateHost(hostType, transportType))
            {
                host.Initialize();
                var connection = new Client.Hubs.HubConnection(host.Url);

                var hub = connection.CreateHubProxy("demo");

                var wh = new ManualResetEventSlim(false);

                hub.On<Guid>("TestGuid", id =>
                {
                    Assert.NotNull(id);
                    wh.Set();
                });

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

                hub.InvokeWithTimeout("TestGuid");

                Assert.True(wh.Wait(TimeSpan.FromSeconds(10)));
                connection.Stop();
            }
        }
Esempio n. 55
0
        private static void RunOne(MemoryHost host)
        {
            var connection = new Client.Hubs.HubConnection("http://foo");
            var proxy = connection.CreateHubProxy("HubWithGroups");
            var wh = new ManualResetEventSlim(false);

            proxy.On<int>("Do", i => wh.Set());

            try
            {
                connection.Start(host).Wait();

                proxy.Invoke("Join", "foo").Wait();

                proxy.Invoke("Send", "foo", 0).Wait();

                if (!wh.Wait(TimeSpan.FromSeconds(10)))
                {
                    Debugger.Break();
                }
            }
            finally
            {
                connection.Stop();
            }
        }
Esempio n. 56
0
        public void HubGroupsRejoinWhenAutoRejoiningGroupsEnabled(HostType hostType, TransportType transportType)
        {
            using (var host = CreateHost(hostType, transportType))
            {
                host.Initialize(keepAlive: null,
                                connectionTimeout: 5,
                                hearbeatInterval: 2,
                                enableAutoRejoiningGroups: true);

                int max = 10;

                var countDown = new CountDownRange<int>(Enumerable.Range(0, max));
                var countDownAfterReconnect = new CountDownRange<int>(Enumerable.Range(max, max));
                var connection = new Client.Hubs.HubConnection(host.Url);
                var proxy = connection.CreateHubProxy("MultGroupHub");

                proxy.On<User>("onRoomJoin", u =>
                {
                    if (u.Index < max)
                    {
                        Assert.True(countDown.Mark(u.Index));
                    }
                    else
                    {
                        Assert.True(countDownAfterReconnect.Mark(u.Index));
                    }
                });

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

                var user = new User { Name = "tester" };
                proxy.InvokeWithTimeout("login", user);

                for (int i = 0; i < max; i++)
                {
                    user.Index = i;
                    proxy.InvokeWithTimeout("joinRoom", user);
                }

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

                for (int i = max; i < 2 * max; i++)
                {
                    user.Index = i;
                    proxy.InvokeWithTimeout("joinRoom", user);
                }

                Assert.True(countDown.Wait(TimeSpan.FromSeconds(30)), "Didn't receive " + max + " messages. Got " + (max - countDown.Count) + " missed " + String.Join(",", countDown.Left.Select(i => i.ToString())));
                Assert.True(countDownAfterReconnect.Wait(TimeSpan.FromSeconds(30)), "Didn't receive " + max + " messages. Got " + (max - countDownAfterReconnect.Count) + " missed " + String.Join(",", countDownAfterReconnect.Left.Select(i => i.ToString())));

                connection.Stop();
            }
        }
Esempio n. 57
0
        public static IDisposable StressGroups(int max = 100)
        {
            using (var host = new MemoryHost())
            {
                host.HubPipeline.EnableAutoRejoiningGroups();
                host.MapHubs();
                host.Configuration.HeartBeatInterval = TimeSpan.FromSeconds(5);
                host.Configuration.KeepAlive = TimeSpan.FromSeconds(5);

                var countDown = new CountDownRange<int>(Enumerable.Range(0, max));
                var connection = new Client.Hubs.HubConnection("http://foo");
                var proxy = connection.CreateHubProxy("HubWithGroups");

                proxy.On<int>("Do", i =>
                {
                    if (!countDown.Mark(i))
                    {
                        Debugger.Break();
                    }
                });

                try
                {
                    connection.Start(host).Wait();

                    proxy.Invoke("Join", "foo").Wait();

                    for (int i = 0; i < max; i++)
                    {
                        proxy.Invoke("Send", "foo", i).Wait();
                    }

                    proxy.Invoke("Leave", "foo").Wait();

                    for (int i = max + 1; i < max + 50; i++)
                    {
                        proxy.Invoke("Send", "foo", i).Wait();
                    }

                    if (!countDown.Wait(TimeSpan.FromSeconds(10)))
                    {
                        Console.WriteLine("Didn't receive " + max + " messages. Got " + (max - countDown.Count) + " missed " + String.Join(",", countDown.Left.Select(i => i.ToString())));
                        Debugger.Break();
                    }
                }
                finally
                {
                    connection.Stop();
                }
            }

            return new DisposableAction(() => { });
        }
Esempio n. 58
0
        public void JoinAndSendToGroupRenamedHub(HostType hostType, TransportType transportType)
        {
            using (var host = CreateHost(hostType, transportType))
            {
                host.Initialize(enableAutoRejoiningGroups: true);

                var connection = new Client.Hubs.HubConnection(host.Url);
                var wh = new ManualResetEventSlim();

                var hub = connection.CreateHubProxy("groupChat");

                hub.On("send", wh.Set);

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

                hub.InvokeWithTimeout("Join", "Foo");

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

                hub.InvokeWithTimeout("Send", "Foo", "new test");

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

                connection.Stop();
            }
        }
Esempio n. 59
0
        public void GetValueFromServer(HostType hostType, TransportType transportType)
        {
            using (var host = CreateHost(hostType, transportType))
            {
                host.Initialize();
                var connection = new Client.Hubs.HubConnection(host.Url);

                var hub = connection.CreateHubProxy("demo");

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

                var result = hub.InvokeWithTimeout<int>("GetValue");

                Assert.Equal(10, result);
                connection.Stop();
            }
        }
Esempio n. 60
0
        public void Overloads(HostType hostType, TransportType transportType)
        {
            using (var host = CreateHost(hostType, transportType))
            {
                host.Initialize();
                var connection = new Client.Hubs.HubConnection(host.Url);

                var hub = connection.CreateHubProxy("demo");

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

                hub.InvokeWithTimeout("Overload");
                int n = hub.InvokeWithTimeout<int>("Overload", 1);

                Assert.Equal(1, n);
                connection.Stop();
            }
        }