/// <summary> /// Demonstrate that messangers work across threads. /// </summary> private static void DemoMultithreadedness() { Console.WriteLine("Multi-threaded"); var queue = new QueriableAsynqueue <int, string>(i => "Hey " + i); // Create 10 threads (more or less, depending on the ThreadPool) // and have each of them send queries to the queue for (var i = 0; i < 10; ++i) { ThreadPool.QueueUserWorkItem(async _ => { var originalId = Thread.CurrentThread.ManagedThreadId; Console.WriteLine("T" + originalId + " starting"); for (var x = 1; x <= 1000000; ++x) { var a = await queue.Query(x); if (x % 100000 == 0) { Console.WriteLine("T" + originalId + " is now " + Thread.CurrentThread.ManagedThreadId); await Task.Delay(1); } } Console.WriteLine("T" + originalId + " stopping, (is now " + Thread.CurrentThread.ManagedThreadId + ")"); }); } Console.ReadLine(); }
public void Queriable_queue_handles_multiple_sends() { var q = new QueriableAsynqueue <int, string>(i => "Hello " + i); var t1 = Task.Run(async() => { var result = await q.Query(1); result.ShouldEqual("Hello 1"); }); var t2 = Task.Run(async() => { var result = await q.Query(2); result.ShouldEqual("Hello 2"); }); Task.WaitAll(t1, t2); }
public void Queriable_queue_returns_value_to_sender() { var q = new QueriableAsynqueue <int, string>(i => "Hello " + i); var task = Task.Run(async() => { var result = await q.Query(7); result.ShouldEqual("Hello 7"); }); task.Wait(); }
/// <summary> /// Demonstrate the performance of the queriable messenger. /// </summary> private static async Task <long> BidirectionalQueue() { var queue = new QueriableAsynqueue <int, string>(i => "Hey " + i); var w = Stopwatch.StartNew(); for (var x = 1; x < 1000000; ++x) { await queue.Query(x); } return(w.ElapsedMilliseconds); }
public void Queriable_queue_throws_exception_on_sender() { Func <int, string> f = (i) => { throw new Exception("Ruh roh"); }; var q = new QueriableAsynqueue <int, string>(f); var task = Task.Run(async() => { try { var result = await q.Query(7); } catch (Exception ex) { ex.Message.ShouldEqual("Ruh roh"); } }); task.Wait(); }