Parallel tests on package level

agolangf · · 441 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I have a lot of packages to test, some of which require a global resource (test db), and others that don&#39;t.</p> <p>Currently I am forced to use -p 1 to avoid conflicts. Is there any way of testing only certain packages in parallel?</p> <hr/>**评论:**<br/><br/>jerf: <pre><p>I am not aware of a Go-level one. What I would suggest is that there are locking constructs that you can use at the OS level that would permit this. I&#39;m not sure what platform you&#39;re on, but I&#39;m familiar with Unix where you can use <a href="https://golang.org/pkg/syscall/#Flock" rel="nofollow">syscall.Flock</a>. You&#39;ll need to read up a bit out of the context of Go as to <a href="https://linux.die.net/man/2/flock" rel="nofollow">how to use that system call</a>. Then anything that uses the DB should take a flock out on some agreed-upon file, and it will serialize just those tests.</p> <p><a href="https://godoc.org/?q=flock" rel="nofollow">Godoc</a> may be able to help out; <a href="https://godoc.org/github.com/theckman/go-flock" rel="nofollow">this looks encouraging</a>.</p> <p>The issue that this would raise is that if you have enough of these that have to run serially that the sum total of the time will take more than the timeout permitted by the Go test suite, they may start hitting that. I don&#39;t see any way to tweak that in the testing docs (although I may have missed them, I just sort of skimmed and did some light CTRL-f&#39;ing), in which case you may still need to do some work. It <em>may</em> be the case that if you take the flock out in a <code>TestMain</code> function (see the end of the documentation of the testing package) that the timeout won&#39;t start until the <code>m.Run()</code> is called, but I have no experience with that.</p></pre>natefinch: <pre><p><code>go test -timeout 30m</code> etc</p></pre>jerf: <pre><p>Thanks.</p></pre>nagai: <pre><p>Good idea! Gonna have a look. :)</p></pre>Ploobers: <pre><p>You can use t. Parallel() <a href="https://splice.com/blog/lesser-known-features-go-test/" rel="nofollow">https://splice.com/blog/lesser-known-features-go-test/</a></p></pre>jerf: <pre><p>That works within a single package, not between packages.</p></pre>Ploobers: <pre><p>My bad, I misread the original question.</p></pre>natefinch: <pre><p>The best way to do it would be if you could organize your packages such that all the DB ones are under a single tree, and then you could test those with -p 1, and test everything not in that tree normally. It would take two calls to test, but simpler than some other hacks. </p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

441 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传