<p>I have a lot of packages to test, some of which require a global resource (test db), and others that don'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'm not sure what platform you're on, but I'm familiar with Unix where you can use <a href="https://golang.org/pkg/syscall/#Flock" rel="nofollow">syscall.Flock</a>. You'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'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'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'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>
![](https://static.golangjob.cn/static/img/banner.png)