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(); } }
public void AddingToMultipleGroups() { var host = new MemoryHost(); host.MapHubs(); int max = 10; var countDown = new CountDownRange <int>(Enumerable.Range(0, max)); var connection = new Client.Hubs.HubConnection("http://foo"); var proxy = connection.CreateProxy("MultGroupHub"); proxy.On <User>("onRoomJoin", user => { Assert.True(countDown.Mark(user.Index)); }); connection.Start(host).Wait(); for (int i = 0; i < max; i++) { var user = new User { Index = i, Name = "tester", Room = "test" + i }; proxy.Invoke("login", user).Wait(); 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()))); connection.Stop(); }
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(); } }
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(); }
public void SubscriptionWithExistingCursor() { var sp = ServiceProviderHelper.CreateServiceProvider(); var ta = new TypeActivator(); using (var bus = ta.CreateInstance <TestScaleoutBus>(sp, 2)) { var subscriber = new TestSubscriber(new[] { "key" }); var cd = new CountDownRange <int>(Enumerable.Range(2, 4)); IDisposable subscription = null; bus.Publish(0, 0, new[] { new Message("test", "key", "1"), new Message("test", "key", "50") }); bus.Publish(1, 0, new[] { new Message("test1", "key", "51") }); bus.Publish(1, 2, new[] { new Message("test2", "key", "2"), new Message("test3", "key", "3"), new Message("test2", "key", "4"), }); try { subscription = bus.Subscribe(subscriber, "s-0,00000000|1,00000000", (result, state) => { foreach (var m in result.GetMessages()) { int n = Int32.Parse(m.GetString()); Assert.True(cd.Mark(n)); } return(TaskAsyncHelper.True); }, 10, null); bus.Publish(0, 2, new[] { new Message("test", "key", "5") }); Assert.True(cd.Wait(TimeSpan.FromSeconds(10))); } finally { if (subscription != null) { subscription.Dispose(); } } } }
public void SubscriptionWithExistingCursor() { var sp = ServiceProviderHelper.CreateServiceProvider(services => { var passThroughMinfier = new PassThroughStringMinifier(); services.AddInstance <IStringMinifier>(passThroughMinfier); }); using (var bus = (MessageBus)sp.GetService <IMessageBus>()) { Func <TestSubscriber> subscriberFactory = () => new TestSubscriber(new[] { "key" }); var cd = new CountDownRange <int>(Enumerable.Range(2, 4)); IDisposable subscription = null; string prefix = DefaultSubscription._defaultCursorPrefix; // Pretend like we had an initial subscription bus.Subscribe(subscriberFactory(), null, (result, state) => TaskAsyncHelper.True, 10, null) .Dispose(); bus.Publish("test", "key", "1").Wait(); bus.Publish("test", "key", "2").Wait(); bus.Publish("test", "key", "3").Wait(); bus.Publish("test", "key", "4").Wait(); try { subscription = bus.Subscribe(subscriberFactory(), prefix + "key,00000001", (result, state) => { foreach (var m in result.GetMessages()) { int n = Int32.Parse(m.GetString()); Assert.True(cd.Mark(n)); } return(TaskAsyncHelper.True); }, 10, null); bus.Publish("test", "key", "5"); Assert.True(cd.Wait(TimeSpan.FromSeconds(5))); } finally { if (subscription != null) { subscription.Dispose(); } } } }
public void AddingEventAndSendingMessages() { var sp = ServiceProviderHelper.CreateServiceProvider(); using (var bus = (MessageBus)sp.GetService <IMessageBus>()) { var subscriber = new TestSubscriber(new[] { "a" }); int max = 100; var cd = new CountDownRange <int>(Enumerable.Range(0, max)); int prev = -1; IDisposable subscription = null; try { subscription = bus.Subscribe(subscriber, null, (result, state) => { foreach (var m in result.GetMessages()) { int n = Int32.Parse(m.GetString()); Assert.True(n == prev + 1, "Expected " + (prev + 1)); prev = n; Assert.True(cd.Mark(n)); } return(TaskAsyncHelper.True); }, maxMessages: 10, state: null); for (int i = 0; i < max; i++) { subscriber.AddEvent("b"); bus.Publish("test", "b", i.ToString()).Wait(); } Assert.True(cd.Wait(TimeSpan.FromSeconds(10))); } finally { if (subscription != null) { subscription.Dispose(); } } } }