Problem
I have a UDPlistener application that I need to write a unit test for. This listener continuously listens on a port and is meant to always be running on the product. We use the poco libraries for frameworks not in the standard library.
Now I need to add it to the unit test application.
Curent Solution
I thought it would be easiest to implement Poco::Runnable in a class RunApp that runs the application. Then I can create a new Poco::Thread in my unit test to run the RunApp class.
This works; my listener is running and I can send test messages in the unit test body after the thread is spawned. BUT, I need to stop the listener so other unit tests can run. I added a UDP message that tells the listener to kill itself but this is only used by the unit test and a potential security problem.
Question
Is there a way to force a Poco::Thread to stop? Or I structuring this unit test wrong? I don’t want the listener to run during all the other unit tests.
If instead of using a
Poco::Threadyou use aPoco::Task, you get a thread that can be cancelled. The following sample code (ready to run as-is) should give you an idea:The POCO slides Multithreading give examples of usage of both Poco::Thread and Poco::Task.
As an aside, a unit test should bypass the UDP communication via abstract classes and mock objects; I think this test should be called feature test 🙂