How should I unit test threaded code?

后端 未结 26 1321
悲&欢浪女
悲&欢浪女 2020-11-22 04:09

I have thus far avoided the nightmare that is testing multi-threaded code since it just seems like too much of a minefield. I\'d like to ask how people have gone about test

26条回答
  •  孤独总比滥情好
    2020-11-22 04:34

    Awaitility can also be useful to help you write deterministic unit tests. It allows you to wait until some state somewhere in your system is updated. For example:

    await().untilCall( to(myService).myMethod(), greaterThan(3) );
    

    or

    await().atMost(5,SECONDS).until(fieldIn(myObject).ofType(int.class), equalTo(1));
    

    It also has Scala and Groovy support.

    await until { something() > 4 } // Scala example
    

提交回复
热议问题