public void Schedule1000In1ms() { var queue = new SynchronousCommandQueue(); queue.Run(); var count = 0; var reset = new AutoResetEvent(false); Action one = delegate { count++; if (count == 1000) { reset.Set(); } }; using (var thread = new ThreadPoolScheduler()) { for (var i = 0; i < 1000; i++) { thread.Schedule(i, () => queue.Enqueue(one)); } Assert.IsTrue(reset.WaitOne(1200, false)); } }
public void Should_filter_out_unwanted_messages() { Channel<UserUpdate> channel = new SynchronousChannel<UserUpdate>(); UserUpdate update = new UserUpdate {LastActivity = DateTime.Now - 5.Minutes()}; CommandQueue queue = new SynchronousCommandQueue(); var future = new Future<UserUpdate>(); channel.Subscribe(queue, future.Complete, message => message.LastActivity > DateTime.Now); var result = channel.Publish(update); Assert.IsTrue(result); Assert.IsFalse(future.IsAvailable(1.Seconds())); }
public void TimeTilNext() { var queue = new SynchronousCommandQueue(); queue.Run(); Action action = () => Assert.Fail("Should not execute"); using (var timer = new ThreadPoolScheduler()) { long now = 0; long span = 0; timer.QueueEvent(new SingleEvent(500, () => queue.Enqueue(action), now)); Assert.IsTrue(timer.GetNextScheduledTime(ref span, 0)); Assert.AreEqual(500, span); Assert.IsTrue(timer.GetNextScheduledTime(ref span, 499)); Assert.AreEqual(1, span); Assert.IsFalse(timer.GetNextScheduledTime(ref span, 500)); Assert.AreEqual(0, span); } }
public void PubSubUnsubscribe() { Channel <string> channel = new Channel <string>(); SynchronousCommandQueue queue = new SynchronousCommandQueue(); bool received = false; Action <string> onReceive = delegate(string data) { Assert.AreEqual("hello", data); received = true; }; IUnsubscriber unsub = channel.Subscribe(queue, onReceive); Assert.IsTrue(channel.Publish("hello")); Assert.IsTrue(received); unsub.Dispose(); Assert.IsFalse(channel.Publish("hello")); unsub.Dispose(); }
public void PubSubFilterTest() { Channel <int> channel = new Channel <int>(); SynchronousCommandQueue queue = new SynchronousCommandQueue(); int received = 0; Action <int> onReceive = delegate(int data) { Assert.IsTrue(data % 2 == 0); received++; }; ChannelSubscription <int> subber = new ChannelSubscription <int>(queue, onReceive); subber.FilterOnProducerThread = delegate(int msg) { return(msg % 2 == 0); }; channel.SubscribeOnProducerThreads(subber); for (int i = 0; i <= 4; i++) { channel.Publish(i); } Assert.AreEqual(3, received); }
public void Should_schedule_events() { Channel<UserUpdate> channel = new SynchronousChannel<UserUpdate>(); var update = new UserUpdate {LastActivity = DateTime.Now - 5.Minutes()}; CommandQueue queue = new SynchronousCommandQueue(); var scheduler = new ThreadPoolScheduler(); var future = new Future<UserUpdate>(); channel.Subscribe(queue, future.Complete); scheduler.Schedule(1000, () => channel.Publish(update)); Thread.Sleep(500); Assert.IsFalse(future.IsAvailable(0.Seconds())); Assert.IsTrue(future.IsAvailable(1.Seconds())); }
public void Schedule() { var queue = new SynchronousCommandQueue(); var count = 0; var reset = new AutoResetEvent(false); Action one = () => Assert.AreEqual(0, count++); Action two = () => Assert.AreEqual(1, count++); Action three = delegate { Assert.AreEqual(2, count++); reset.Set(); }; using (var thread = new ThreadPoolScheduler()) { thread.Schedule(50, () => queue.Enqueue(three)); thread.Schedule(1, () => queue.Enqueue(one)); thread.Schedule(1, () => queue.Enqueue(two)); Assert.IsTrue(reset.WaitOne(10000, false)); } }
public void PubSub() { Channel<string> channel = new Channel<string>(); SynchronousCommandQueue queue = new SynchronousCommandQueue(); Assert.IsFalse(channel.Publish("hello")); bool received = false; Action<string> onReceive = delegate(string data) { Assert.AreEqual("hello", data); received = true; }; channel.Subscribe(queue, onReceive); Assert.IsTrue(channel.Publish("hello")); Assert.IsTrue(received); channel.ClearSubscribers(); Assert.IsFalse(channel.Publish("hello")); }
public void PubSubFilterTest() { Channel<int> channel = new Channel<int>(); SynchronousCommandQueue queue = new SynchronousCommandQueue(); int received = 0; Action<int> onReceive = delegate(int data) { Assert.IsTrue(data%2 == 0); received++; }; ChannelSubscription<int> subber = new ChannelSubscription<int>(queue, onReceive); subber.FilterOnProducerThread = delegate(int msg) { return msg%2 == 0; }; channel.SubscribeOnProducerThreads(subber); for (int i = 0; i <= 4; i++) { channel.Publish(i); } Assert.AreEqual(3, received); }