# Testing Channels Worker processes

**URL:** <https://forum.djangoproject.com/t/testing-channels-worker-processes/12809>\
**Category:** Async/Channels\
**Created:** [March 25, 2022, 4:04am UTC](https://forum.djangoproject.com/t/testing-channels-worker-processes/12809 "2022-03-25T04:04:11Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 30, 2022, 1:23pm UTC](https://forum.djangoproject.com/t/testing-channels-worker-processes/12809/7 "2022-03-30T13:23:47Z")

</div>

> [@carltongibson](#):
>
> OK… Perhaps I’m a bit lost by exactly what you mean by _Run the worker_ …

Ok, there may be some terminology mismatch here - let me see if I can clarify without making things worse.

The overall architecture, as I’m using it:  
 ![Channels 1](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/3/3651cd057ce6ebbac1fa9e5827968fe31a4b6531.png)

So I’m using the term “Worker” in the context as described at [Worker and Background Tasks — Channels 4.0.0 documentation](https://channels.readthedocs.io/en/stable/topics/worker.html#worker-and-background-tasks)

The consumers:

- Receive messages from the browser(s),
- Figures out _which_ worker needs to process that message
- Forwards the message to the right worker, via the channel layer

The guts of the _work_ is being done in the workers.

What’s not shown in the diagram is that a worker, in the process of handling a message, _could_ post additional messages to also be processed by the same worker, or by a different worker. So running the worker in this context means that the worker process is listening to 4 channels.

That’s what I’m wanting to test. I need to send a message to worker “A”, and (potentially) wait for responses from workers “A” and “B”.

One final aspect of this is that the logic for worker “A” is really quite trivial.

- Read the message,
- Get some state information from a Model,
- Call a function as identified by the combination of message and state
  - Determine the new state,

- Prepare a response by calling a different function

So what I’m _really_ trying to test _here_ is a state machine.

Testing the logic isn’t the issue. These tests are being created to test all the state transitions and verify that the state machine is accurate.

What I have working:  
 ![Current pytest](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/c/c917843b776825ed5431be70fcad9814c6bf2126.png)  
This works, but every time I make a change to the worker, I need to restart it before restarting the tests.

I wouldn’t really call this E2E, because I’m completely bypassing the Consumer layer. But I acknowledge that it’s not truly a “unit test” either because of the dispatch from worker “A” to worker “B”.  
In theory, I should test the message to worker “B” separately from the message to worker “A”, and I’m not ruling that out as an option.

Where I’m trying to get to:  
 ![Desired](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/d/d9b3b62ff1815c5f7ef932357846b9487eb385d8.png)

Now, if I start a separate thread within a fixture, I can (kinda, sorta, effectively) do this now. The problem is that I’m not cleanly stopping that thread after a test.

What I’d really like to get to:  
 ![Ultimate](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/8/801932d99caba4c9e5ef36f03253107d8ce397b1.png)

And that’s where I have some ideas.

Also, in thinking more about it based on what you’ve said, I may try the even more simplified version:  
 ![Ultimate 1](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/c/c39d2122b74b255941a2f9135ebcc3212742190b.png)

Like I said - I ain’t done yet…

---

_[View the full topic](https://forum.djangoproject.com/t/testing-channels-worker-processes/12809)._
