Those fortunate enough to program asynchronously with Qt4 C++ can use a combination of QSignalSpy and KDE's QTest::kWaitForSignal to get the same behavior.
Separating the behavior of an object from the execution context is also a good idea. Java 5 ExecutorService objects are a great way to determine at run time the execution context. Rather than spawning threads, dependency injection can be used to give an object an executor service, and then in unit tests the executor service passed can be a SerialExecutorService which executes everything on the current thread. This isn't the best description, but more here: http://hamletdarcy.blogspot.com/2007/05/unit-testing-multi-threaded-code-with.html
Your test is named testInternMakesCoffee() but after your magic refactor you're not testing an intern's ability to make coffee, you're testing your test, or at best your Delegator.
Or maybe you should have renamed your test to testDelegateToIntern()
How would you actually write a test for Intern().MakeCoffee in a threaded environment?
Those fortunate enough to program asynchronously with Qt4 C++ can use a combination of QSignalSpy and KDE's QTest::kWaitForSignal to get the same behavior.
ReplyDeletecfr http://api.kde.org/4.1-api/kdelibs-apidocs/kdecore/html/namespaceQTest.html
Separating the behavior of an object from the execution context is also a good idea. Java 5 ExecutorService objects are a great way to determine at run time the execution context. Rather than spawning threads, dependency injection can be used to give an object an executor service, and then in unit tests the executor service passed can be a SerialExecutorService which executes everything on the current thread. This isn't the best description, but more here: http://hamletdarcy.blogspot.com/2007/05/unit-testing-multi-threaded-code-with.html
ReplyDeleteQ: Shouldn't the order of these two lines be:
ReplyDeleteis_done.wait(timeout=60)
can_finish.set() # Now let FakeCoffeeMaker return.
Your test is named testInternMakesCoffee() but after your magic refactor you're not testing an intern's ability to make coffee, you're testing your test, or at best your Delegator.
ReplyDeleteOr maybe you should have renamed your test to testDelegateToIntern()
How would you actually write a test for Intern().MakeCoffee in a threaded environment?