I’m building a solution with Temporal (java SDK) and I would like to know if there is a way to test a worker implementation within the TestWorkflowEnvironment.
Imagine a scenario where I have a worker that is responsible for processing the workflow and its activities. How can I implement a test that assures the worker is processing everything that is intended?
AFAIK the only way to register workers in the testEnviroment is with testEnv.newWorker(“taskQueueName”)
I’ve developed a worker to process a workflow and its activities. How can I make sure that the worker is processing the workflow and the related activities correctly? The end goal may be something like connecting the custom-developed worker to the TestWorkflowEnvironment in order to make sure that everything will work as expected
What do you mean by “I’ve developed a worker”? Did you completely reimplement the Worker provided by the framework? Then you can initialize it using WorkflowServiceStubs returned by TestWorkflowEnvironment.getWorkflowService method.
“I’ve developed a worker” means I have a class similar to this one:
class Worker(
private val workflowClient: WorkflowClient,
) {
fun start() {
val factory = WorkerFactory.newInstance(workflowClient)
val workerOptions = WorkerOptions.newBuilder().build()
val worker: Worker = factory.newWorker("taskQueueName", workerOptions)
worker.addWorkflowImplementationFactory(...)
worker.registerActivitiesImplementations(...)
factory.start()
}
}
Thanks for your suggestion, it worked out!
However, I’ve noticed that the tests (only have 3 ATM) lasted for 3s with a worker from TestWorkflowEnvironment.newWorker(..) and ~3m30s with my “custom-developed worker”.
I’ve a:
@AfterEach
fun tearDown() {
testEnv.close()
}
and it seems that on each test, after the execution of the last assert statement, it logs the following lines and gets “stuck” :
The test framework automatically performs time skipping if a workflow is blocked. It allows executing unit tests much faster. Your own worker is not integrated with the time skipping logic and executes the test in system time.
I would recommend not using your worker for unit testing the workflow code.
It makes total sense considering that the workflow that I’m testing has an activity that is responsible for dispatching the execution of a different workflow (processed by a different worker that doesn’t exist in the context of the tests)