public void SingleConsumer() { int oneConsumed = 0; using (IFiber one = PoolFiber.StartNew()) using (var reset = new AutoResetEvent(false)) { var channel = new QueueChannel <int>(); void OnMsg(int obj) { oneConsumed++; if (oneConsumed == 20) { reset.Set(); } } channel.Subscribe(one, OnMsg); for (int i = 0; i < 20; i++) { channel.Publish(i); } Assert.IsTrue(reset.WaitOne(10000, false)); } }
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 SingleConsumerWithException() { var failed = new List <Exception>(); var exec = new ExceptionHandlingExecutor(failed.Add); using (IFiber one = PoolFiber.StartNew(exec)) using (var reset = new AutoResetEvent(false)) { var channel = new QueueChannel <int>(); void OnMsg(int num) { if (num == 0) { throw new Exception(); } reset.Set(); } channel.Subscribe(one, OnMsg); channel.Publish(0); channel.Publish(1); Assert.IsTrue(reset.WaitOne(10000, false)); Assert.AreEqual(1, failed.Count); } }
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 Setup() { _pool1 = PoolFiber.StartNew(); _pool2 = new PoolFiber2(); _pool2.Start(); _spinPool = new SpinLockPoolFiber(); _spinPool.Start(); }
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 PushPullSocketPortSpecs() { Context1 = NetMQContext.Create(); Context2 = NetMQContext.Create(); RcvdSignal = new ManualResetEvent(false); ClientFiber = PoolFiber.StartNew(); Pull = new PullSocket <string>(Context1, "tcp://localhost:6002", x => Encoding.Unicode.GetString(x), Channel); Push = new SendSocket <string>(Context2, "tcp://localhost:6002", s => Encoding.Unicode.GetBytes(s), ZmqSocketType.Push, false); }
public void TestName() { IRequestChannel <int, int> channel = new RequestChannel <int, int>(); IFiber fiber1 = PoolFiber.StartNew(); channel.SetRequestHandler(fiber1, request => request.Reply(request.Request + 1)); using (var perfTimer = new PerfTimer(1000000)) { for (int i = 0; i < 1000000; i++) { channel.SendRequest(0); } } }
public PubSubSpecs() { ClientFiber = PoolFiber.StartNew(); Context1 = NetMQContext.Create(); Send = new SendSocket <string>(Context1, "tcp://localhost:6001", s => Encoding.Unicode.GetBytes(s)); Context2 = NetMQContext.Create(); RcvdSignal = new ManualResetEvent(false); Subscriber = new SubscribeSocket <string>(Context2, "tcp://localhost:6001", x => Encoding.Unicode.GetString(x), Channel); Subscriber.SubscribeAll(); }
private static void Main() { IChannel <string> toProcess = new Channel <string>(); IChannel <string> completed = new Channel <string>(); //first fiber will place something on channel for 2nd fiber to process using (IFiber fiber1 = PoolFiber.StartNew()) //2nd fiber just writes to the completed channel using (IDisposable processor = new ChannelAgent <string>(toProcess, s => completed.Publish("Received " + s))) //A logger that watches the completed channel. Could be optional using (IDisposable logger = new ChannelAgent <string>(completed, Console.WriteLine)) { int count = 0; //Start sending a message after a second, every 2 seconds... fiber1.Schedule(() => toProcess.Publish("Test" + count++), TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(2)); Console.ReadKey(); } }
public void MultiConsumerPool() { var queues = new List <IFiber>(); IChannel <string> channel = new QueueChannel <string>(); //Init executing Fibers for (int i = 0; i < 5; i++) { Action <string> onReceive = (message) => { var firstChar = message[0]; //Console.WriteLine("[{0}] {1}", Thread.CurrentThread.ManagedThreadId, message); }; IFiber threadFiber = PoolFiber.StartNew();//new ThreadFiber(new Executor(),new TimerScheduler(),new YieldingQueue() , i.ToString());//new DisruptorQueue(1024*1024) queues.Add(threadFiber); channel.Subscribe(threadFiber, onReceive); } Stopwatch sw = new Stopwatch(); sw.Start(); //Push messages for (int i = 0; i < 1000000; i++) { string msg = "[" + i + "] Push"; channel.Publish(msg); } sw.Stop(); Console.WriteLine("End : " + sw.ElapsedMilliseconds); //#Results: //1 ThreadFiber ~= 1sec //2 ThreadFiber ~=> 3sec //3 ThreadFiber ~=> 5sec //4 ThreadFiber ~=> 8sec //5 ThreadFiber ~=> 10sec }
public void Snapshot() { using (IFiber fiber = PoolFiber.StartNew()) using (IFiber fiber2 = PoolFiber.StartNew()) { var list = new List <string> { "Prime" }; var channel = new SnapshotChannel <string, string[]>(); channel.ReplyToPrimingRequest(fiber2, list.ToArray); var primeResult = new List <string>(); Action <string> update = primeResult.Add; Action <string[]> snap = primeResult.AddRange; channel.Subscribe(fiber, update, snap); Thread.Sleep(100); channel.Publish("hello"); channel.Publish("hello2"); Thread.Sleep(100); Assert.AreEqual("Prime", primeResult[0]); Assert.AreEqual("hello2", primeResult[primeResult.Count - 1]); } }
public void Multiple() { var queues = new List <IFiber>(); int receiveCount = 0; using (var reset = new AutoResetEvent(false)) { var channel = new QueueChannel <int>(); const int MessageCount = 100; var updateLock = new object(); for (int i = 0; i < 5; i++) { void OnReceive(int obj) { Thread.Sleep(15); lock (updateLock) { receiveCount++; if (receiveCount == MessageCount) { reset.Set(); } } } IFiber fiber = PoolFiber.StartNew(); queues.Add(fiber); channel.Subscribe(fiber, OnReceive); } for (int i = 0; i < MessageCount; i++) { channel.Publish(i); } Assert.IsTrue(reset.WaitOne(10000, false)); queues.ForEach(q => q.Dispose()); } }
public void MultiConsumerPool() { var queues = new List <IFiber>(); IChannel <string> channel = new QueueChannel <string>(); //Init executing Fibers for (int i = 0; i < 10; i++) { void OnReceive(string message) { var firstChar = message[0]; } IFiber fiber = PoolFiber.StartNew(); queues.Add(fiber); channel.Subscribe(fiber, OnReceive); } Stopwatch sw = new Stopwatch(); sw.Start(); //Push messages for (int i = 0; i < 1000000; i++) { string msg = "[" + i + "] Push"; channel.Publish(msg); } sw.Stop(); Console.WriteLine("End : " + sw.ElapsedMilliseconds); //#Results: //1 ThreadFiber ~= 1sec //2 ThreadFiber ~=> 3sec //3 ThreadFiber ~=> 5sec //4 ThreadFiber ~=> 8sec //5 ThreadFiber ~=> 10sec }
public void InOrderExecution() { FiberTester.InOrderExecution(ThreadFiber.StartNew()); FiberTester.InOrderExecution(PoolFiber.StartNew()); FiberTester.InOrderExecution(StubFiber.StartNew()); }
public void Setup() { _fiber = PoolFiber.StartNew(); }
public void TestReqReply() { FiberTester.TestReqReply1(ThreadFiber.StartNew()); FiberTester.TestReqReply1(PoolFiber.StartNew()); FiberTester.TestReqReply1(StubFiber.StartNew()); }
public void TestPubSubWithFilter() { FiberTester.TestPubSubWithFilter(ThreadFiber.StartNew()); FiberTester.TestPubSubWithFilter(PoolFiber.StartNew()); FiberTester.TestPubSubWithFilter(StubFiber.StartNew()); }
public void TestPubSubSimple() { FiberTester.TestPubSubSimple(ThreadFiber.StartNew()); FiberTester.TestPubSubSimple(PoolFiber.StartNew()); FiberTester.TestPubSubSimple(StubFiber.StartNew()); }
public void Setup() { _fiber = PoolFiber.StartNew(); _channel.Subscribe(_fiber, o => { }); }
public void TestName() { IRequestChannel <int, int> channel = new RequestChannel <int, int>(); IFiber fiber1 = PoolFiber.StartNew(); }