public void TestSay_Task_runs_concurrently() { var sut = new Concurrency(); Task.Run(() => sut.Say("Hello", TimeSpan.FromMilliseconds(10))); sut.Say("World", TimeSpan.FromMilliseconds(1)); var output = sut.Logs.ToArray(); Assert.Equal(5, output.Length); Assert.Equal(5, output.Count(message => message.EndsWith("World"))); }
public void TestSay() { var sut = new Concurrency(); var duration = TimeSpan.FromMilliseconds(5); Task.Run(() => sut.Say("Hello", duration)); sut.Say("World", duration); // wait for task same as Go's. Thread.Sleep(duration); var output = sut.Logs.ToArray(); Assert.Equal(10, output.Length); Assert.Equal(5, output.Count(message => message.EndsWith("Hello"))); }
public void Test_wait_Task() { var sut = new Concurrency(); // Unlike Go's goroutine, Task has its state. var task = Task.Run(() => sut.Say("Hi", TimeSpan.FromMilliseconds(5))); // wait task completes with timeout. task.Wait(TimeSpan.FromSeconds(1)); var output = sut.Logs.ToArray(); Assert.True(output.All(message => message == "Hi")); }