summaryrefslogtreecommitdiff
path: root/src/dreyfus/test/dreyfus_blacklist_await_test.erl
blob: 82665eb021a844114e3d035e6a0f750586eaef90 (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
% 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(dreyfus_blacklist_await_test).

-include_lib("couch/include/couch_db.hrl").
-include_lib("dreyfus/include/dreyfus.hrl").
-include_lib("eunit/include/eunit.hrl").

-define(DDOC_ID, <<"_design/black_list_doc">>).
-define(INDEX_NAME, <<"my_index">>).
-define(DBNAME, <<"mydb">>).
-define(TIMEOUT, 1000).

start() ->
    test_util:start_couch([dreyfus]).

stop(_) ->
    test_util:stop_couch([dreyfus]).

setup() ->
    ok = meck:new(couch_log),
    ok = meck:expect(couch_log, notice, fun(_Fmt, _Args) ->
        ?debugFmt(_Fmt, _Args)
    end).

teardown(_) ->
    ok = meck:unload(couch_log).

dreyfus_blacklist_await_test_() ->
    {
        "dreyfus black_list_doc await tests",
        {
            setup,
            fun start/0, fun stop/1,
            {
                foreach,
                fun setup/0, fun teardown/1,
                [
                    fun do_not_await_1/0
                ]
            }
        }
    }.

do_not_await_1() ->
    ok = meck:new(dreyfus_index, [passthrough]),
    Denied = lists:flatten([?b2l(?DBNAME), ".", "black_list_doc", ".",
        "my_index"]),
    config:set("dreyfus_blacklist", Denied, "true"),
    dreyfus_test_util:wait_config_change(Denied, "true"),
    Index = #index{dbname=?DBNAME, name=?INDEX_NAME, ddoc_id=?DDOC_ID},
    State = create_state(?DBNAME, Index, nil, nil, []),
    Msg = "Index Blocked from Updating - db: ~p, ddocid: ~p name: ~p",
    Return = wait_log_message(Msg, fun() ->
        {noreply, _NewState} = dreyfus_index:handle_call({await, 1},
            self(), State)
    end),
    ?assertEqual(Return, ok).

wait_log_message(Fmt, Fun) ->
    ok = meck:reset(couch_log),
    Fun(),
    ok = meck:wait(couch_log, '_', [Fmt, '_'], 5000).

create_state(DbName, Index, UPid, IPid, WList) ->
    {state, DbName, Index, UPid, IPid, WList}.