I have a worker processes working on a data queue. A training worker reads and processes data from the training queue. It continues to poll and on the look out for new data written to the queue.
I have implemented the worker as follows:
public static void main(String[] args) {
startTraining()
}
public static boolean startTraining() {
trainingWorker = new WorkerImpl(config);
final Thread workerThread = new Thread(trainingWorker);
WorkerListener listener = new WorkerListener() {
public void onEvent(WorkerEvent event, Worker worker, String queue, Job job,
Object runner, Object result, Exception ex) {
if(event.equals(WorkerEvnet.JOB_SUCCESS) {
//get data
//process data
//send data
}
}
}
trainingWorker.addListener(listener, WorkerEvent.JOB_SUCCESS, WorkerEvent.JOB_FAILURE,
WorkerEvent.WORKER_ERROR, WorkerEvent.WORKER_POLL);
workerThread.start();
workerThread.join();
}
I want unit tests to see if the worker read the data properly, processed it correctly and successfully sent it. The problem is because of Thread.join(); the call to the main function never returns (as the worker is suppose to run continuously). In this case should I not write unit tests for it? If yes, how can I write some other form of test that will ensure the worker is working as desired?
you may want either to remove join from your code, or to start new thread from your test case, which will start the worker for you.
Obviously you will need the way to terminate the worker, for version it may look like
so you will need to convert your workerThread local value to a field, and make it volatile.