<div dir="ltr">Matthew,<div><br></div><div>Like Nathaniel said, the eunit test should involve gen_server. Do not test handle_cast/2 directly. Instead, run your test on an exported function (say: Module:accept/0) which in turn should execute: </div><div><br></div><div>gen_server:cast(?MODULE, accept)</div><div><br></div><div>You can start a gen_server process, execute such tests and shut it down properly using 'setup' Fixture: <a href="http://learnyousomeerlang.com/eunit#fixtures">http://learnyousomeerlang.com/eunit#fixtures</a></div><div><br></div><div>Regards,</div><div>Vimal</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Mar 27, 2016 at 10:15 AM, Nathaniel Waisbrot <span dir="ltr"><<a href="mailto:nathaniel@waisbrot.net" target="_blank">nathaniel@waisbrot.net</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I don't know about your garbage-collection question, but:<br>
<span class=""><br>
> Why does that io:format/3 call (only when after the spawn/3 call) keep the socket open and active for the spawned process? I wouldn't expect it to be garbage collected because the spawned process holds a reference to it (and even adding it to the State in the return doesn't fix it). Only other thing I could think of was that handle_cast is run in it's own process which closes the socket when it dies (since it has ownership), but that doesn't make sense from what I've read about OTP.<br>
<br>
<br>
</span>Normally handle_cast would be run in the gen_server's process (since that's the whole point), but it looks to me like in your test:<br>
<span class=""><br>
<br>
> spawn_link(fun () -> tcp_listener_server:handle_cast(accept, State) end),<br>
<br>
<br>
</span>you are bypassing gen_server and instead ... telling handle_cast to run in its own one-off process.<br>
<br>
<br>
Do you mean to be doing this? In your test code, you could call gen_server:start/1 and then gen_server:cast/2 and get the actual gen_server behavior.<br>
<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" rel="noreferrer" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
<br></blockquote></div><br></div>