我将GenServer作为后台作业运行,每个interval
都会重新调度。
此作业由主管在应用程序启动时启动。
它工作得很好,但是现在我想测试一下我的GenServer模块是否真的在每个interval
生成新进程。
如何测试?
- 编辑 *
我发现可以使用:sys.get_status(pid)
来获取一些关于进程数据,但我确实希望使用类似receive do ... end
的数据,
编辑2
handle_info/2
函数:
@impl true
def handle_info(:work, state) do
do_smt()
schedule_worker()
{:noreply, state}
end
schedule_worker/0
函数:
defp schedule_worker do
Process.send_after(self(), :work, @interval)
end
2条答案
按热度按时间wgeznvg71#
您的消息中缺少某些内容。从您发布的内容中,我们可以了解到每隔
@interval
毫秒就会发送一条:work
消息。您没有告诉我们在发送消息时handle_info/2
应该执行什么操作。一旦定义了它,就可以使用
assert_received
Assert编写一个测试来Assert消息已经收到。w6lpcovy2#
I would test
do_smt()
by using Mock library and writing a test that makes as assertion like the following:In this way, you have called the function that the task should execute, so you can assume that the task creation is being called.
If you want to let the
do_stm_else
function communicate with your test (in this scenario it looks a bit overengineered) you should:self()
assert_receive
to verify that the communication has occurredPlease note that I had no time to check this, you should spend a bit to investigate.