luke / test / test_util.erl

%% This file is provided to you under the Apache License,
%% Version 2.0 (the "License"); you may not use this file
%% except in compliance with the License.  You may obtain
%% a copy of the License at

%%   http://www.apache.org/licenses/LICENSE-2.0

%% Unless required by applicable law or agreed to in writing,
%% software distributed under the License is distributed on an
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
%% KIND, either express or implied.  See the License for the
%% specific language governing permissions and limitations
%% under the License.

-module(test_util).

-include_lib("eunit/include/eunit.hrl").

-export([start_flow/1, verify_phases/2, verify_results/2, assertDead/1]).

start_flow(FlowDesc) ->
    start_deps([sasl, luke]),
    FlowId = make_ref(),
    {ok, Pid} = luke:new_flow(FlowId, FlowDesc),
    Phases = test_util:verify_phases(Pid, length(FlowDesc)),
    {FlowId, Pid, Phases}.


verify_phases(Pid, Size) ->
    Phases = luke_flow:get_phases(Pid),
    ?assertEqual(Size, length(Phases)),
    Phases.

verify_results(FlowId, none) ->
    receive
        {flow_results, FlowId, done} ->
            throw({error, unexpected_done});
        {flow_results, _PhaseId, FlowId, Results} ->
            throw({error, unexpected_results, Results})
    after 100 ->
            ok
    end;
verify_results(FlowId, results) ->
    receive
        {flow_results, FlowId, done} ->
            throw({error, unexpected_done});
        {flow_results, _PhaseId, FlowId, Results} ->
            {ok, Results}
    after 100 ->
            throw({error, no_results})
    end;
verify_results(FlowId, done) ->
    receive
        {flow_results, FlowId, done} ->
            ok;
        {flow_results, _PhaseId, FlowId, Results} ->
            throw({error, unexpected_results, Results})
    after 100 ->
            throw({error, no_results})
    end.

assertDead(Pids)->
    timer:sleep(25),
    assertDead0(Pids).

assertDead0([]) ->
    ok;
assertDead0([H|T]) when is_list(H) ->
    ok = assertDead0(H),
    assertDead0(T);
assertDead0([H|T]) when is_pid(H) ->
    ?assertMatch(false, erlang:is_process_alive(H)),
    assertDead0(T).

start_deps([]) ->
    ok;
start_deps([H|T]) ->
    case application:start(H) of
        ok ->
            timer:sleep(50),
            start_deps(T);
        {error, {already_started, H}} ->
            start_deps(T);
        Error ->
            throw(Error)
    end.
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.