elixir – 测试时Ecto 2.0 SQL Sandbox错误

前端之家收集整理的这篇文章主要介绍了elixir – 测试时Ecto 2.0 SQL Sandbox错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我最近将我的凤凰项目升级到了Ecto 2.0.2.我有一些代码使用Task.Supervisor.async_nolink在自己的线程上对db进行一些更新.我的测试运行时出现以下错误(仅在我的测试中发生)
[error] Postgrex.Protocol (#PID<0.XXX.0>) disconnected: **
(DBConnection.ConnectionError) owner #PID<0.XXX.0> exited while 
client #PID<0.XXX.0> is still running with: shutdown

现在我想我了解发生了什么:在db事务完成之前,正在检查Ecto SandBox连接池.根据文档(至少我读它们的方式),解决这些问题的方法是使用共享连接池:Ecto.Adapters.sql.SandBox.mode(MyApp.Repo,{:shared,self()} )我在做什么.不幸的是,这不起作用.

如何设置我的测试以便不会发生此错误

解决方法

如果有其他人遇到这个,我直接从语言作者Jose Valim那里得到了答案:

Yes,your understanding of the issue is correct. It is happening because the test process,the one who owns the connection,has exited but the Task is still using its connection. Using {:shared,self()} does not fix it because the test is still owning the connection,you are just sharing it implicitly.

The way to fix is to guarantee the Task has finished before the test exits. This can be done by calling Process.exit(task_pid,:kill). If you don’t know the Task PID,you can call Task.Supervisor.which_children(NameOfYourTaskSupervisor) to return all PIDs which you then traverse and kill them. However,if you do this approach,the test cannot run concurrently (as you may kill tasks started by another test).

猜你在找的MsSQL相关文章