xref: /5.5.2/couchdb/test/etap/002-write-guard.t (revision 29c7ee68)
1#! /usr/bin/env escript
2
3% Licensed under the Apache License, Version 2.0 (the "License"); you may not
4% use this file except in compliance with the License. You may obtain a copy of
5% the License at
6%
7%   http://www.apache.org/licenses/LICENSE-2.0
8%
9% Unless required by applicable law or agreed to in writing, software
10% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12% License for the specific language governing permissions and limitations under
13% the License.
14
15main(_) ->
16    test_util:init_code_path(),
17    etap:plan(6),
18    case (catch test()) of
19        ok ->
20            etap:end_tests();
21        Other ->
22            etap:diag(io_lib:format("Test died abnormally: ~p", [Other])),
23            etap:bail()
24    end,
25    ok.
26
27
28loop() ->
29    receive
30    close -> ok
31    end.
32
33
34test() ->
35    couch_file_write_guard:sup_start_link(),
36    Pid1 = spawn(fun() -> ok end),
37    Pid2 = spawn(fun() -> loop() end),
38
39    %% Added sleep timer in order to allow Pid1 to finish
40    %% else is_process_alive(Pid1) will return true
41    timer:sleep(1000),
42    etap:is(is_process_alive(Pid1), false, "Process is not alive"),
43    etap:is(couch_file_write_guard:add("index.1", Pid1),
44        ok,
45        "Added a file to non-existent pid mapping into write guard"),
46
47    etap:is(couch_file_write_guard:add("index.1", Pid2),
48        ok,
49        "Added file to existing pid mapping into write guard"),
50
51    etap:is(couch_file_write_guard:remove(Pid2),
52        ok,
53        "Removing ets table entry"),
54
55    etap:is(couch_file_write_guard:add("index.1", Pid1),
56        ok,
57        "Added file"),
58
59    etap:is(couch_file_write_guard:remove(Pid2),
60        removing_unadded_file,
61        "Removing unadded entry ets table entry"),
62
63    Pid2 ! close,
64    ok.
65