public void AddingToMultipleGroups() { var host = new MemoryHost(); host.MapHubs(); int max = 100; var countDown = new CountDown(max); var list = Enumerable.Range(0, max).ToList(); var connection = new Client.Hubs.HubConnection("http://foo"); var proxy = connection.CreateProxy("MultGroupHub"); proxy.On<User>("onRoomJoin", user => { lock (list) { list.Remove(user.Index); } countDown.Dec(); }); connection.Start(host).Wait(); for (int i = 0; i < max; i++) { proxy.Invoke("login", new User { Index = i, Name = "tester", Room = "test" + i }).Wait(); } Assert.True(countDown.Wait(TimeSpan.FromSeconds(10)), "Didn't receive " + max + " messages. Got " + (max - countDown.Count) + " missed " + String.Join(",", list.Select(i => i.ToString()))); connection.Stop(); }
private static void RunInMemoryHost() { var host = new MemoryHost(); host.MapConnection<MyConnection>("/echo"); var connection = new Connection("http://foo/echo"); connection.Received += data => { Console.WriteLine(data); }; connection.Start(host).Wait(); ThreadPool.QueueUserWorkItem(_ => { try { while (true) { connection.Send(DateTime.Now.ToString()); Thread.Sleep(2000); } } catch { } }); }
public void SendingBigData() { var host = new MemoryHost(); host.MapConnection<SampleConnection>("/echo"); var connection = new Connection("http://foo/echo"); var wh = new ManualResetEventSlim(); var n = 0; var target = 20; connection.Received += data => { n++; if (n == target) { wh.Set(); } }; connection.Start(host).Wait(); var conn = host.ConnectionManager.GetConnection<SampleConnection>(); for (int i = 0; i < target; ++i) { var node = new BigData(); conn.Broadcast(node).Wait(); Thread.Sleep(1000); } Assert.True(wh.Wait(TimeSpan.FromMinutes(1)), "Timed out"); }
public void GenericTaskWithException() { var host = new MemoryHost(); host.MapHubs(); var connection = new Client.Hubs.HubConnection("http://foo/"); var hub = connection.CreateProxy("demo"); connection.Start(host).Wait(); var ex = Assert.Throws<AggregateException>(() => hub.Invoke("GenericTaskWithException").Wait()); Assert.Equal("Exception of type 'System.Exception' was thrown.", ex.GetBaseException().Message); }
public void GetValueFromServer() { var host = new MemoryHost(); host.MapHubs(); var connection = new Client.Hubs.HubConnection("http://foo/"); var hub = connection.CreateProxy("demo"); connection.Start(host).Wait(); var result = hub.Invoke<int>("GetValue").Result; Assert.Equal(10, result); }
public void Overloads() { var host = new MemoryHost(); host.MapHubs(); var connection = new Client.Hubs.HubConnection("http://foo/"); var hub = connection.CreateProxy("demo"); connection.Start(host).Wait(); hub.Invoke("Overload").Wait(); int n = hub.Invoke<int>("Overload", 1).Result; Assert.Equal(1, n); }
public void SettingState() { var host = new MemoryHost(); host.MapHubs(); var connection = new Client.Hubs.HubConnection("http://foo/"); var hub = connection.CreateProxy("demo"); hub["name"] = "test"; connection.Start(host).Wait(); var result = hub.Invoke<string>("ReadStateValue").Result; Assert.Equal("test", result); }
public void GroupsAreNotReadOnConnectedAsync() { 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(10)); }
public void ComplexPersonState() { var host = new MemoryHost(); host.MapHubs(); var connection = new Client.Hubs.HubConnection("http://site/"); var hub = connection.CreateProxy("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(); }
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); } }
public void HubNamesAreNotCaseSensitive() { var host = new MemoryHost(); host.MapHubs(); var hubConnection = new HubConnection("http://fake"); IHubProxy proxy = hubConnection.CreateProxy("chatHub"); var wh = new ManualResetEvent(false); proxy.On("addMessage", data => { Assert.Equal("hello", data); wh.Set(); }); hubConnection.Start(host).Wait(); proxy.Invoke("Send", "hello").Wait(); Assert.True(wh.WaitOne(TimeSpan.FromSeconds(5))); }
public void EndToEndTest() { var host = new MemoryHost(); host.MapHubs(); var hubConnection = new HubConnection("http://fake"); IHubProxy proxy = hubConnection.CreateProxy("ChatHub"); var called = false; proxy.On("addMessage", data => { called = true; Assert.Equal("hello", data); }); hubConnection.Start(host).Wait(); proxy.Invoke("Send", "hello").Wait(); Assert.True(called); }
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"); }
public void ChangeHubUrl() { var host = new MemoryHost(); host.MapHubs("/foo"); var connection = new Client.Hubs.HubConnection("http://site/foo", useDefaultUrl: false); var hub = connection.CreateProxy("demo"); var wh = new ManualResetEvent(false); hub.On("signal", id => { Assert.NotNull(id); wh.Set(); }); connection.Start(host).Wait(); hub.Invoke("DynamicTask").Wait(); Assert.True(wh.WaitOne(TimeSpan.FromSeconds(5))); }
public void DisconnectFiresForHubsWhenConnectionGoesAway() { 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/"); // Maximum wait time for disconnect to fire (2 heart beat intervals) var disconnectWait = host.Configuration.HeartBeatInterval + host.Configuration.HeartBeatInterval; connection.Start(host).Wait(); Assert.True(connectWh.Wait(TimeSpan.FromSeconds(100)), "Connect never fired"); connection.Stop(); Assert.True(disconnectWh.Wait(disconnectWait), "Disconnect never fired"); }
public void AuthenticatedAndAuthorizedUserCanInvokeMethodsInHubsAuthorizedWithRoles() { 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.CreateProxy("AdminAuthHub"); 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(); }
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(10)); 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]); }
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]); }
public void DynamicInvokeTest() { var host = new MemoryHost(); host.MapHubs(); var connection = new Client.Hubs.HubConnection("http://site/"); string callback = "!!!CallMeBack!!!"; var hub = connection.CreateProxy("demo"); var wh = new ManualResetEvent(false); hub.On(callback, () => wh.Set()); connection.Start(host).Wait(); hub.Invoke("DynamicInvoke", callback).Wait(); Assert.True(wh.WaitOne(TimeSpan.FromSeconds(5))); connection.Stop(); }
public void TaskWithException() { var host = new MemoryHost(); host.MapHubs(); var connection = new Client.Hubs.HubConnection("http://foo/"); var hub = connection.CreateProxy("demo"); connection.Start(host).Wait(); var ex = Assert.Throws<AggregateException>(() => hub.Invoke("TaskWithException").Wait()); Assert.IsType<InvalidOperationException>(ex.GetBaseException()); Assert.Contains("System.Exception", ex.GetBaseException().Message); connection.Stop(); }
public void UnsupportedOverloads() { var host = new MemoryHost(); host.MapHubs(); var connection = new Client.Hubs.HubConnection("http://foo/"); var hub = connection.CreateProxy("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(); }
private static MemoryHost RunMemoryHost() { var host = new MemoryHost(); host.MapConnection<StressConnection>("/echo"); string payload = GetPayload(); MeasureStats((MessageBus)host.DependencyResolver.Resolve<IMessageBus>()); Action<PersistentResponse> handler = (r) => { Interlocked.Add(ref _received, r.TotalCount); Interlocked.Add(ref _avgLastReceivedCount, r.TotalCount); }; LongPollingTransport.SendingResponse += handler; ForeverFrameTransport.SendingResponse += handler; for (int i = 0; i < _clients; i++) { ThreadPool.QueueUserWorkItem(state => { Interlocked.Increment(ref _clientsRunning); string connectionId = state.ToString(); //LongPollingLoop(host, connectionId); ProcessRequest(host, "serverSentEvents", connectionId); }, i); } for (var i = 1; i <= _senders; i++) { ThreadPool.QueueUserWorkItem(_ => { var context = host.ConnectionManager.GetConnectionContext<StressConnection>(); StartSendLoop(i.ToString(), (source, key, value) => context.Connection.Broadcast(value), payload); }); } return host; }
public void GuidTest() { var host = new MemoryHost(); host.MapHubs(); var connection = new Client.Hubs.HubConnection("http://site/"); var hub = connection.CreateProxy("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(); }
private static void LongPollingLoop(MemoryHost host, string connectionId) { LongPoll: var task = ProcessRequest(host, "longPolling", connectionId); if (task.IsCompleted) { task.Wait(); goto LongPoll; } task.ContinueWith(t => LongPollingLoop(host, connectionId)); }
private static Task ProcessRequest(MemoryHost host, string transport, string connectionId) { return host.ProcessRequest("http://foo/echo/connect?transport=" + transport + "&connectionId=" + connectionId, request => { }, null); }
public static void StressGroups() { var host = new MemoryHost(); host.HubPipeline.EnableAutoRejoiningGroups(); host.MapHubs(); int max = 15; var countDown = new CountDown(max); var list = Enumerable.Range(0, max).ToList(); var connection = new Client.Hubs.HubConnection("http://foo"); var proxy = connection.CreateProxy("MultGroupHub"); var bus = (MessageBus)host.DependencyResolver.Resolve<IMessageBus>(); proxy.On<int>("Do", i => { lock (list) { if (!list.Remove(i)) { Debugger.Break(); } } countDown.Dec(); }); try { connection.Start(host).Wait(); for (int i = 0; i < max; i++) { proxy.Invoke("Do", i).Wait(); } int retry = 3; bool result = false; do { result = countDown.Wait(TimeSpan.FromSeconds(10)); if (!result) { Console.WriteLine("Didn't receive " + max + " messages. Got " + (max - countDown.Count) + " missed (" + String.Join(",", list.Select(i => i.ToString())) + ")"); Console.WriteLine("A=" + bus.AllocatedWorkers + " B=" + bus.BusyWorkers); countDown.Reset(); } retry--; } while (retry > 0); if (!result) { Console.WriteLine("A=" + bus.AllocatedWorkers + " B=" + bus.BusyWorkers); Debugger.Break(); } } finally { connection.Stop(); host.Dispose(); GC.Collect(); GC.WaitForPendingFinalizers(); } }
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]); }
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(); }
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); }
public static void StressGroups() { var host = new MemoryHost(); host.MapHubs(); int max = 15; var countDown = new CountDown(max); var list = Enumerable.Range(0, max).ToList(); var connection = new Client.Hubs.HubConnection("http://foo"); var proxy = connection.CreateProxy("MultGroupHub"); proxy.On<int>("Do", i => { lock (list) { if (!list.Remove(i)) { Debugger.Break(); } } countDown.Dec(); }); try { connection.Start(new Client.Transports.ServerSentEventsTransport(host)).Wait(); for (int i = 0; i < max; i++) { proxy.Invoke("Do", i).Wait(); } if (!countDown.Wait(TimeSpan.FromSeconds(10))) { Console.WriteLine("Didn't receive " + max + " messages. Got " + (max - countDown.Count) + " missed (" + String.Join(",", list.Select(i => i.ToString())) + ")"); var bus = host.DependencyResolver.Resolve<INewMessageBus>(); Debugger.Break(); } } finally { connection.Stop(); host.Dispose(); GC.Collect(); GC.WaitForPendingFinalizers(); } }