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 static IDisposable StressGroups(int max = 100) { var host = new MemoryHost(); host.Configure(app => { var config = new HubConfiguration() { Resolver = new DefaultDependencyResolver() }; app.MapSignalR(config); var configuration = config.Resolver.Resolve<IConfigurationManager>(); // The below effectively sets the heartbeat interval to five seconds. configuration.KeepAlive = TimeSpan.FromSeconds(10); }); var countDown = new CountDownRange<int>(Enumerable.Range(0, max)); var connection = new HubConnection("http://foo"); var proxy = connection.CreateHubProxy("HubWithGroups"); proxy.On<int>("Do", i => { if (!countDown.Mark(i)) { Debugger.Break(); } }); try { connection.Start(new Client.Transports.LongPollingTransport(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 host; }
public void SubscriptionWithMultipleExistingCursors() { var dr = new DefaultDependencyResolver(); var passThroughMinfier = new PassThroughStringMinifier(); dr.Register(typeof(IStringMinifier), () => passThroughMinfier); using (var bus = new MessageBus(dr)) { Func <ISubscriber> subscriberFactory = () => new TestSubscriber(new[] { "key", "key2" }); var cdKey = new CountDownRange <int>(Enumerable.Range(2, 4)); var cdKey2 = new CountDownRange <int>(new[] { 1, 2, 10 }); IDisposable subscription = null; // Pretend like we had an initial subscription bus.Subscribe(subscriberFactory(), null, (result, state) => TaskAsyncHelper.True, 10, null) .Dispose(); // This simulates a reconnect bus.Publish("test", "key", "1").Wait(); bus.Publish("test", "key", "2").Wait(); bus.Publish("test", "key", "3").Wait(); bus.Publish("test", "key", "4").Wait(); bus.Publish("test", "key2", "1").Wait(); bus.Publish("test", "key2", "2").Wait(); try { subscription = bus.Subscribe(subscriberFactory(), "key,00000001|key2,00000000", (result, state) => { foreach (var m in result.GetMessages()) { int n = Int32.Parse(m.GetString()); if (m.Key == "key") { Assert.True(cdKey.Mark(n)); } else { Assert.True(cdKey2.Mark(n)); } } return(TaskAsyncHelper.True); }, 10, null); bus.Publish("test", "key", "5"); bus.Publish("test", "key2", "10"); Assert.True(cdKey.Wait(TimeSpan.FromSeconds(5))); Assert.True(cdKey2.Wait(TimeSpan.FromSeconds(5))); } finally { if (subscription != null) { subscription.Dispose(); } } } }
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(10)), "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 SubscriptionWithExistingCursor() { var dr = new DefaultDependencyResolver(); using (var bus = new TestScaleoutBus(dr, topicCount: 2)) { var subscriber = new TestSubscriber(new[] { "key" }); var cd = new CountDownRange <int>(Enumerable.Range(2, 4)); IDisposable subscription = null; // test, test2 = 1 // test1, test3 = 0 // // Cursor 1, 1 bus.SendMany(new[] { new Message("test", "key", "1"), new Message("test", "key", "50") }); // Cursor 0,1|1,1 bus.SendMany(new[] { new Message("test1", "key", "51") }); bus.SendMany(new[] { new Message("test2", "key", "2"), new Message("test3", "key", "3"), new Message("test2", "key", "4"), }); try { subscription = bus.Subscribe(subscriber, "0,00000001|1,00000001", result => { foreach (var m in result.GetMessages()) { int n = Int32.Parse(m.Value); Assert.True(cd.Mark(n)); } return(TaskAsyncHelper.True); }, 10); bus.SendMany(new[] { new Message("test", "key", "5") }); Assert.True(cd.Wait(TimeSpan.FromSeconds(10))); } finally { if (subscription != null) { subscription.Dispose(); } } } }
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.CreateProxy("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(3)), "Didn't receive " + max + " messages. Got " + (max - countDown.Count) + " missed " + String.Join(",", countDown.Left.Select(i => i.ToString()))); Assert.True(countDownAfterReconnect.Wait(TimeSpan.FromSeconds(3)), "Didn't receive " + max + " messages. Got " + (max - countDownAfterReconnect.Count) + " missed " + String.Join(",", countDownAfterReconnect.Left.Select(i => i.ToString()))); connection.Stop(); }
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(() => { })); }
public void SubscriptionWithExistingCursor() { var dr = new DefaultDependencyResolver(); using (var bus = new TestScaleoutBus(dr, streams: 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 SubscriptionWithMultipleExistingCursors() { var dr = new DefaultDependencyResolver(); var bus = new MessageBus(dr); var subscriber = new Subscriber(new[] { "key", "key2" }); var cdKey = new CountDownRange <int>(Enumerable.Range(2, 4)); var cdKey2 = new CountDownRange <int>(new[] { 1, 2, 10 }); IDisposable subscription = null; bus.Publish("test", "key", "1").Wait(); bus.Publish("test", "key", "2").Wait(); bus.Publish("test", "key", "3").Wait(); bus.Publish("test", "key", "4").Wait(); bus.Publish("test", "key2", "1").Wait(); bus.Publish("test", "key2", "2").Wait(); try { subscription = bus.Subscribe(subscriber, "key,00000001|key2,00000000", result => { foreach (var m in EnumerateMessages(result)) { int n = Int32.Parse(m.Value); if (m.Key == "key") { Assert.True(cdKey.Mark(n)); } else { Assert.True(cdKey2.Mark(n)); } } return(TaskAsyncHelper.True); }, 10); bus.Publish("test", "key", "5"); bus.Publish("test", "key2", "10"); Assert.True(cdKey.Wait(TimeSpan.FromSeconds(5))); Assert.True(cdKey2.Wait(TimeSpan.FromSeconds(5))); } finally { if (subscription != null) { subscription.Dispose(); } } }
public void SubscriptionWithExistingCursor() { var dr = new DefaultDependencyResolver(); var passThroughMinfier = new PassThroughStringMinifier(); dr.Register(typeof(IStringMinifier), () => passThroughMinfier); using (var bus = new MessageBus(dr)) { 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 dr = new DefaultDependencyResolver(); using (var bus = new MessageBus(dr)) { 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 => { foreach (var m in result.GetMessages()) { int n = Int32.Parse(m.Value); Assert.True(prev < n, "out of order"); prev = n; Assert.True(cd.Mark(n)); } return(TaskAsyncHelper.True); }, 10); 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(); } } } }
public void SubscriptionWithExistingCursor() { var dr = new DefaultDependencyResolver(); var passThroughMinfier = new PassThroughStringMinifier(); dr.Register(typeof(IStringMinifier), () => passThroughMinfier); using (var bus = new MessageBus(dr)) { var subscriber = new TestSubscriber(new[] { "key" }); var cd = new CountDownRange <int>(Enumerable.Range(2, 4)); IDisposable subscription = null; 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(subscriber, "key,00000001", result => { foreach (var m in result.GetMessages()) { int n = Int32.Parse(m.Value); Assert.True(cd.Mark(n)); } return(TaskAsyncHelper.True); }, 10); bus.Publish("test", "key", "5"); Assert.True(cd.Wait(TimeSpan.FromSeconds(5))); } finally { if (subscription != null) { subscription.Dispose(); } } } }
public void AddingEventAndSendingMessages() { var dr = new DefaultDependencyResolver(); using (var bus = new MessageBus(dr)) { 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 => { foreach (var m in result.GetMessages()) { int n = Int32.Parse(m.Value); Assert.True(prev < n, "out of order"); prev = n; Assert.True(cd.Mark(n)); } return TaskAsyncHelper.True; }, 10); 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(); } } } }
public void SubscriptionWithMultipleExistingCursors() { var dr = new DefaultDependencyResolver(); var bus = new MessageBus(dr); var subscriber = new TestSubscriber(new[] { "key", "key2" }); var cdKey = new CountDownRange<int>(Enumerable.Range(2, 4)); var cdKey2 = new CountDownRange<int>(new[] { 1, 2, 10 }); IDisposable subscription = null; bus.Publish("test", "key", "1").Wait(); bus.Publish("test", "key", "2").Wait(); bus.Publish("test", "key", "3").Wait(); bus.Publish("test", "key", "4").Wait(); bus.Publish("test", "key2", "1").Wait(); bus.Publish("test", "key2", "2").Wait(); try { subscription = bus.Subscribe(subscriber, "key,00000001|key2,00000000", result => { foreach (var m in result.GetMessages()) { int n = Int32.Parse(m.Value); if (m.Key == "key") { Assert.True(cdKey.Mark(n)); } else { Assert.True(cdKey2.Mark(n)); } } return TaskAsyncHelper.True; }, 10); bus.Publish("test", "key", "5"); bus.Publish("test", "key2", "10"); Assert.True(cdKey.Wait(TimeSpan.FromSeconds(5))); Assert.True(cdKey2.Wait(TimeSpan.FromSeconds(5))); } finally { if (subscription != null) { subscription.Dispose(); } } }
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(() => { }); }
public void SubscriptionWithMultipleExistingCursors() { var dr = new DefaultDependencyResolver(); var passThroughMinfier = new PassThroughStringMinifier(); dr.Register(typeof(IStringMinifier), () => passThroughMinfier); using (var bus = new MessageBus(dr)) { Func<ISubscriber> subscriberFactory = () => new TestSubscriber(new[] { "key", "key2" }); var cdKey = new CountDownRange<int>(Enumerable.Range(2, 4)); var cdKey2 = new CountDownRange<int>(new[] { 1, 2, 10 }); 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(); // This simulates a reconnect bus.Publish("test", "key", "1").Wait(); bus.Publish("test", "key", "2").Wait(); bus.Publish("test", "key", "3").Wait(); bus.Publish("test", "key", "4").Wait(); bus.Publish("test", "key2", "1").Wait(); bus.Publish("test", "key2", "2").Wait(); try { subscription = bus.Subscribe(subscriberFactory(), prefix + "key,00000001|key2,00000000", (result, state) => { foreach (var m in result.GetMessages()) { int n = Int32.Parse(m.GetString()); if (m.Key == "key") { Assert.True(cdKey.Mark(n)); } else { Assert.True(cdKey2.Mark(n)); } } return TaskAsyncHelper.True; }, 10, null); bus.Publish("test", "key", "5"); bus.Publish("test", "key2", "10"); Assert.True(cdKey.Wait(TimeSpan.FromSeconds(5))); Assert.True(cdKey2.Wait(TimeSpan.FromSeconds(5))); } finally { if (subscription != null) { subscription.Dispose(); } } } }
public void SubscriptionWithExistingCursor() { var dr = new DefaultDependencyResolver(); var passThroughMinfier = new PassThroughStringMinifier(); dr.Register(typeof(IStringMinifier), () => passThroughMinfier); using (var bus = new MessageBus(dr)) { var subscriber = new TestSubscriber(new[] { "key" }); var cd = new CountDownRange<int>(Enumerable.Range(2, 4)); IDisposable subscription = null; 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(subscriber, "key,00000001", result => { foreach (var m in result.GetMessages()) { int n = Int32.Parse(m.Value); Assert.True(cd.Mark(n)); } return TaskAsyncHelper.True; }, 10); bus.Publish("test", "key", "5"); Assert.True(cd.Wait(TimeSpan.FromSeconds(5))); } finally { if (subscription != null) { subscription.Dispose(); } } } }
public void SubscriptionWithExistingCursor() { var sp = ServiceProviderHelper.CreateServiceProvider(services => { var passThroughMinfier = new PassThroughStringMinifier(); services.AddSingleton<IStringMinifier>(passThroughMinfier); }); using (var bus = (MessageBus)sp.GetRequiredService<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 SubscriptionWithExistingCursor() { var dr = new DefaultDependencyResolver(); using (var bus = new TestScaleoutBus(dr, streams: 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 dr = new DefaultDependencyResolver(); using (var bus = new TestScaleoutBus(dr, topicCount: 2)) { var subscriber = new TestSubscriber(new[] { "key" }); var cd = new CountDownRange<int>(Enumerable.Range(2, 4)); IDisposable subscription = null; // test, test2 = 1 // test1, test3 = 0 // // Cursor 1, 1 bus.SendMany(new[] { new Message("test", "key", "1"), new Message("test", "key", "50") }); // Cursor 0,1|1,1 bus.SendMany(new[] { new Message("test1", "key", "51") }); bus.SendMany(new[]{ new Message("test2", "key", "2"), new Message("test3", "key", "3"), new Message("test2", "key", "4"), }); try { subscription = bus.Subscribe(subscriber, "0,00000001|1,00000001", result => { foreach (var m in result.GetMessages()) { int n = Int32.Parse(m.Value); Assert.True(cd.Mark(n)); } return TaskAsyncHelper.True; }, 10); bus.SendMany(new[] { new Message("test", "key", "5") }); Assert.True(cd.Wait(TimeSpan.FromSeconds(10))); } finally { if (subscription != null) { subscription.Dispose(); } } } }
public void AddingEventAndSendingMessages() { var sp = ServiceProviderHelper.CreateServiceProvider(); using (var bus = (MessageBus)sp.GetRequiredService<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(); } } } }
public static IDisposable StressGroups(int max = 100) { var host = new MemoryHost(); 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 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(new Client.Transports.LongPollingTransport(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(host); }
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 HubGroupsRejoinWhenRejoiningGroupsOverridden() { var host = new MemoryHost(); 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.CreateProxy("RejoinMultGroupHub"); 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(); }