<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="126656" data-post-id="126656">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Fl4m3Ph03n1x" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/Fl4m3Ph03n1x/120/11709_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Fl4m3Ph03n1x
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<blockquote>
<p>In the absence of macros keyword lists are cheaper to create than maps. That way the production code pays the minimum of overhead while the cost is moved to the testing code.</p>
</blockquote>
<p>If you tell me of a real use case where using maps (instead of keywords) made your application sluggish and nightmare slow for your clients to use, then I will refactor the code to use keyword lists.</p>
<p>Until then, I shall treat the Maps VS Keywords debate as I treat everything else before doing performance testing and finding actual pain points: as premature optimization.</p>
<p>Still, it’s good to know this detail about Keywords, thanks for chipping in!</p>
<aside class="quote no-group" data-username="hauleth" data-post="11" data-topic="22090">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hauleth/48/18942_2.png" class="avatar"> hauleth:</div>
<blockquote>
<p>How about testing <code>handle_continue/2</code> by calling <code>handle_continue/2</code> ? If <code>init/1</code> does nothing then this would be as simple as:</p>
</blockquote>
</aside>
<p>I don’t <em>want</em> to test <code>handle_continue</code>, I just want it to run before <code>ExUnit</code> starts verifying the assertions in the tests <img src="https://forum.elixirforum.com/images/emoji/apple/smiley.png?v=15" title=":smiley:" class="emoji" alt=":smiley:" loading="lazy" width="20" height="20"></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="126656" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/test-processes-using-handle-continue/22090/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-126656" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="126656"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #11"></div>
  </section>
</div>
    <div class="postbit" id="126658" data-post-id="126658">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="hauleth" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hauleth/120/18942_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  hauleth
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Is the SUT your process?</p>
<ul>
<li>If yes, and these are unit tests, then my answer is - <strong>do not start the server at all</strong>. Just call functions and provide your own state directly. In that way you do not have the problem with asynchronous testing.</li>
<li>If that process isn’t SUT then mock it by starting different process with the same name</li>
<li>If these tests are integration tests then sleep in the test setup and then perform all actions.</li>
</ul>
<p>Nothing prevents you from instead of using <code>GenServer.call(my_server_pid, :message)</code> use <code>MyServer.handle_call(:message, make_ref(), state)</code>.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="126658" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/test-processes-using-handle-continue/22090/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-126658" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="126658"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #12"></div>
  </section>
</div>
    <div class="postbit" id="126662" data-post-id="126662">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="chrismcg" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/chrismcg/120/671_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  chrismcg
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I can’t remember where I saw it now, but I’m sure I saw a post about using the <code>:erlang.trace</code> function to set up tracing on the SUT from the test. i.e. setup tracing for <code>YourModule.handle_continue</code> function and when it’s called tear down the tracing and continue with the assertions.</p>
<p>I can’t remember any more details than that, and I haven’t tried it myself but it seems possible to know the function has been called without having to add an extra message just for testing.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="126662" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/test-processes-using-handle-continue/22090/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-126662" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="126662"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #13"></div>
  </section>
</div>
    <div class="postbit" id="126666" data-post-id="126666">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="amnu3387" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  amnu3387
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="Fl4m3Ph03n1x" data-post="12" data-topic="22090">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/fl4m3ph03n1x/48/11709_2.png" class="avatar"> Fl4m3Ph03n1x:</div>
<blockquote>
<p>I don’t <em>want</em> to test <code>handle_continue</code> , I just want it to run before <code>ExUnit</code> starts verifying the assertions in the tests <img src="https://forum.elixirforum.com/images/emoji/apple/smiley.png?v=15" title=":smiley:" class="emoji" alt=":smiley:" loading="lazy" width="20" height="20"></p>
</blockquote>
</aside>
<p>But what do you want to test then? Showing it might help understanding how to do it? If you want to test that a call made after the full initialisation (meaning <code>init</code> + <code>:continue</code>) results in either A or B according to the state set after the <code>:continue</code> executes, then doing a call with a big enough timeout should be (for practical purposes) deterministic (say 10secs timeout). If you want to test that side effects to the process state happen, then probably chaining manual calls to the <code>handle</code> methods should be enough, etc</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="126666" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/test-processes-using-handle-continue/22090/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-126666" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="126666"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #14"></div>
  </section>
</div>
    <div class="postbit" id="126667" data-post-id="126667">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="chrismcg" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/chrismcg/120/671_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  chrismcg
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>So I had a play around with this:</p>
<p>A simple server:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Tracetest.Server do
  use GenServer

  def start_link(arg) do
    GenServer.start_link(__MODULE__, [arg])
  end

  @impl true
  def init([arg]) do
    {:ok, :some_state, {:continue, arg}}
  end

  @impl true
  def handle_continue(_, state) do
    {:noreply, state}
  end
end
</code></pre>
<p>The test:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir"> test "can know when handle_continue finished" do
    :erlang.trace(:new, true, [:call, :return_to])
    :erlang.trace_pattern({Tracetest.Server, :handle_continue, 2}, true, [:local])

    {:ok, pid} = Tracetest.Server.start_link(:foo)

    assert_receive {:trace, ^pid, :call,
                    {Tracetest.Server, :handle_continue, [:foo, :some_state]}}

    assert_receive {:trace, ^pid, :return_to, {:gen_server, :try_dispatch, 4}}

    assert true == true
  end
</code></pre>
<p>I setup the same tracing calls in the iex console, started the server there, and ran <code>flush</code> to see what trace messages got received. There were only two so it was straightforward to match on them. I am sure your real code is a lot more complicated than this but perhaps just waiting till the method had been called rather than a return would be enough to avoid the race condition.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="126667" data-batch-url="/posts/batch_likers">
                        4
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/test-processes-using-handle-continue/22090/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-126667" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="126667"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #15"></div>
  </section>
</div>
    <div class="postbit" id="126671" data-post-id="126671">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="peerreynders" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/peerreynders/120/5826_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  peerreynders
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="Fl4m3Ph03n1x" data-post="12" data-topic="22090">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/fl4m3ph03n1x/48/11709_2.png" class="avatar"> Fl4m3Ph03n1x:</div>
<blockquote>
<p>I don’t <em>want</em> to test <code>handle_continue</code> , I just want it to run before <code>ExUnit</code> starts verifying the assertions in the tests <img src="https://forum.elixirforum.com/images/emoji/apple/smiley.png?v=15" title=":smiley:" class="emoji" alt=":smiley:" loading="lazy" width="20" height="20"></p>
</blockquote>
</aside>
<p>The last few topics that you have posted suggest to me that you need a new boundary - for example between the worker process and the http client library.</p>
<p>Essentially that puts the http client library in the horrible outside world and the new module becomes the way your application wants to interact with “the service” - i.e. it becomes a contract that is specified by your application.</p>
<p>Once that boundary is established a test double can be slipped in that the worker process uses to complete its flow to produce results (e.g. a response message) that are directly observable by the test case process.</p>
<p>Depending on the necessary sophistication of the test double “test first” may need to take a back seat for a while.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="126671" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/test-processes-using-handle-continue/22090/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-126671" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="126671"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #16"></div>
  </section>
</div>
    <div class="postbit" id="126678" data-post-id="126678">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="hauleth" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hauleth/120/18942_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  hauleth
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Especially for you I have prepared small library that I want to publish on Hex later:</p>
<pre data-code-wrap="erlang"><code class="lang-erlang">-module(gen_local).

%% API exports
-export([start/2,
         call/2,
         cast/2,
         send/2]).

-record(state, {state, module}).

%%====================================================================
%% API functions
%%====================================================================

start(Module, Arg) -&gt;
    case Module:init(Arg) of
        {ok, State} -&gt; {ok, #state{state = State, module = Module}};
        {ok, State, {continue, Msg}} -&gt;
            handle_continue(Msg, #state{state = State, module = Module});
        {ok, State, _Timeout} -&gt; {ok, #state{state = State, module = Module}};
        ignore -&gt; ignore;
        {stop, Reason} -&gt; {stopped, Reason}
    end.

call(#state{module = Module, state = State} = S, Msg) -&gt;
    Tag = make_ref(),
    case Module:handle_call(Msg, {self(), Tag}, State) of
        {reply, Reply, NewState} -&gt; {ok, Reply, S#state{state = NewState}};
        {reply, Reply, NewState, {continue, Cont}} -&gt;
            case handle_continue(Cont, S#state{state = NewState}) of
                {ok, NewNewState} -&gt; {ok, Reply, NewNewState};
                Other -&gt; Other
            end;
        {reply, Reply, NewState, _Timeout} -&gt; {ok, Reply, S#state{state = NewState}};
        {noreply, NewState} -&gt; async_reply(Tag, S#state{state = NewState});
        {noreply, NewState, {continue, Cont}} -&gt;
            case handle_continue(Cont, S#state{state = NewState}) of
                {ok, NewNewState} -&gt; async_reply(Tag, S#state{state = NewNewState});
                Other -&gt; Other
            end;
        {noreply, NewState, _Timeout} -&gt; async_reply(Tag, S#state{state = NewState});
        {stop, Reason, NewState} -&gt; {stopped, Reason, NewState};
        {stop, Reason, Reply, NewState} -&gt; {stopped, Reason, Reply, NewState}
    end.

cast(S, Msg) -&gt;
    handle_reply(fake_call(S, handle_cast, Msg), S).

send(S, Msg) -&gt;
    handle_reply(fake_call(S, handle_info, Msg), S).

%%====================================================================
%% Internal functions
%%====================================================================

fake_call(#state{state = State, module = Module}, Callback, Msg) -&gt;
    Module:Callback(Msg, State).

handle_continue(Msg, S) -&gt;
    handle_reply(fake_call(S, handle_continue, Msg), S).

async_reply(Tag, State) -&gt;
    receive
        {Tag, Reply} -&gt; {ok, Reply, State}
    end.

handle_reply({noreply, NewState}, S) -&gt;
    {ok, S#state{state = NewState}};
handle_reply({noreply, NewState, {continue, Msg}}, S) -&gt;
    handle_continue(Msg, S#state{state = NewState});
handle_reply({noreply, NewState, _Timeout}, S) -&gt;
    {ok, S#state{state = NewState}};
handle_reply({stop, Reason, NewState}, _S) -&gt;
    {stopped, Reason, NewState}.
</code></pre>
<p>This provide interface similar to the <code>gen_server</code> but functions are run synchronously instead of asynchronously. This should make testing of <code>gen_server</code> a little bit easier (however there still is no timeout, so you cannot test that).</p>
<p><strong>EDIT:</strong></p>
<p>Published on <a href="https://github.com/hauleth/gen_local" rel="noopener nofollow ugc">GitHub</a> for now.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="126678" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/test-processes-using-handle-continue/22090/18">Post #17</a>
	                </div>
	            </div>
              <div id="likers-container-126678" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="126678"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #17"></div>
  </section>
</div>
    <div class="postbit" id="126726" data-post-id="126726">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="sorentwo" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sorentwo/120/37360_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  sorentwo
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Oban Core Team</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>When working with a lot of asynchronous calls (<code>handle_continue</code>, <code>handle_info</code>, <code>:timer.send_interval</code>, etc) I often make use of a helper to automate testing for results repeatedly. This avoids using fixed <code>Process.sleep/1</code> calls where the sleep period is statically defined and either too long (which is pointlessly slow) or too short (which causes flickering tests):</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def with_backoff(opts \\ [], fun) do
  total = Keyword.get(opts, :total, 50)
  sleep = Keyword.get(opts, :sleep, 10)

  with_backoff(fun, 0, total, sleep)
end

def with_backoff(fun, count, total, sleep) do
  fun.()
rescue
  exception in [ExUnit.AssertionError] -&gt;
    if count &lt; total do
      Process.sleep(sleep)

      with_backoff(fun, count + 1, total, sleep)
    else
      reraise(exception, __STACKTRACE__)
    end
end
</code></pre>
<p>I typically use this from higher level integration tests, where it isn’t desirable to call <code>handle_</code> functions directly.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="126726" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/test-processes-using-handle-continue/22090/19">Post #18</a>
	                </div>
	            </div>
              <div id="likers-container-126726" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="126726"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #18"></div>
  </section>
</div>
    <div class="postbit" id="126728" data-post-id="126728">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="axelson" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/axelson/120/26351_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  axelson
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Scenic Core Team</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="Fl4m3Ph03n1x" data-post="12" data-topic="22090">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/fl4m3ph03n1x/48/11709_2.png" class="avatar"> Fl4m3Ph03n1x:</div>
<blockquote>
<p>I don’t <em>want</em> to test <code>handle_continue</code> , I just want it to run before <code>ExUnit</code> starts verifying the assertions in the tests <img src="https://forum.elixirforum.com/images/emoji/apple/smiley.png?v=15" title=":smiley:" class="emoji" alt=":smiley:" loading="lazy" width="20" height="20"></p>
</blockquote>
</aside>
<p>In your production system how do you know that the workers are available to run? Also what type of failure do you get in your tests when the workers aren’t finished initializing? If you’re just doing a <code>handle_call</code> to the workers then I would expect it to work just fine (unless the initialization takes more than 5 seconds which would cause you to exceed the default <code>handle_call</code> timeout)</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="126728" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/test-processes-using-handle-continue/22090/20">Post #19</a>
	                </div>
	            </div>
              <div id="likers-container-126728" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="126728"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #19"></div>
  </section>
</div>
    <div class="postbit" id="126779" data-post-id="126779">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Fl4m3Ph03n1x" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/Fl4m3Ph03n1x/120/11709_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Fl4m3Ph03n1x
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="hauleth" data-post="13" data-topic="22090">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hauleth/48/18942_2.png" class="avatar"> hauleth:</div>
<blockquote>
<p>Is the SUT your process?</p>
</blockquote>
</aside>
<p>I feel there is some clarification needed here. The Worker process is  a GenServer, that does, some work.<br>
As all GenServers, it’s code is divided into 3 parts:</p>
<ol>
<li>The public API that clients can call</li>
<li>GenServer calls to the process itself</li>
<li><code>handle_x</code> calls which do the real work</li>
</ol>
<p>In the tests I am performing right now, I am testing the public API. Say I have the following code:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">
defmodule Clint.Worker do
  use GenServer

  defmodule State do
    defstruct conn_pid: nil,
      active_streams: [],
      worker_id: nil,
      opts: %{},
      deps: %{}
  end

  ###############
  # Public API  #
  ###############

  @spec start_link(map) :: Supervisor.on_start
  def start_link(args) do
    deps =
      args
      |&gt; Map.get(:deps, %{})
      |&gt; build_deps()

    worker_id = Map.fetch!(args, :worker_id)
    opts = Map.fetch!(args, :opts)

    Logger.debug("Starting worker #{inspect worker_id }")

    init_state = %State{worker_id: worker_id, opts: opts, deps: deps}
    pname = :bananas
    GenServer.start_link(__MODULE__, init_state, name: pname)
  end

  @spec request(atom, integer, charlist, map) :: {:ok, :received}
  def request(group_name, worker_id, url, injected_deps \\ %{}) do
    deps = build_deps(injected_deps)
    GenServer.cast(:bananas, {:fire, url})
    {:ok, :received}
  end

  @spec build_deps(map) :: map
  defp build_deps(injected_deps), do:
    Map.merge(@default_deps, injected_deps, fn _, a, b -&gt; Map.merge(a, b) end)

  #############
  # Callbacks #
  #############

  @impl GenServer
  def init(state) do
    Process.flag(:trap_exit, true)
    {:ok, state, {:continue, :establish_conn}}
  end

  @impl GenServer
  def handle_continue(:establish_conn, state) do
    with  {:ok, conn_pid} &lt;- Logic.establish_connection do
      new_state = %{state | conn_pid: conn_pid}
      {:noreply, new_state}
    else
      {:error, reason} -&gt; {:stop, reason, state}
    end
  end

  @impl GenServer
  def handle_cast({:fire, url}, state) do
    stream_ref = state.deps.http.get.(state.conn_pid, url)

    new_state = %{state | active_streams: [stream_ref | state.active_streams]}
    {:noreply, new_state}
  end
end
</code></pre>
<p>Here the public API (what clients will call) are the <code>start_link</code> and <code>request</code> functions. I am testing the Public API of the worker, the face it shows to the world.</p>
<p>This fits nicely into the “<em>Test the interface, not the implementation</em>” rule of testing, but it is insufficient. For example, using this methodology I can’t test any <code>handle_info</code> calls, unless I create a worker and then send him the exact messages that trigger <code>handle_info</code> (which according to some community members, I should do. I am now trying out this strategy).</p>
<p>Is this Unit testing? Is this integration?<br>
I argue that the Worker is my unit, so I can argue this is unit testing. Some of you will say “<em>You are mixing your state tests with GenServer OTP and therefore this is integration testing</em>”. I wouldn’t say you are wrong, but I will definitely say this is a very thin line, at least for me.</p>
<hr>
<p><a class="mention" href="/u/chrismcg" rel="nofollow">@chrismcg</a> I have never used <code>trace</code> for testing before, and I fail to see how I could apply it here. I need to invest more time into this idea, as it looks really cool!</p>
<p>I am however not sure why I need to wait for the</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">:erlang.trace(:new, true, [:call, :return_to])
</code></pre>
<p>call.</p>
<hr>
<aside class="quote no-group" data-username="peerreynders" data-post="17" data-topic="22090">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/peerreynders/48/5826_2.png" class="avatar"> peerreynders:</div>
<blockquote>
<p>The last few topics that you have posted suggest to me that you need a new boundary - for example between the worker process and the http client library.</p>
</blockquote>
</aside>
<p>I usually (try to) make my questions and topics as small and isolated as possible, to make the load on the people reading smaller. Sometimes, this comes at the cost of clarity, which I believe is the case here.</p>
<p>My worker does not depend on any HTTP client. It depends on an HTTP contract, which can be implemented by any client I wish. The boundary is well defined, the way I see it <img src="https://forum.elixirforum.com/images/emoji/apple/stuck_out_tongue.png?v=15" title=":stuck_out_tongue:" class="emoji" alt=":stuck_out_tongue:" loading="lazy" width="20" height="20"></p>
<p>Perhaps, the trouble here is in making it clear it is a boundary. Perhaps you believe I am testing too much the details of my worker which leads you to think I have no boundaries defined. You are one of the most well educated people in testing I have seen and I find it curious how your opinions differ from mine in so many areas. All I can say to defend myself is that I am following a traditional London style TDD, while injecting the dependencies directly without creating Mock modules, because I believe functional injection is the best way of passing dependencies.</p>
<aside class="quote no-group" data-username="peerreynders" data-post="17" data-topic="22090">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/peerreynders/48/5826_2.png" class="avatar"> peerreynders:</div>
<blockquote>
<p>Depending on the necessary sophistication of the test double “test first” may need to take a back seat for a while.</p>
</blockquote>
</aside>
<p>Test first is not always the solution, true. This is actually the refactor of a project that I made and that I consider is a complete disaster. No better time to fix it than now <img src="https://forum.elixirforum.com/images/emoji/apple/smiley.png?v=15" title=":smiley:" class="emoji" alt=":smiley:" loading="lazy" width="20" height="20"></p>
<p>In fact, one of the many problem is that this project has not tests at all <img src="https://forum.elixirforum.com/images/emoji/apple/rofl.png?v=15" title=":rofl:" class="emoji" alt=":rofl:" loading="lazy" width="20" height="20"></p>
<hr>
<aside class="quote no-group" data-username="hauleth" data-post="18" data-topic="22090">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hauleth/48/18942_2.png" class="avatar"> hauleth:</div>
<blockquote>
<p>Especially for you I have prepared small library that I want to publish on Hex later:</p>
</blockquote>
</aside>
<p>Oh my God. Thank you so much. I don;t want to sound … ungrateful, but I am not well versed in erlang yet, so I have trouble understanding what this actually accomplishes. I can only hope this comes with great documentation for dummies like me <img src="https://forum.elixirforum.com/images/emoji/apple/smiley.png?v=15" title=":smiley:" class="emoji" alt=":smiley:" loading="lazy" width="20" height="20"></p>
<hr>
<p><a class="mention" href="/u/sorentwo" rel="nofollow">@sorentwo</a> So you let the tests fail repeatedly until you hit a timeout that is slow enough? I understand you provide a maximum number of tries for the test to run, correct (50 times) ?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="126779" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/test-processes-using-handle-continue/22090/21">Post #20</a>
	                </div>
	            </div>
              <div id="likers-container-126779" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="126779"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-solved cat-solved" title="Marked as solution"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <a class="load-more-button" data-turbo-stream="true" href="/topics/22090/load_more?page=3">Load more posts (11 remaining)</a>
</div></template></turbo-stream>