<div>It's hard to help without actually seeing the code (or the test suite), but the problem is not a bug, although it does appear to be a race condition.<br></div><div><br></div><div>As you have found, you need to use gen_server:call/2-3 and make the termination of a gen_server synchronous to be very useful with tests.</div>
<div><br></div><div>The other things to be careful about after this is to make sure that starting/stopping the server doesn't happen at the same time. If you're using fixtures or just test generating functions, using the '{inorder, TestObjects}' representation can help:</div>
<div><br></div><div><font face="courier new,monospace">{inorder, [ListOfTests]}</font></div><div><br></div><div>or in a fixture:</div><div><font face="courier new,monospace">some_test_() -></font><font face="courier new,monospace"><br>
</font></div><div><font face="courier new,monospace"> {"test description",</font></div><div><font face="courier new,monospace"> {setup,</font></div><div><font face="courier new,monospace"> fun setup/0,<br>
</font></div><div><font face="courier new,monospace"> fun teardown/1,<br></font></div><div><font face="courier new,monospace"> fun(SetupArg) -><br></font></div><div><font face="courier new,monospace"> {inorder, [<br>
</font></div><div><font face="courier new,monospace"> ?_assert(...),<br></font></div><div><font face="courier new,monospace"> ...<br></font></div><div><font face="courier new,monospace"> ]}<br></font></div>
<div><font face="courier new,monospace"> end}}.<br></font></div><div><br></div><div>The other reason I can imagine is that you're making the mistake of calling your start/stop functions within a test generator (within a ?_Macro(Assertion)): these are actually equivalent to 'fun() -> ?Macro(Assertion) end' and won't be run at declare time, but only later, which can provoke a few errors if you're not careful.<font face="courier new,monospace"><br>
</font></div><br><div class="gmail_quote">On Thu, Nov 3, 2011 at 6:35 AM, Lukas P <span dir="ltr"><<a href="mailto:lukasp.p017@gmail.com">lukasp.p017@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div>Hello,</div><div><br></div><div>in my eunit tests I am starting/stopping a gen_server.</div><div><br></div><div>The code used to fail occassionally - gen_server's process name was still registered when gen_server:call(?SERVER, stop) returned and the next gen_server:start_link({local, ?SERVER}...) failed with {error,{already_started,_}}.</div>
<div><br></div><div>Adding unregister to gen_server's terminate/2 solved the problem (terminate/2 is called before responding to the stop request), but is there a reason why gen_server does not unregister its name at the right time?</div>
<div><br></div><div>Thanks, Lukas</div><div><br></div>
<br>_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
<br></blockquote></div><br>