Test worker implementation

Hi :wave:

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”) :thinking:

Thanks in advance!

I’m not sure I understand the question. Using TestWorkflowEnvironment.newWorker is the way you implement unit tests in Java SDK.

Ok, let me rephrase then :sweat_smile:

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 :thinking:

Thanks!

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! :pray:

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” :confused: :

[host="NA" pid="63947" app="NA" thread="Test worker" level="INFO"] io.temporal.worker.WorkerFactory shutdownNow
[host="NA" pid="63947" app="NA" thread="Test worker" level="INFO"] io.temporal.internal.worker.Poller shutdownNow poller=Host Local Workflow Poller
[host="NA" pid="63947" app="NA" thread="Host Local Workflow Poller: 3" level="INFO"] io.temporal.internal.worker.Poller poll loop done
[host="NA" pid="63947" app="NA" thread="Host Local Workflow Poller: 5" level="INFO"] io.temporal.internal.worker.Poller poll loop done
[host="NA" pid="63947" app="NA" thread="Host Local Workflow Poller: 1" level="INFO"] io.temporal.internal.worker.Poller poll loop done
[host="NA" pid="63947" app="NA" thread="Host Local Workflow Poller: 2" level="INFO"] io.temporal.internal.worker.Poller poll loop done
[host="NA" pid="63947" app="NA" thread="Host Local Workflow Poller: 4" level="INFO"] io.temporal.internal.worker.Poller poll loop done
[host="NA" pid="63947" app="NA" thread="Test worker" level="INFO"] io.temporal.worker.WorkerFactory awaitTermination begin
[host="NA" pid="63947" app="NA" thread="Test worker" level="INFO"] io.temporal.worker.WorkerFactory awaitTermination done
[host="NA" pid="63947" app="NA" thread="Test worker" level="INFO"] i.t.s.WorkflowServiceStubsImpl shutdownNow

Do you have any insights on what may be causing this? :thinking:

Thanks!

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) :+1:

Thanks for your help so far @maxim :pray: