Skip to content

Commit

Permalink
MB-16826 Don't use extra process in misc:wait_for_process.
Browse files Browse the repository at this point in the history
And be careful not to leave garbage messages in process' mailbox.

Change-Id: Id10bb2b093f39a8535ababe78a68c6ccfe0def50
Reviewed-on: http://review.couchbase.org/57092
Tested-by: Aliaksey Artamonau <aliaksiej.artamonau@gmail.com>
Reviewed-by: Poonam D <poonam@couchbase.com>
  • Loading branch information
aartamonau authored and dpoonam committed Nov 17, 2015
1 parent e129ba9 commit 1fd510c
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions src/misc.erl
Expand Up @@ -521,16 +521,14 @@ remove_trailing_whitespace(X) ->

%% Wait for a process.

wait_for_process(Pid, Timeout) ->
Me = self(),
Signal = make_ref(),
spawn(fun() ->
erlang:monitor(process, Pid),
erlang:monitor(process, Me),
receive _ -> Me ! Signal end
end),
receive Signal -> ok
after Timeout -> {error, timeout}
wait_for_process(PidOrName, Timeout) ->
MRef = erlang:monitor(process, PidOrName),
receive
{'DOWN', MRef, process, _, _Reason} ->
ok
after Timeout ->
erlang:demonitor(MRef, [flush]),
{error, timeout}
end.

wait_for_process_test_() ->
Expand Down

0 comments on commit 1fd510c

Please sign in to comment.