summaryrefslogtreecommitdiff
path: root/src/couch_tests/setups/couch_epi_dispatch.erl
blob: 9c0b6b0b0ad2636a1df02cbfc9fc8173e8319b8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
% Licensed 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(couch_epi_dispatch).

-export([
    dispatch/2
]).

%% Exports needed for tests
-export([
    app/0,
    providers/0,
    services/0,
    data_providers/0,
    data_subscriptions/0,
    processes/0,
    notify/3
]).


%% ------------------------------------------------------------------
%% API functions definitions
%% ------------------------------------------------------------------

dispatch(ServiceId, CallbackModule) ->
    couch_tests:new(?MODULE, dispatch,
        setup_dispatch(ServiceId, CallbackModule), teardown_dispatch()).

%% ------------------------------------------------------------------
%% setups and teardowns
%% ------------------------------------------------------------------

setup_dispatch(ServiceId, CallbackModule) ->
    fun(Fixture, Ctx0) ->
        Plugins = application:get_env(couch_epi, plugins, []),
        Ctx1 = start_epi(Ctx0, [CallbackModule]),
        couch_tests:set_state(Fixture, Ctx1, {ServiceId, CallbackModule, Plugins})
    end.

teardown_dispatch() ->
    fun(Fixture, Ctx0) ->
        {ServiceId, _Module, Plugins} = couch_tests:get_state(Fixture, Ctx0),
        stop_epi(Ctx0, ServiceId, Plugins)
    end.

%% ------------------------------------------------------------------
%% Helper functions definitions
%% ------------------------------------------------------------------

start_epi(Ctx0, Plugins) ->
    %% stop in case it's started from other tests..
    Ctx1 = couch_tests:stop_applications([couch_epi], Ctx0),
    application:unload(couch_epi),
    ok = application:load(couch_epi),
    ok = application:set_env(couch_epi, plugins, Plugins),
    couch_tests:start_applications([couch_epi], Ctx1).

stop_epi(Ctx0, ServiceId, Plugins) ->
    ok = application:set_env(couch_epi, plugins, Plugins),
    Handle = couch_epi:get_handle(ServiceId),
    catch couch_epi_module_keeper:reload(Handle),
    Ctx1 = couch_tests:stop_applications([couch_epi], Ctx0),
    application:unload(couch_epi),
    Ctx1.

%% ------------------------------------------------------------------
%% Tests
%% ------------------------------------------------------------------

%% EPI behaviour callbacks
app() -> test_app.
providers() -> [].
services() -> [].
data_providers() -> [].
data_subscriptions() -> [].
processes() -> [].
notify(_, _, _) -> ok.

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

dispatch_test() ->
    ?assert(couch_tests:validate_fixture(dispatch(test_service, ?MODULE))).

-endif.