1%%
2%% %CopyrightBegin%
3%%
4%% Copyright Ericsson AB 2007-2020. All Rights Reserved.
5%%
6%% Licensed under the Apache License, Version 2.0 (the "License");
7%% you may not use this file except in compliance with the License.
8%% You may obtain a copy of the License at
9%%
10%%     http://www.apache.org/licenses/LICENSE-2.0
11%%
12%% Unless required by applicable law or agreed to in writing, software
13%% distributed under the License is distributed on an "AS IS" BASIS,
14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15%% See the License for the specific language governing permissions and
16%% limitations under the License.
17%%
18%% %CopyrightEnd%
19%%
20
21%%
22%%----------------------------------------------------------------------
23%% Purpose: Utility module for the generator users
24%%----------------------------------------------------------------------
25
26-module(megaco_test_generator_lib).
27
28-export([
29	 await_completion/1, await_completion/2
30	]).
31
32
33
34%%%=========================================================================
35%%%  API
36%%%=========================================================================
37
38await_completion(Tags) ->
39    await_completion(Tags, infinity).
40
41await_completion(Tags, Timeout) ->
42    TmrRef = start_timer(Timeout),
43    await_completion(TmrRef, Tags, [], []).
44
45await_completion(TmrRef, [], OK, []) ->
46    stop_timer(TmrRef),
47    {ok, OK};
48await_completion(TmrRef, [], OK, ERROR) ->
49    stop_timer(TmrRef),
50    {error, {OK, ERROR}};
51await_completion(TmrRef, Tags, OK, ERROR) ->
52    receive
53	exec_complete_timeout ->
54            error_msg("Exec complete timeout:"
55                      "~n   Tags:  ~p"
56                      "~n   OK:    ~p"
57                      "~n   ERROR: ~p", [Tags, OK, ERROR]),
58	    {error, {timeout, Tags, OK, ERROR}};
59
60	{exec_complete, Tag, ok, Result} ->
61	    case lists:delete(Tag, Tags) of
62		Tags ->
63		    %% Unknown => ignore
64                    warning_msg("Exec unknown complete with success:"
65                                "~n   Tag:    ~p"
66                                "~n   Result: ~p", [Tag, Result]),
67		    await_completion(TmrRef, Tags, OK, ERROR);
68		Tags2 ->
69                    info_msg("Exec complete with success:"
70                             "~n   Tag:    ~p"
71                             "~n   Result: ~p", [Tag, Result]),
72		    await_completion(TmrRef, Tags2, [{Tag, Result}|OK], ERROR)
73	    end;
74
75	{exec_complete, Tag, error, Reason} ->
76	    case lists:delete(Tag, Tags) of
77		Tags ->
78		    %% Unknown => ignore
79                    warning_msg("Exec unknown complete with failure:"
80                                "~n   Tag:    ~p"
81                                "~n   Reason: ~p", [Tag, Reason]),
82		    await_completion(TmrRef, Tags, OK, ERROR);
83		Tags2 ->
84                    error_msg("Exec complete with failure:"
85                              "~n   Tag:    ~p"
86                              "~n   Reason: ~p", [Tag, Reason]),
87		    await_completion(TmrRef, Tags2, OK, [{Tag, Reason}|ERROR])
88	    end
89    end.
90
91start_timer(infinity) ->
92    undefined;
93start_timer(Timeout) when is_integer(Timeout) andalso (Timeout > 0) ->
94    Msg = exec_complete_timeout,
95    erlang:send_after(Timeout, self(), Msg).
96
97stop_timer(undefined) ->
98    ok;
99stop_timer(TmrRef) ->
100    erlang:cancel_timer(TmrRef).
101
102
103info_msg(F, A) ->
104    error_logger:info_msg(F ++ "~n", A).
105
106warning_msg(F, A) ->
107    error_logger:warning_msg(F ++ "~n", A).
108
109error_msg(F, A) ->
110    error_logger:error_msg(F ++ "~n", A).
111
112