ronsun
3/17/2018 - 3:57 PM

multi-thread sample

testing for thread-safe

[Test()]
public void NextTest_RandomTwiceInShortTimeWithMultipleThreads_DifferentResult()
{
    //arrage
    int? firstThreadResult = null;
    var firstThread = new Thread(new ThreadStart(() =>
    {
        firstThreadResult = new RandomUtil().Next();
    }));

    int? secondThreadResult = null;
    var secondThread = new Thread(new ThreadStart(() =>
    {
        secondThreadResult = new RandomUtil().Next();
    }));

    //act
    firstThread.Start();
    secondThread.Start();

    firstThread.Join();
    secondThread.Join();

    //assert
    firstThreadResult.Should().NotBe(secondThreadResult);
}