public void StubFiberPendingTasksShouldAllowEnqueueOfCommandsWhenExecutingAllPending() { var sut = new StubFiber { ExecutePendingImmediately = false }; var fired1 = new object(); var fired2 = new object(); var fired3 = new object(); var actionMarkers = new List <object>(); Action command1 = delegate { actionMarkers.Add(fired1); sut.Enqueue(() => actionMarkers.Add(fired3)); }; Action command2 = () => actionMarkers.Add(fired2); sut.Enqueue(command1); sut.Enqueue(command2); sut.ExecuteAllPendingUntilEmpty(); Assert.AreEqual(new[] { fired1, fired2, fired3 }, actionMarkers.ToArray()); }
public void TestTwoFibers() { FiberTester.TestPubSubWExtraFiber(ThreadFiber.StartNew(), ThreadFiber.StartNew()); FiberTester.TestPubSubWExtraFiber(PoolFiber.StartNew(), ThreadFiber.StartNew()); FiberTester.TestPubSubWExtraFiber(PoolFiber.StartNew(), PoolFiber.StartNew()); FiberTester.TestPubSubWExtraFiber(PoolFiber.StartNew(), StubFiber.StartNew()); }
public void ShouldCompletelyClearPendingActionsBeforeExecutingNewActions() { var msgs = new List <int>(); var sut = new StubFiber { ExecutePendingImmediately = true }; var channel = new Channel <int>(); const int count = 4; channel.Subscribe(sut, delegate(int x) { if (x == count) { return; } channel.Publish(x + 1); msgs.Add(x); }); channel.Publish(0); Assert.AreEqual(count, msgs.Count); for (var i = 0; i < msgs.Count; i++) { Assert.AreEqual(i, msgs[i]); } }
public IFiber Build() { if (_executor == null) { _executor = new Executor(); } IFiber fiber; switch (_type) { case FiberType.Thread: fiber = new ThreadFiber(_executor, new TimerScheduler(), _queue, _name, true, _priority); break; case FiberType.Pool: fiber = new PoolFiber(_executor); break; case FiberType.Stub: fiber = new StubFiber(_executor); break; default: throw new ArgumentOutOfRangeException(); } return(fiber); }
public AsyncReqReplyServiceSpecs() { Reply = string.Empty; Replied = new ManualResetEvent(false); Console.WriteLine("Start client fiber"); Func <byte[], string> unmarshaller = x => Encoding.Unicode.GetString(x); Func <string, byte[]> marshaller = x => Encoding.Unicode.GetBytes(x); ServerFiber = PoolFiber.StartNew(); ServerContext = NetMQContext.Create(); Service = new AsyncRequestHandlerSocket <string, string>(ServerContext, "tcp://localhost:9997", unmarshaller, marshaller); Service.SetRequestHandler(ServerFiber, request => request.Reply(request.Request.ToUpper())); Console.WriteLine("Start service"); ClientFiber = StubFiber.StartNew(); ClientContext = NetMQContext.Create(); Client = new AsyncRequestSocket <string, string>(ClientContext, "tcp://localhost:9997", marshaller, unmarshaller); Console.WriteLine("Start client"); }
public void TestBatching() { FiberTester.TestBatching(ThreadFiber.StartNew()); FiberTester.TestBatching(PoolFiber.StartNew()); FiberTester.TestBatching(StubFiber.StartNew()); FiberTester.TestBatchingWithKey(ThreadFiber.StartNew()); FiberTester.TestBatchingWithKey(PoolFiber.StartNew()); FiberTester.TestBatchingWithKey(StubFiber.StartNew()); }
public RequestSocket(NetMQContext context, string address, Func <TRequest, byte[]> requestMarshaller, Func <byte[], TReply> replyUnmarshaller) { _requestMarshaller = requestMarshaller; _replyUnmarshaller = replyUnmarshaller; _socket = context.CreateSocket(ZmqSocketType.Req); _socket.Connect(address); _internalChannel.SetRequestHandler(StubFiber.StartNew(), InternalSendRequest); }
public void Cancel() { int executionCount = 0; Action action = () => executionCount++; var timer = new TimerAction(StubFiber.StartNew(), action, TimeSpan.FromMilliseconds(2)); Thread.Sleep(100); Assert.AreEqual(1, executionCount); timer.Dispose(); Thread.Sleep(150); Assert.AreEqual(1, executionCount); }
public void CanSubscribeToEvent() { bool triggered = false; var stub = StubFiber.StartNew(); var evt = new EventTester(); var dispose = stub.SubscribeToEvent <object>(evt, "Event", x => triggered = true); Assert.IsTrue(evt.IsAttached); evt.Invoke(); Assert.IsTrue(triggered); dispose.Dispose(); Assert.IsFalse(evt.IsAttached); }
public void CanSubscribeToEvent() { bool triggered = false; StubFiber stub = new StubFiber(); EventTester evt = new EventTester(); IDisposable dispose = stub.SubscribeToEvent(evt, "Event", () => triggered = true); Assert.IsTrue(evt.IsAttached); evt.Invoke(); Assert.IsTrue(triggered); dispose.Dispose(); Assert.IsFalse(evt.IsAttached); }
public void Offline() { var fiber = new StubFiber(); fiber.ExecutePendingImmediately = false; var queue = new HttpRequestQueue(); queue.MaxConcurrentRequests = 2; queue.ReconnectInterval = TimeSpan.FromMilliseconds(500); var state = new RequestState(); var state2 = new RequestState(); var state3 = new RequestState(); queue.Enqueue("http://localhost:8080?func=Forbidden", this.ResponseCallBack, state); queue.Enqueue("http://localhost:8080?func=Forbidden", this.ResponseCallBack, state2); queue.Enqueue("http://localhost:8080?func=Forbidden", this.ResponseCallBack, state3); Assert.IsTrue(state.ResetEvent.WaitOne(TimeOutMilliseconds)); Assert.AreEqual(HttpRequestQueueResultCode.Error, state.Result); Assert.IsTrue(state2.ResetEvent.WaitOne(TimeOutMilliseconds)); Assert.AreEqual(HttpRequestQueueResultCode.Offline, state2.Result); Assert.IsTrue(state3.ResetEvent.WaitOne(TimeOutMilliseconds)); Assert.AreEqual(HttpRequestQueueResultCode.Offline, state3.Result); Assert.AreEqual(HttpRequestQueueState.Offline, queue.QueueState); state = new RequestState(); queue.Enqueue("http://localhost:8080", this.ResponseCallBack, state); Assert.IsTrue(state.ResetEvent.WaitOne(TimeOutMilliseconds)); Assert.AreEqual(HttpRequestQueueResultCode.Offline, state.Result); Assert.AreEqual(HttpRequestQueueState.Offline, queue.QueueState); Thread.Sleep(750); state = new RequestState(); state2 = new RequestState(); queue.Enqueue("http://localhost:8080", this.ResponseCallBack, state); queue.Enqueue("http://localhost:8080", this.ResponseCallBack, state2); Assert.IsTrue(state.ResetEvent.WaitOne(TimeOutMilliseconds)); Assert.AreEqual(HttpRequestQueueResultCode.Success, state.Result); Assert.IsTrue(state2.ResetEvent.WaitOne(TimeOutMilliseconds)); Assert.AreEqual(HttpRequestQueueResultCode.Offline, state2.Result); Assert.AreEqual(HttpRequestQueueState.Running, queue.QueueState); }
public void PipelineTest1() { string value = null; int tvalue = 0; IStage <double, string> pipeline = new Stage <double, int>(d => (int)d) .Tap(i => tvalue = i) .Select(i => (i * 10).ToString()); StubFiber stub = new StubFiber(); pipeline.Subscribe(stub, s => value = s); pipeline.Publish(1.3); Thread.Sleep(100); Assert.AreEqual("10", value); Assert.AreEqual(1, tvalue); }
public void ChannelSubscription() { var fiber = StubFiber.StartNew(); var channel = new Channel <int>(); fiber.Subscribe(channel, i => { }); Assert.AreEqual(true, channel.HasSubscriptions()); fiber.Dispose(); Assert.AreEqual(false, channel.HasSubscriptions()); var sub = fiber.Subscribe(channel, i => { }); Assert.AreEqual(true, channel.HasSubscriptions()); sub.Dispose(); Assert.AreEqual(false, channel.HasSubscriptions()); }
public void PipelineTest1() { string value = null; int tvalue = 0; var stage1 = new Stage <double, int>(d => (int)d); var stage2 = new Tee <int>(i => tvalue = i); var stage3 = new Stage <int, string>(i => (i * 10).ToString()); var stub = StubFiber.StartNew(); var pipeline = stage1.To(stage2).To(stage3); pipeline.Subscribe(stub, s => value = s); stage1.Publish(1.3); Thread.Sleep(10); Assert.AreEqual("10", value); Assert.AreEqual(1, tvalue); }
public void CallbackFromIntervalTimerWithCancel() { int executionCount = 0; Action action = () => executionCount++; using (IFiber stubFiber = StubFiber.StartNew()) { var timer = new TimerAction(stubFiber, action, TimeSpan.FromMilliseconds(2), TimeSpan.FromMilliseconds(150)); Thread.Sleep(100); Assert.AreEqual(1, executionCount); timer.Dispose(); Thread.Sleep(150); Assert.AreEqual(1, executionCount); } }
public void ShouldIncreaseStubFiberSubscriberCountByOne() { var fiber = new StubFiber(); fiber.Start(); var channel = new Channel <int>(); Assert.AreEqual(0, fiber.NumSubscriptions); Assert.AreEqual(0, channel.NumSubscribers); channel.Subscribe(fiber, x => { }); Assert.AreEqual(1, fiber.NumSubscriptions); Assert.AreEqual(1, channel.NumSubscribers); fiber.Dispose(); Assert.AreEqual(0, fiber.NumSubscriptions); Assert.AreEqual(0, channel.NumSubscribers); }
public void DisposeShouldClearAllLists() { var sut = new StubFiber(); var channel = new Channel <int>(); channel.Subscribe(sut, x => { }); sut.Schedule(() => { }, 1000); channel.Publish(2); Assert.AreEqual(1, sut.Subscriptions.Count); Assert.AreEqual(1, sut.Scheduled.Count); Assert.AreEqual(1, sut.Pending.Count); sut.Dispose(); Assert.AreEqual(0, sut.Subscriptions.Count); Assert.AreEqual(0, sut.Scheduled.Count); Assert.AreEqual(0, sut.Pending.Count); }
public void MaxConcurrentRequests() { const int requestCount = 10; this.responseCount = 0; var fiber = new StubFiber(); fiber.ExecutePendingImmediately = false; var queue = new HttpRequestQueue(fiber); queue.MaxConcurrentRequests = 2; for (int i = 0; i < requestCount; i++) { queue.Enqueue("http://localhost:8080/", this.ResponseCallBack, null); } fiber.ExecuteAllPending(); Assert.AreEqual(1, queue.RunningRequestsCount); Assert.AreEqual(requestCount - 1, queue.QueuedRequestCount); var startTime = DateTime.UtcNow; var time = TimeSpan.FromMilliseconds(TimeOutMilliseconds * requestCount); while (this.responseCount < 10) { Assert.Less(DateTime.UtcNow.Subtract(startTime), time, "Received not all responses in the expected time"); if (this.responseCount > 0) { Assert.Less(queue.MaxConcurrentRequests, queue.MaxConcurrentRequests + 1); } fiber.ExecuteAllPending(); Thread.Sleep(50); } }
public void CallbackFromIntervalTimerWithCancel() { int executionCount = 0; void Action() { executionCount++; } using (StubFiber stubFiber = new StubFiber()) { TimerAction timer = new TimerAction(stubFiber, Action, TimeSpan.FromMilliseconds(2), TimeSpan.FromMilliseconds(150)); Thread.Sleep(100); Assert.AreEqual(1, executionCount); timer.Dispose(); Thread.Sleep(150); Assert.AreEqual(1, executionCount); } }
static void Main(string[] args) { //This example uses mutable messages, which require care. Normally you want to //default to using immutable messages for each stage. //Its possible to fan out certain stages to deal with the issue with pipelines only being //as fast as the slowest stage. You can use a Queue channel say between stage1 and stage2 and then //instantiate more than one stage2 processor. var channels = new Channels(); using (var stage1 = new ConcurrentComponent <Payload, Payload>(new Stage1(new SomeService()), channels.Input, channels.Stage1To2, channels.Errors)) using (var stage2 = new ConcurrentComponent <Payload, Payload>(new Stage2(new SomeDataAccess()), channels.Stage1To2, channels.Output, channels.Errors)) using (var stub = StubFiber.StartNew()) { channels.Output.Subscribe(stub, payload => Console.WriteLine("Got output")); channels.Stage1To2.Subscribe(stub, payload => Console.WriteLine("Monitoring Stage1to2 channel saw a message")); using (var timer = new ThreadSafeTimer()) { timer.Schedule(() => channels.Input.Publish(new Payload()), TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(2)); Console.WriteLine("Hit any key to stop"); Console.ReadKey(); } } }
public void ScheduledTasksShouldBeExecutedOnceScheduleIntervalShouldBeExecutedEveryTimeExecuteScheduleAllIsCalled() { var sut = new StubFiber(); var scheduleFired = 0; var scheduleOnIntervalFired = 0; sut.Schedule(() => scheduleFired++, 100); var intervalSub = sut.ScheduleOnInterval(() => scheduleOnIntervalFired++, 100, 100); sut.ExecuteAllScheduled(); Assert.AreEqual(1, scheduleFired); Assert.AreEqual(1, scheduleOnIntervalFired); sut.ExecuteAllScheduled(); Assert.AreEqual(1, scheduleFired); Assert.AreEqual(2, scheduleOnIntervalFired); intervalSub.Dispose(); sut.ExecuteAllScheduled(); Assert.AreEqual(1, scheduleFired); Assert.AreEqual(2, scheduleOnIntervalFired); }
public void InOrderExecution() { FiberTester.InOrderExecution(ThreadFiber.StartNew()); FiberTester.InOrderExecution(PoolFiber.StartNew()); FiberTester.InOrderExecution(StubFiber.StartNew()); }
public void TestPubSubSimple() { FiberTester.TestPubSubSimple(ThreadFiber.StartNew()); FiberTester.TestPubSubSimple(PoolFiber.StartNew()); FiberTester.TestPubSubSimple(StubFiber.StartNew()); }
public void TestPubSubWithFilter() { FiberTester.TestPubSubWithFilter(ThreadFiber.StartNew()); FiberTester.TestPubSubWithFilter(PoolFiber.StartNew()); FiberTester.TestPubSubWithFilter(StubFiber.StartNew()); }
public void TestReqReply() { FiberTester.TestReqReply1(ThreadFiber.StartNew()); FiberTester.TestReqReply1(PoolFiber.StartNew()); FiberTester.TestReqReply1(StubFiber.StartNew()); }