public static void TestBasicScenarios() { var bc = new BlockingPipe <int>(3); var tks = new Task[2]; // A simple blocking consumer with no cancellation. int expect = 1; tks[0] = Task.Run(() => { while (!bc.IsCompleted) { try { int data = bc.Take(); Assert.Equal(expect, data); expect++; } catch (InvalidOperationException) { } // throw when CompleteAdding called } }); // A simple blocking producer with no cancellation. tks[1] = Task.Run(() => { bc.Add(1); bc.Add(2); bc.Add(3); }); Task.WaitAll(tks); }
public static void PipeMain(string[] args) { new MicroThread(() => { var server = new BlockingPipeListener(); server.Bind("file"); var pipe = server.Accept(); server.Close(); byte[] data = new byte[512]; int n = 0; while ((n = pipe.Receive(data)) != 0) { Console.WriteLine(n); Console.WriteLine(Encoding.ASCII.GetString(data, 0, n)); } pipe.Close(); }).Start(); new MicroThread(() => { var pipe = new BlockingPipe(); pipe.Connect("file"); var bytes = Encoding.ASCII.GetBytes("NESAMONE"); pipe.Write(bytes); pipe.Close(); }).Start(); }
private static void Test() { Pipe.counter = 0; var list = new List <string>(); var p = new BlockingPipe(list); var f = Task.Factory; var b = new Barrier(3); f.StartNew(() => { p.Client("asdf"); b.SignalAndWait(); }); f.StartNew(() => { p.Server("qwer"); b.SignalAndWait(); }); b.SignalAndWait(); var exp = String.Join("\n", new[] { "server0 processes qwer", "client0 processes asdf" }); var act = String.Join("\n", list); Assert.AreEqual(exp, act); }