You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by ch...@apache.org on 2014/09/05 04:30:12 UTC
[33/50] [abbrv] Move files out of test/couchdb into top level test/
folder
http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/0ce84d8e/test/couchdb/couch_stats_tests.erl
----------------------------------------------------------------------
diff --git a/test/couchdb/couch_stats_tests.erl b/test/couchdb/couch_stats_tests.erl
deleted file mode 100644
index d156449..0000000
--- a/test/couchdb/couch_stats_tests.erl
+++ /dev/null
@@ -1,412 +0,0 @@
-% 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_stats_tests).
-
--include("couch_eunit.hrl").
--include_lib("couchdb/couch_db.hrl").
-
--define(STATS_CFG_FIXTURE,
- filename:join([?FIXTURESDIR, "couch_stats_aggregates.cfg"])).
--define(STATS_INI_FIXTURE,
- filename:join([?FIXTURESDIR, "couch_stats_aggregates.ini"])).
--define(TIMEOUT, 1000).
--define(TIMEWAIT, 500).
-
-
-setup_collector() ->
- couch_stats_collector:start(),
- ok.
-
-setup_aggregator(_) ->
- {ok, Pid} = couch_config:start_link([?STATS_INI_FIXTURE]),
- {ok, _} = couch_stats_collector:start(),
- {ok, _} = couch_stats_aggregator:start(?STATS_CFG_FIXTURE),
- Pid.
-
-teardown_collector(_) ->
- couch_stats_collector:stop(),
- ok.
-
-teardown_aggregator(_, Pid) ->
- couch_stats_aggregator:stop(),
- couch_stats_collector:stop(),
- erlang:monitor(process, Pid),
- couch_config:stop(),
- receive
- {'DOWN', _, _, Pid, _} ->
- ok
- after ?TIMEOUT ->
- throw({timeout, config_stop})
- end,
- ok.
-
-
-couch_stats_collector_test_() ->
- {
- "CouchDB stats collector tests",
- {
- foreach,
- fun setup_collector/0, fun teardown_collector/1,
- [
- should_increment_counter(),
- should_decrement_counter(),
- should_increment_and_decrement_counter(),
- should_record_absolute_values(),
- should_clear_absolute_values(),
- should_track_process_count(),
- should_increment_counter_multiple_times_per_pid(),
- should_decrement_counter_on_process_exit(),
- should_decrement_for_each_track_process_count_call_on_exit(),
- should_return_all_counters_and_absolute_values(),
- should_return_incremental_counters(),
- should_return_absolute_values()
- ]
- }
- }.
-
-couch_stats_aggregator_test_() ->
- Funs = [
- fun should_init_empty_aggregate/2,
- fun should_get_empty_aggregate/2,
- fun should_change_stats_on_values_add/2,
- fun should_change_stats_for_all_times_on_values_add/2,
- fun should_change_stats_on_values_change/2,
- fun should_change_stats_for_all_times_on_values_change/2,
- fun should_not_remove_data_after_some_time_for_0_sample/2,
- fun should_remove_data_after_some_time_for_other_samples/2
- ],
- {
- "CouchDB stats aggregator tests",
- [
- {
- "Absolute values",
- {
- foreachx,
- fun setup_aggregator/1, fun teardown_aggregator/2,
- [{absolute, Fun} || Fun <- Funs]
- }
- },
- {
- "Counters",
- {
- foreachx,
- fun setup_aggregator/1, fun teardown_aggregator/2,
- [{counter, Fun} || Fun <- Funs]
- }
- }
- ]
- }.
-
-
-should_increment_counter() ->
- ?_assertEqual(100,
- begin
- AddCount = fun() -> couch_stats_collector:increment(foo) end,
- repeat(AddCount, 100),
- couch_stats_collector:get(foo)
- end).
-
-should_decrement_counter() ->
- ?_assertEqual(67,
- begin
- AddCount = fun() -> couch_stats_collector:increment(foo) end,
- RemCount = fun() -> couch_stats_collector:decrement(foo) end,
- repeat(AddCount, 100),
- repeat(RemCount, 33),
- couch_stats_collector:get(foo)
- end).
-
-should_increment_and_decrement_counter() ->
- ?_assertEqual(0,
- begin
- AddCount = fun() -> couch_stats_collector:increment(foo) end,
- RemCount = fun() -> couch_stats_collector:decrement(foo) end,
- repeat(AddCount, 100),
- repeat(RemCount, 25),
- repeat(AddCount, 10),
- repeat(RemCount, 5),
- repeat(RemCount, 80),
- couch_stats_collector:get(foo)
- end).
-
-should_record_absolute_values() ->
- ?_assertEqual(lists:seq(1, 15),
- begin
- lists:map(fun(Val) ->
- couch_stats_collector:record(bar, Val)
- end, lists:seq(1, 15)),
- couch_stats_collector:get(bar)
- end).
-
-should_clear_absolute_values() ->
- ?_assertEqual(nil,
- begin
- lists:map(fun(Val) ->
- couch_stats_collector:record(bar, Val)
- end, lists:seq(1, 15)),
- couch_stats_collector:clear(bar),
- couch_stats_collector:get(bar)
- end).
-
-should_track_process_count() ->
- ?_assertMatch({_, 1}, spawn_and_count(1)).
-
-should_increment_counter_multiple_times_per_pid() ->
- ?_assertMatch({_, 3}, spawn_and_count(3)).
-
-should_decrement_counter_on_process_exit() ->
- ?_assertEqual(2,
- begin
- {Pid, 1} = spawn_and_count(1),
- spawn_and_count(2),
- RefMon = erlang:monitor(process, Pid),
- Pid ! sepuku,
- receive
- {'DOWN', RefMon, _, _, _} -> ok
- after ?TIMEOUT ->
- throw(timeout)
- end,
- % sleep for awhile to let collector handle the updates
- % suddenly, it couldn't notice process death instantly
- timer:sleep(?TIMEWAIT),
- couch_stats_collector:get(hoopla)
- end).
-
-should_decrement_for_each_track_process_count_call_on_exit() ->
- ?_assertEqual(2,
- begin
- {_, 2} = spawn_and_count(2),
- {Pid, 6} = spawn_and_count(4),
- RefMon = erlang:monitor(process, Pid),
- Pid ! sepuku,
- receive
- {'DOWN', RefMon, _, _, _} -> ok
- after ?TIMEOUT ->
- throw(timeout)
- end,
- timer:sleep(?TIMEWAIT),
- couch_stats_collector:get(hoopla)
- end).
-
-should_return_all_counters_and_absolute_values() ->
- ?_assertEqual([{bar,[1.0,0.0]}, {foo,1}],
- begin
- couch_stats_collector:record(bar, 0.0),
- couch_stats_collector:record(bar, 1.0),
- couch_stats_collector:increment(foo),
- lists:sort(couch_stats_collector:all())
- end).
-
-should_return_incremental_counters() ->
- ?_assertEqual([{foo,1}],
- begin
- couch_stats_collector:record(bar, 0.0),
- couch_stats_collector:record(bar, 1.0),
- couch_stats_collector:increment(foo),
- lists:sort(couch_stats_collector:all(incremental))
- end).
-
-should_return_absolute_values() ->
- ?_assertEqual([{bar,[1.0,0.0]}, {zing, "Z"}],
- begin
- couch_stats_collector:record(bar, 0.0),
- couch_stats_collector:record(bar, 1.0),
- couch_stats_collector:record(zing, 90),
- couch_stats_collector:increment(foo),
- lists:sort(couch_stats_collector:all(absolute))
- end).
-
-should_init_empty_aggregate(absolute, _) ->
- {Aggs} = couch_stats_aggregator:all(),
- ?_assertEqual({[{'11', make_agg(<<"randomosity">>,
- null, null, null, null, null)}]},
- couch_util:get_value(number, Aggs));
-should_init_empty_aggregate(counter, _) ->
- {Aggs} = couch_stats_aggregator:all(),
- ?_assertEqual({[{stuff, make_agg(<<"yay description">>,
- null, null, null, null, null)}]},
- couch_util:get_value(testing, Aggs)).
-
-should_get_empty_aggregate(absolute, _) ->
- ?_assertEqual(make_agg(<<"randomosity">>, null, null, null, null, null),
- couch_stats_aggregator:get_json({number, '11'}));
-should_get_empty_aggregate(counter, _) ->
- ?_assertEqual(make_agg(<<"yay description">>, null, null, null, null, null),
- couch_stats_aggregator:get_json({testing, stuff})).
-
-should_change_stats_on_values_add(absolute, _) ->
- lists:foreach(fun(X) ->
- couch_stats_collector:record({number, 11}, X)
- end, lists:seq(0, 10)),
- couch_stats_aggregator:collect_sample(),
- ?_assertEqual(make_agg(<<"randomosity">>, 5.0, 5.0, null, 5.0, 5.0),
- couch_stats_aggregator:get_json({number, 11}));
-should_change_stats_on_values_add(counter, _) ->
- lists:foreach(fun(_) ->
- couch_stats_collector:increment({testing, stuff})
- end, lists:seq(1, 100)),
- couch_stats_aggregator:collect_sample(),
- ?_assertEqual(make_agg(<<"yay description">>, 100.0, 100.0, null, 100, 100),
- couch_stats_aggregator:get_json({testing, stuff})).
-
-should_change_stats_for_all_times_on_values_add(absolute, _) ->
- lists:foreach(fun(X) ->
- couch_stats_collector:record({number, 11}, X)
- end, lists:seq(0, 10)),
- couch_stats_aggregator:collect_sample(),
- ?_assertEqual(make_agg(<<"randomosity">>, 5.0, 5.0, null, 5.0, 5.0),
- couch_stats_aggregator:get_json({number, 11}, 1));
-should_change_stats_for_all_times_on_values_add(counter, _) ->
- lists:foreach(fun(_) ->
- couch_stats_collector:increment({testing, stuff})
- end, lists:seq(1, 100)),
- couch_stats_aggregator:collect_sample(),
- ?_assertEqual(make_agg(<<"yay description">>, 100.0, 100.0, null, 100, 100),
- couch_stats_aggregator:get_json({testing, stuff}, 1)).
-
-should_change_stats_on_values_change(absolute, _) ->
- ?_assertEqual(make_agg(<<"randomosity">>, 20.0, 10.0, 7.071, 5.0, 15.0),
- begin
- lists:foreach(fun(X) ->
- couch_stats_collector:record({number, 11}, X)
- end, lists:seq(0, 10)),
- couch_stats_aggregator:collect_sample(),
- timer:sleep(?TIMEWAIT),
- couch_stats_collector:record({number, 11}, 15),
- couch_stats_aggregator:collect_sample(),
- couch_stats_aggregator:get_json({number, 11})
- end);
-should_change_stats_on_values_change(counter, _) ->
- ?_assertEqual(make_agg(<<"yay description">>, 100.0, 50.0, 70.711, 0, 100),
- begin
- lists:foreach(fun(_) ->
- couch_stats_collector:increment({testing, stuff})
- end, lists:seq(1, 100)),
- couch_stats_aggregator:collect_sample(),
- timer:sleep(?TIMEWAIT),
- couch_stats_aggregator:collect_sample(),
- couch_stats_aggregator:get_json({testing, stuff})
- end).
-
-should_change_stats_for_all_times_on_values_change(absolute, _) ->
- ?_assertEqual(make_agg(<<"randomosity">>, 20.0, 10.0, 7.071, 5.0, 15.0),
- begin
- lists:foreach(fun(X) ->
- couch_stats_collector:record({number, 11}, X)
- end, lists:seq(0, 10)),
- couch_stats_aggregator:collect_sample(),
- timer:sleep(?TIMEWAIT),
- couch_stats_collector:record({number, 11}, 15),
- couch_stats_aggregator:collect_sample(),
- couch_stats_aggregator:get_json({number, 11}, 1)
- end);
-should_change_stats_for_all_times_on_values_change(counter, _) ->
- ?_assertEqual(make_agg(<<"yay description">>, 100.0, 50.0, 70.711, 0, 100),
- begin
- lists:foreach(fun(_) ->
- couch_stats_collector:increment({testing, stuff})
- end, lists:seq(1, 100)),
- couch_stats_aggregator:collect_sample(),
- timer:sleep(?TIMEWAIT),
- couch_stats_aggregator:collect_sample(),
- couch_stats_aggregator:get_json({testing, stuff}, 1)
- end).
-
-should_not_remove_data_after_some_time_for_0_sample(absolute, _) ->
- ?_assertEqual(make_agg(<<"randomosity">>, 20.0, 10.0, 7.071, 5.0, 15.0),
- begin
- lists:foreach(fun(X) ->
- couch_stats_collector:record({number, 11}, X)
- end, lists:seq(0, 10)),
- couch_stats_aggregator:collect_sample(),
- timer:sleep(?TIMEWAIT),
- couch_stats_collector:record({number, 11}, 15),
- couch_stats_aggregator:collect_sample(),
- timer:sleep(?TIMEWAIT),
- couch_stats_aggregator:collect_sample(),
- couch_stats_aggregator:get_json({number, 11})
- end);
-should_not_remove_data_after_some_time_for_0_sample(counter, _) ->
- ?_assertEqual(make_agg(<<"yay description">>, 100.0, 33.333, 57.735, 0, 100),
- begin
- lists:foreach(fun(_) ->
- couch_stats_collector:increment({testing, stuff})
- end, lists:seq(1, 100)),
- couch_stats_aggregator:collect_sample(),
- timer:sleep(?TIMEWAIT),
- couch_stats_aggregator:collect_sample(),
- timer:sleep(?TIMEWAIT),
- couch_stats_aggregator:collect_sample(),
- couch_stats_aggregator:get_json({testing, stuff})
- end).
-
-should_remove_data_after_some_time_for_other_samples(absolute, _) ->
- ?_assertEqual(make_agg(<<"randomosity">>, 15.0, 15.0, null, 15.0, 15.0),
- begin
- lists:foreach(fun(X) ->
- couch_stats_collector:record({number, 11}, X)
- end, lists:seq(0, 10)),
- couch_stats_aggregator:collect_sample(),
- timer:sleep(?TIMEWAIT),
- couch_stats_collector:record({number, 11}, 15),
- couch_stats_aggregator:collect_sample(),
- timer:sleep(?TIMEWAIT),
- couch_stats_aggregator:collect_sample(),
- couch_stats_aggregator:get_json({number, 11}, 1)
- end);
-should_remove_data_after_some_time_for_other_samples(counter, _) ->
- ?_assertEqual(make_agg(<<"yay description">>, 0, 0.0, 0.0, 0, 0),
- begin
- lists:foreach(fun(_) ->
- couch_stats_collector:increment({testing, stuff})
- end, lists:seq(1, 100)),
- couch_stats_aggregator:collect_sample(),
- timer:sleep(?TIMEWAIT),
- couch_stats_aggregator:collect_sample(),
- timer:sleep(?TIMEWAIT),
- couch_stats_aggregator:collect_sample(),
- couch_stats_aggregator:get_json({testing, stuff}, 1)
- end).
-
-
-spawn_and_count(N) ->
- Self = self(),
- Pid = spawn(fun() ->
- lists:foreach(
- fun(_) ->
- couch_stats_collector:track_process_count(hoopla)
- end, lists:seq(1,N)),
- Self ! reporting,
- receive
- sepuku -> ok
- end
- end),
- receive reporting -> ok end,
- {Pid, couch_stats_collector:get(hoopla)}.
-
-repeat(_, 0) ->
- ok;
-repeat(Fun, Count) ->
- Fun(),
- repeat(Fun, Count-1).
-
-make_agg(Desc, Sum, Mean, StdDev, Min, Max) ->
- {[
- {description, Desc},
- {current, Sum},
- {sum, Sum},
- {mean, Mean},
- {stddev, StdDev},
- {min, Min},
- {max, Max}
- ]}.
http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/0ce84d8e/test/couchdb/couch_stream_tests.erl
----------------------------------------------------------------------
diff --git a/test/couchdb/couch_stream_tests.erl b/test/couchdb/couch_stream_tests.erl
deleted file mode 100644
index 335a2fe..0000000
--- a/test/couchdb/couch_stream_tests.erl
+++ /dev/null
@@ -1,100 +0,0 @@
-% 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_stream_tests).
-
--include("couch_eunit.hrl").
-
-
-setup() ->
- {ok, Fd} = couch_file:open(?tempfile(), [create, overwrite]),
- {ok, Stream} = couch_stream:open(Fd),
- {Fd, Stream}.
-
-teardown({Fd, _}) ->
- ok = couch_file:close(Fd).
-
-
-stream_test_() ->
- {
- "CouchDB stream tests",
- {
- foreach,
- fun setup/0, fun teardown/1,
- [
- fun should_write/1,
- fun should_write_consecutive/1,
- fun should_write_empty_binary/1,
- fun should_return_file_pointers_on_close/1,
- fun should_return_stream_size_on_close/1,
- fun should_return_valid_pointers/1,
- fun should_recall_last_pointer_position/1,
- fun should_stream_more_with_4K_chunk_size/1
- ]
- }
- }.
-
-
-should_write({_, Stream}) ->
- ?_assertEqual(ok, couch_stream:write(Stream, <<"food">>)).
-
-should_write_consecutive({_, Stream}) ->
- couch_stream:write(Stream, <<"food">>),
- ?_assertEqual(ok, couch_stream:write(Stream, <<"foob">>)).
-
-should_write_empty_binary({_, Stream}) ->
- ?_assertEqual(ok, couch_stream:write(Stream, <<>>)).
-
-should_return_file_pointers_on_close({_, Stream}) ->
- couch_stream:write(Stream, <<"foodfoob">>),
- {Ptrs, _, _, _, _} = couch_stream:close(Stream),
- ?_assertEqual([{0, 8}], Ptrs).
-
-should_return_stream_size_on_close({_, Stream}) ->
- couch_stream:write(Stream, <<"foodfoob">>),
- {_, Length, _, _, _} = couch_stream:close(Stream),
- ?_assertEqual(8, Length).
-
-should_return_valid_pointers({Fd, Stream}) ->
- couch_stream:write(Stream, <<"foodfoob">>),
- {Ptrs, _, _, _, _} = couch_stream:close(Stream),
- ?_assertEqual(<<"foodfoob">>, read_all(Fd, Ptrs)).
-
-should_recall_last_pointer_position({Fd, Stream}) ->
- couch_stream:write(Stream, <<"foodfoob">>),
- {_, _, _, _, _} = couch_stream:close(Stream),
- {ok, ExpPtr} = couch_file:bytes(Fd),
- {ok, Stream2} = couch_stream:open(Fd),
- ZeroBits = <<0:(8 * 10)>>,
- OneBits = <<1:(8 * 10)>>,
- ok = couch_stream:write(Stream2, OneBits),
- ok = couch_stream:write(Stream2, ZeroBits),
- {Ptrs, 20, _, _, _} = couch_stream:close(Stream2),
- [{ExpPtr, 20}] = Ptrs,
- AllBits = iolist_to_binary([OneBits, ZeroBits]),
- ?_assertEqual(AllBits, read_all(Fd, Ptrs)).
-
-should_stream_more_with_4K_chunk_size({Fd, _}) ->
- {ok, Stream} = couch_stream:open(Fd, [{buffer_size, 4096}]),
- lists:foldl(
- fun(_, Acc) ->
- Data = <<"a1b2c">>,
- couch_stream:write(Stream, Data),
- [Data | Acc]
- end, [], lists:seq(1, 1024)),
- ?_assertMatch({[{0, 4100}, {4106, 1020}], 5120, _, _, _},
- couch_stream:close(Stream)).
-
-
-read_all(Fd, PosList) ->
- Data = couch_stream:foldl(Fd, PosList, fun(Bin, Acc) -> [Bin, Acc] end, []),
- iolist_to_binary(Data).
http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/0ce84d8e/test/couchdb/couch_task_status_tests.erl
----------------------------------------------------------------------
diff --git a/test/couchdb/couch_task_status_tests.erl b/test/couchdb/couch_task_status_tests.erl
deleted file mode 100644
index f71ad2b..0000000
--- a/test/couchdb/couch_task_status_tests.erl
+++ /dev/null
@@ -1,225 +0,0 @@
-% 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_task_status_tests).
-
--include("couch_eunit.hrl").
--include_lib("couchdb/couch_db.hrl").
-
--define(TIMEOUT, 1000).
-
-
-setup() ->
- {ok, TaskStatusPid} = couch_task_status:start_link(),
- TaskUpdaterPid = spawn(fun() -> loop() end),
- {TaskStatusPid, TaskUpdaterPid}.
-
-teardown({TaskStatusPid, _}) ->
- erlang:monitor(process, TaskStatusPid),
- couch_task_status:stop(),
- receive
- {'DOWN', _, _, TaskStatusPid, _} ->
- ok
- after ?TIMEOUT ->
- throw(timeout_error)
- end.
-
-
-couch_task_status_test_() ->
- {
- "CouchDB task status updates",
- {
- foreach,
- fun setup/0, fun teardown/1,
- [
- fun should_register_task/1,
- fun should_set_task_startup_time/1,
- fun should_have_update_time_as_startup_before_any_progress/1,
- fun should_set_task_type/1,
- fun should_not_register_multiple_tasks_for_same_pid/1,
- fun should_set_task_progress/1,
- fun should_update_task_progress/1,
- fun should_update_time_changes_on_task_progress/1,
- fun should_control_update_frequency/1,
- fun should_reset_control_update_frequency/1,
- fun should_track_multiple_tasks/1,
- fun should_finish_task/1
-
- ]
- }
- }.
-
-
-should_register_task({_, Pid}) ->
- ok = call(Pid, add, [{type, replication}, {progress, 0}]),
- ?_assertEqual(1, length(couch_task_status:all())).
-
-should_set_task_startup_time({_, Pid}) ->
- ok = call(Pid, add, [{type, replication}, {progress, 0}]),
- ?_assert(is_integer(get_task_prop(Pid, started_on))).
-
-should_have_update_time_as_startup_before_any_progress({_, Pid}) ->
- ok = call(Pid, add, [{type, replication}, {progress, 0}]),
- StartTime = get_task_prop(Pid, started_on),
- ?_assertEqual(StartTime, get_task_prop(Pid, updated_on)).
-
-should_set_task_type({_, Pid}) ->
- ok = call(Pid, add, [{type, replication}, {progress, 0}]),
- ?_assertEqual(replication, get_task_prop(Pid, type)).
-
-should_not_register_multiple_tasks_for_same_pid({_, Pid}) ->
- ok = call(Pid, add, [{type, replication}, {progress, 0}]),
- ?_assertEqual({add_task_error, already_registered},
- call(Pid, add, [{type, compaction}, {progress, 0}])).
-
-should_set_task_progress({_, Pid}) ->
- ok = call(Pid, add, [{type, replication}, {progress, 0}]),
- ?_assertEqual(0, get_task_prop(Pid, progress)).
-
-should_update_task_progress({_, Pid}) ->
- ok = call(Pid, add, [{type, replication}, {progress, 0}]),
- call(Pid, update, [{progress, 25}]),
- ?_assertEqual(25, get_task_prop(Pid, progress)).
-
-should_update_time_changes_on_task_progress({_, Pid}) ->
- ?_assert(
- begin
- ok = call(Pid, add, [{type, replication}, {progress, 0}]),
- ok = timer:sleep(1000), % sleep awhile to customize update time
- call(Pid, update, [{progress, 25}]),
- get_task_prop(Pid, updated_on) > get_task_prop(Pid, started_on)
- end).
-
-should_control_update_frequency({_, Pid}) ->
- ?_assertEqual(66,
- begin
- ok = call(Pid, add, [{type, replication}, {progress, 0}]),
- call(Pid, update, [{progress, 50}]),
- call(Pid, update_frequency, 500),
- call(Pid, update, [{progress, 66}]),
- call(Pid, update, [{progress, 77}]),
- get_task_prop(Pid, progress)
- end).
-
-should_reset_control_update_frequency({_, Pid}) ->
- ?_assertEqual(87,
- begin
- ok = call(Pid, add, [{type, replication}, {progress, 0}]),
- call(Pid, update, [{progress, 50}]),
- call(Pid, update_frequency, 500),
- call(Pid, update, [{progress, 66}]),
- call(Pid, update, [{progress, 77}]),
- call(Pid, update_frequency, 0),
- call(Pid, update, [{progress, 87}]),
- get_task_prop(Pid, progress)
- end).
-
-should_track_multiple_tasks(_) ->
- ?_assert(run_multiple_tasks()).
-
-should_finish_task({_, Pid}) ->
- ok = call(Pid, add, [{type, replication}, {progress, 0}]),
- ?assertEqual(1, length(couch_task_status:all())),
- ok = call(Pid, done),
- ?_assertEqual(0, length(couch_task_status:all())).
-
-
-run_multiple_tasks() ->
- Pid1 = spawn(fun() -> loop() end),
- Pid2 = spawn(fun() -> loop() end),
- Pid3 = spawn(fun() -> loop() end),
- call(Pid1, add, [{type, replication}, {progress, 0}]),
- call(Pid2, add, [{type, compaction}, {progress, 0}]),
- call(Pid3, add, [{type, indexer}, {progress, 0}]),
-
- ?assertEqual(3, length(couch_task_status:all())),
- ?assertEqual(replication, get_task_prop(Pid1, type)),
- ?assertEqual(compaction, get_task_prop(Pid2, type)),
- ?assertEqual(indexer, get_task_prop(Pid3, type)),
-
- call(Pid2, update, [{progress, 33}]),
- call(Pid3, update, [{progress, 42}]),
- call(Pid1, update, [{progress, 11}]),
- ?assertEqual(42, get_task_prop(Pid3, progress)),
- call(Pid1, update, [{progress, 72}]),
- ?assertEqual(72, get_task_prop(Pid1, progress)),
- ?assertEqual(33, get_task_prop(Pid2, progress)),
-
- call(Pid1, done),
- ?assertEqual(2, length(couch_task_status:all())),
- call(Pid3, done),
- ?assertEqual(1, length(couch_task_status:all())),
- call(Pid2, done),
- ?assertEqual(0, length(couch_task_status:all())),
-
- true.
-
-
-loop() ->
- receive
- {add, Props, From} ->
- Resp = couch_task_status:add_task(Props),
- From ! {ok, self(), Resp},
- loop();
- {update, Props, From} ->
- Resp = couch_task_status:update(Props),
- From ! {ok, self(), Resp},
- loop();
- {update_frequency, Msecs, From} ->
- Resp = couch_task_status:set_update_frequency(Msecs),
- From ! {ok, self(), Resp},
- loop();
- {done, From} ->
- From ! {ok, self(), ok}
- end.
-
-call(Pid, Command) ->
- Pid ! {Command, self()},
- wait(Pid).
-
-call(Pid, Command, Arg) ->
- Pid ! {Command, Arg, self()},
- wait(Pid).
-
-wait(Pid) ->
- receive
- {ok, Pid, Msg} ->
- Msg
- after ?TIMEOUT ->
- throw(timeout_error)
- end.
-
-get_task_prop(Pid, Prop) ->
- From = list_to_binary(pid_to_list(Pid)),
- Element = lists:foldl(
- fun(PropList, Acc) ->
- case couch_util:get_value(pid, PropList) of
- From ->
- [PropList | Acc];
- _ ->
- Acc
- end
- end,
- [], couch_task_status:all()
- ),
- case couch_util:get_value(Prop, hd(Element), nil) of
- nil ->
- erlang:error({assertion_failed,
- [{module, ?MODULE},
- {line, ?LINE},
- {reason, "Could not get property '"
- ++ couch_util:to_list(Prop)
- ++ "' for task "
- ++ pid_to_list(Pid)}]});
- Value ->
- Value
- end.
http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/0ce84d8e/test/couchdb/couch_util_tests.erl
----------------------------------------------------------------------
diff --git a/test/couchdb/couch_util_tests.erl b/test/couchdb/couch_util_tests.erl
deleted file mode 100644
index 8e24e72..0000000
--- a/test/couchdb/couch_util_tests.erl
+++ /dev/null
@@ -1,136 +0,0 @@
-% 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_util_tests).
-
--include("couch_eunit.hrl").
-
-
-setup() ->
- %% We cannot start driver from here since it becomes bounded to eunit
- %% master process and the next couch_server_sup:start_link call will
- %% fail because server couldn't load driver since it already is.
- %%
- %% On other hand, we cannot unload driver here due to
- %% {error, not_loaded_by_this_process} while it is. Any ideas is welcome.
- %%
- couch_server_sup:start_link(?CONFIG_CHAIN),
- %% couch_config:start_link(?CONFIG_CHAIN),
- %% {ok, _} = couch_drv:start_link(),
- ok.
-
-teardown(_) ->
- couch_server_sup:stop(),
- %% couch_config:stop(),
- %% erl_ddll:unload_driver(couch_icu_driver),
- ok.
-
-
-collation_test_() ->
- {
- "Collation tests",
- [
- {
- setup,
- fun setup/0, fun teardown/1,
- [
- should_collate_ascii(),
- should_collate_non_ascii()
- ]
- }
- ]
- }.
-
-should_collate_ascii() ->
- ?_assertEqual(1, couch_util:collate(<<"foo">>, <<"bar">>)).
-
-should_collate_non_ascii() ->
- ?_assertEqual(-1, couch_util:collate(<<"A">>, <<"aa">>)).
-
-to_existed_atom_test() ->
- ?assert(couch_util:to_existing_atom(true)),
- ?assertMatch(foo, couch_util:to_existing_atom(<<"foo">>)),
- ?assertMatch(foobarbaz, couch_util:to_existing_atom("foobarbaz")).
-
-implode_test() ->
- ?assertEqual([1, 38, 2, 38, 3], couch_util:implode([1, 2, 3], "&")).
-
-trim_test() ->
- lists:map(fun(S) -> ?assertEqual("foo", couch_util:trim(S)) end,
- [" foo", "foo ", "\tfoo", " foo ", "foo\t", "foo\n", "\nfoo"]).
-
-abs_pathname_test() ->
- {ok, Cwd} = file:get_cwd(),
- ?assertEqual(Cwd ++ "/foo", couch_util:abs_pathname("./foo")).
-
-flush_test() ->
- ?assertNot(couch_util:should_flush()),
- AcquireMem = fun() ->
- _IntsToAGazillion = lists:seq(1, 200000),
- _LotsOfData = lists:map(fun(_) -> <<"foobar">> end,
- lists:seq(1, 500000)),
- _BigBin = list_to_binary(_LotsOfData),
-
- %% Allocation 200K tuples puts us above the memory threshold
- %% Originally, there should be:
- %% ?assertNot(should_flush())
- %% however, unlike for etap test, GC collects all allocated bits
- %% making this conditions fail. So we have to invert the condition
- %% since GC works, cleans the memory and everything is fine.
- ?assertNot(couch_util:should_flush())
- end,
- AcquireMem(),
-
- %% Checking to flush invokes GC
- ?assertNot(couch_util:should_flush()).
-
-verify_test() ->
- ?assert(couch_util:verify("It4Vooya", "It4Vooya")),
- ?assertNot(couch_util:verify("It4VooyaX", "It4Vooya")),
- ?assert(couch_util:verify(<<"ahBase3r">>, <<"ahBase3r">>)),
- ?assertNot(couch_util:verify(<<"ahBase3rX">>, <<"ahBase3r">>)),
- ?assertNot(couch_util:verify(nil, <<"ahBase3r">>)).
-
-find_in_binary_test_() ->
- Cases = [
- {<<"foo">>, <<"foobar">>, {exact, 0}},
- {<<"foo">>, <<"foofoo">>, {exact, 0}},
- {<<"foo">>, <<"barfoo">>, {exact, 3}},
- {<<"foo">>, <<"barfo">>, {partial, 3}},
- {<<"f">>, <<"fobarfff">>, {exact, 0}},
- {<<"f">>, <<"obarfff">>, {exact, 4}},
- {<<"f">>, <<"obarggf">>, {exact, 6}},
- {<<"f">>, <<"f">>, {exact, 0}},
- {<<"f">>, <<"g">>, not_found},
- {<<"foo">>, <<"f">>, {partial, 0}},
- {<<"foo">>, <<"g">>, not_found},
- {<<"foo">>, <<"">>, not_found},
- {<<"fofo">>, <<"foofo">>, {partial, 3}},
- {<<"foo">>, <<"gfobarfo">>, {partial, 6}},
- {<<"foo">>, <<"gfobarf">>, {partial, 6}},
- {<<"foo">>, <<"gfobar">>, not_found},
- {<<"fog">>, <<"gbarfogquiz">>, {exact, 4}},
- {<<"ggg">>, <<"ggg">>, {exact, 0}},
- {<<"ggg">>, <<"ggggg">>, {exact, 0}},
- {<<"ggg">>, <<"bggg">>, {exact, 1}},
- {<<"ggg">>, <<"bbgg">>, {partial, 2}},
- {<<"ggg">>, <<"bbbg">>, {partial, 3}},
- {<<"ggg">>, <<"bgbggbggg">>, {exact, 6}},
- {<<"ggg">>, <<"bgbggb">>, not_found}
- ],
- lists:map(
- fun({Needle, Haystack, Result}) ->
- Msg = lists:flatten(io_lib:format("Looking for ~s in ~s",
- [Needle, Haystack])),
- {Msg, ?_assertMatch(Result,
- couch_util:find_in_binary(Needle, Haystack))}
- end, Cases).
http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/0ce84d8e/test/couchdb/couch_uuids_tests.erl
----------------------------------------------------------------------
diff --git a/test/couchdb/couch_uuids_tests.erl b/test/couchdb/couch_uuids_tests.erl
deleted file mode 100644
index ea1d034..0000000
--- a/test/couchdb/couch_uuids_tests.erl
+++ /dev/null
@@ -1,161 +0,0 @@
-% 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_uuids_tests).
-
--include("couch_eunit.hrl").
-
--define(TIMEOUT_S, 20).
-
-
-setup() ->
- {ok, Pid} = couch_config:start_link(?CONFIG_CHAIN),
- erlang:monitor(process, Pid),
- couch_uuids:start(),
- Pid.
-
-setup(Opts) ->
- Pid = setup(),
- lists:foreach(
- fun({Option, Value}) ->
- couch_config:set("uuids", Option, Value, false)
- end, Opts),
- Pid.
-
-teardown(Pid) ->
- couch_uuids:stop(),
- couch_config:stop(),
- receive
- {'DOWN', _, _, Pid, _} -> ok
- after
- 1000 -> throw({timeout_error, config_stop})
- end.
-
-teardown(_, Pid) ->
- teardown(Pid).
-
-
-default_test_() ->
- {
- "Default UUID algorithm",
- {
- setup,
- fun setup/0, fun teardown/1,
- fun should_be_unique/1
- }
- }.
-
-sequential_test_() ->
- Opts = [{"algorithm", "sequential"}],
- Cases = [
- fun should_be_unique/2,
- fun should_increment_monotonically/2,
- fun should_rollover/2
- ],
- {
- "UUID algorithm: sequential",
- {
- foreachx,
- fun setup/1, fun teardown/2,
- [{Opts, Fun} || Fun <- Cases]
- }
- }.
-
-utc_test_() ->
- Opts = [{"algorithm", "utc_random"}],
- Cases = [
- fun should_be_unique/2,
- fun should_increment_monotonically/2
- ],
- {
- "UUID algorithm: utc_random",
- {
- foreachx,
- fun setup/1, fun teardown/2,
- [{Opts, Fun} || Fun <- Cases]
- }
- }.
-
-utc_id_suffix_test_() ->
- Opts = [{"algorithm", "utc_id"}, {"utc_id_suffix", "bozo"}],
- Cases = [
- fun should_be_unique/2,
- fun should_increment_monotonically/2,
- fun should_preserve_suffix/2
- ],
- {
- "UUID algorithm: utc_id",
- {
- foreachx,
- fun setup/1, fun teardown/2,
- [{Opts, Fun} || Fun <- Cases]
- }
- }.
-
-
-should_be_unique() ->
- %% this one may really runs for too long on slow hosts
- {timeout, ?TIMEOUT_S, ?_assert(test_unique(10000, [couch_uuids:new()]))}.
-should_be_unique(_) ->
- should_be_unique().
-should_be_unique(_, _) ->
- should_be_unique().
-
-should_increment_monotonically(_, _) ->
- ?_assert(couch_uuids:new() < couch_uuids:new()).
-
-should_rollover(_, _) ->
- ?_test(begin
- UUID = binary_to_list(couch_uuids:new()),
- Prefix = element(1, lists:split(26, UUID)),
- N = gen_until_pref_change(Prefix, 0),
- ?assert(N >= 5000 andalso N =< 11000)
- end).
-
-should_preserve_suffix(_, _) ->
- ?_test(begin
- UUID = binary_to_list(couch_uuids:new()),
- Suffix = get_suffix(UUID),
- ?assert(test_same_suffix(10000, Suffix))
- end).
-
-
-test_unique(0, _) ->
- true;
-test_unique(N, UUIDs) ->
- UUID = couch_uuids:new(),
- ?assertNot(lists:member(UUID, UUIDs)),
- test_unique(N - 1, [UUID| UUIDs]).
-
-get_prefix(UUID) ->
- element(1, lists:split(26, binary_to_list(UUID))).
-
-gen_until_pref_change(_, Count) when Count > 8251 ->
- Count;
-gen_until_pref_change(Prefix, N) ->
- case get_prefix(couch_uuids:new()) of
- Prefix -> gen_until_pref_change(Prefix, N + 1);
- _ -> N
- end.
-
-get_suffix(UUID) when is_binary(UUID) ->
- get_suffix(binary_to_list(UUID));
-get_suffix(UUID) ->
- element(2, lists:split(14, UUID)).
-
-test_same_suffix(0, _) ->
- true;
-test_same_suffix(N, Suffix) ->
- case get_suffix(couch_uuids:new()) of
- Suffix -> test_same_suffix(N - 1, Suffix);
- _ -> false
- end.
http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/0ce84d8e/test/couchdb/couch_work_queue_tests.erl
----------------------------------------------------------------------
diff --git a/test/couchdb/couch_work_queue_tests.erl b/test/couchdb/couch_work_queue_tests.erl
deleted file mode 100644
index 8a463b5..0000000
--- a/test/couchdb/couch_work_queue_tests.erl
+++ /dev/null
@@ -1,393 +0,0 @@
-% 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_work_queue_tests).
-
--include("couch_eunit.hrl").
-
--define(TIMEOUT, 100).
-
-
-setup(Opts) ->
- {ok, Q} = couch_work_queue:new(Opts),
- Producer = spawn_producer(Q),
- Consumer = spawn_consumer(Q),
- {Q, Producer, Consumer}.
-
-setup_max_items() ->
- setup([{max_items, 3}]).
-
-setup_max_size() ->
- setup([{max_size, 160}]).
-
-setup_max_items_and_size() ->
- setup([{max_size, 160}, {max_items, 3}]).
-
-setup_multi_workers() ->
- {Q, Producer, Consumer1} = setup([{max_size, 160},
- {max_items, 3},
- {multi_workers, true}]),
- Consumer2 = spawn_consumer(Q),
- Consumer3 = spawn_consumer(Q),
- {Q, Producer, [Consumer1, Consumer2, Consumer3]}.
-
-teardown({Q, Producer, Consumers}) when is_list(Consumers) ->
- % consume all to unblock and let producer/consumer stop without timeout
- [consume(Consumer, all) || Consumer <- Consumers],
-
- ok = close_queue(Q),
- ok = stop(Producer, "producer"),
- R = [stop(Consumer, "consumer") || Consumer <- Consumers],
- R = [ok || _ <- Consumers],
- ok;
-teardown({Q, Producer, Consumer}) ->
- teardown({Q, Producer, [Consumer]}).
-
-
-single_consumer_test_() ->
- {
- "Single producer and consumer",
- [
- {
- "Queue with 3 max items",
- {
- foreach,
- fun setup_max_items/0, fun teardown/1,
- single_consumer_max_item_count() ++ common_cases()
- }
- },
- {
- "Queue with max size of 160 bytes",
- {
- foreach,
- fun setup_max_size/0, fun teardown/1,
- single_consumer_max_size() ++ common_cases()
- }
- },
- {
- "Queue with max size of 160 bytes and 3 max items",
- {
- foreach,
- fun setup_max_items_and_size/0, fun teardown/1,
- single_consumer_max_items_and_size() ++ common_cases()
- }
- }
- ]
- }.
-
-multiple_consumers_test_() ->
- {
- "Single producer and multiple consumers",
- [
- {
- "Queue with max size of 160 bytes and 3 max items",
- {
- foreach,
- fun setup_multi_workers/0, fun teardown/1,
- common_cases() ++ multiple_consumers()
- }
-
- }
- ]
- }.
-
-common_cases()->
- [
- fun should_block_consumer_on_dequeue_from_empty_queue/1,
- fun should_consume_right_item/1,
- fun should_timeout_on_close_non_empty_queue/1,
- fun should_not_block_producer_for_non_empty_queue_after_close/1,
- fun should_be_closed/1
- ].
-
-single_consumer_max_item_count()->
- [
- fun should_have_no_items_for_new_queue/1,
- fun should_block_producer_on_full_queue_count/1,
- fun should_receive_first_queued_item/1,
- fun should_consume_multiple_items/1,
- fun should_consume_all/1
- ].
-
-single_consumer_max_size()->
- [
- fun should_have_zero_size_for_new_queue/1,
- fun should_block_producer_on_full_queue_size/1,
- fun should_increase_queue_size_on_produce/1,
- fun should_receive_first_queued_item/1,
- fun should_consume_multiple_items/1,
- fun should_consume_all/1
- ].
-
-single_consumer_max_items_and_size() ->
- single_consumer_max_item_count() ++ single_consumer_max_size().
-
-multiple_consumers() ->
- [
- fun should_have_zero_size_for_new_queue/1,
- fun should_have_no_items_for_new_queue/1,
- fun should_increase_queue_size_on_produce/1
- ].
-
-
-should_have_no_items_for_new_queue({Q, _, _}) ->
- ?_assertEqual(0, couch_work_queue:item_count(Q)).
-
-should_have_zero_size_for_new_queue({Q, _, _}) ->
- ?_assertEqual(0, couch_work_queue:size(Q)).
-
-should_block_consumer_on_dequeue_from_empty_queue({_, _, Consumers}) when is_list(Consumers) ->
- [consume(C, 2) || C <- Consumers],
- Pongs = [ping(C) || C <- Consumers],
- ?_assertEqual([timeout, timeout, timeout], Pongs);
-should_block_consumer_on_dequeue_from_empty_queue({_, _, Consumer}) ->
- consume(Consumer, 1),
- Pong = ping(Consumer),
- ?_assertEqual(timeout, Pong).
-
-should_consume_right_item({Q, Producer, Consumers}) when is_list(Consumers) ->
- [consume(C, 3) || C <- Consumers],
-
- Item1 = produce(Producer, 10),
- ok = ping(Producer),
- ?assertEqual(0, couch_work_queue:item_count(Q)),
- ?assertEqual(0, couch_work_queue:size(Q)),
-
- Item2 = produce(Producer, 10),
- ok = ping(Producer),
- ?assertEqual(0, couch_work_queue:item_count(Q)),
- ?assertEqual(0, couch_work_queue:size(Q)),
-
- Item3 = produce(Producer, 10),
- ok = ping(Producer),
- ?assertEqual(0, couch_work_queue:item_count(Q)),
- ?assertEqual(0, couch_work_queue:size(Q)),
-
- R = [{ping(C), Item}
- || {C, Item} <- lists:zip(Consumers, [Item1, Item2, Item3])],
-
- ?_assertEqual([{ok, Item1}, {ok, Item2}, {ok, Item3}], R);
-should_consume_right_item({_, Producer, Consumer}) ->
- consume(Consumer, 1),
- Item = produce(Producer, 10),
- produce(Producer, 20),
- ok = ping(Producer),
- ok = ping(Consumer),
- {ok, Items} = last_consumer_items(Consumer),
- ?_assertEqual([Item], Items).
-
-should_increase_queue_size_on_produce({Q, Producer, _}) ->
- produce(Producer, 50),
- ok = ping(Producer),
- Count1 = couch_work_queue:item_count(Q),
- Size1 = couch_work_queue:size(Q),
-
- produce(Producer, 10),
- Count2 = couch_work_queue:item_count(Q),
- Size2 = couch_work_queue:size(Q),
-
- ?_assertEqual([{Count1, Size1}, {Count2, Size2}], [{1, 50}, {2, 60}]).
-
-should_block_producer_on_full_queue_count({Q, Producer, _}) ->
- produce(Producer, 10),
- ?assertEqual(1, couch_work_queue:item_count(Q)),
- ok = ping(Producer),
-
- produce(Producer, 15),
- ?assertEqual(2, couch_work_queue:item_count(Q)),
- ok = ping(Producer),
-
- produce(Producer, 20),
- ?assertEqual(3, couch_work_queue:item_count(Q)),
- Pong = ping(Producer),
-
- ?_assertEqual(timeout, Pong).
-
-should_block_producer_on_full_queue_size({Q, Producer, _}) ->
- produce(Producer, 100),
- ok = ping(Producer),
- ?assertEqual(1, couch_work_queue:item_count(Q)),
- ?assertEqual(100, couch_work_queue:size(Q)),
-
- produce(Producer, 110),
- Pong = ping(Producer),
- ?assertEqual(2, couch_work_queue:item_count(Q)),
- ?assertEqual(210, couch_work_queue:size(Q)),
-
- ?_assertEqual(timeout, Pong).
-
-should_consume_multiple_items({_, Producer, Consumer}) ->
- Item1 = produce(Producer, 10),
- ok = ping(Producer),
-
- Item2 = produce(Producer, 15),
- ok = ping(Producer),
-
- consume(Consumer, 2),
-
- {ok, Items} = last_consumer_items(Consumer),
- ?_assertEqual([Item1, Item2], Items).
-
-should_receive_first_queued_item({Q, Producer, Consumer}) ->
- consume(Consumer, 100),
- timeout = ping(Consumer),
-
- Item = produce(Producer, 11),
- ok = ping(Producer),
-
- ok = ping(Consumer),
- ?assertEqual(0, couch_work_queue:item_count(Q)),
-
- {ok, Items} = last_consumer_items(Consumer),
- ?_assertEqual([Item], Items).
-
-should_consume_all({_, Producer, Consumer}) ->
- Item1 = produce(Producer, 10),
- Item2 = produce(Producer, 15),
- Item3 = produce(Producer, 20),
-
- consume(Consumer, all),
-
- {ok, Items} = last_consumer_items(Consumer),
- ?_assertEqual([Item1, Item2, Item3], Items).
-
-should_timeout_on_close_non_empty_queue({Q, Producer, _}) ->
- produce(Producer, 1),
- Status = close_queue(Q),
-
- ?_assertEqual(timeout, Status).
-
-should_not_block_producer_for_non_empty_queue_after_close({Q, Producer, _}) ->
- produce(Producer, 1),
- close_queue(Q),
- Pong = ping(Producer),
- Size = couch_work_queue:size(Q),
- Count = couch_work_queue:item_count(Q),
-
- ?_assertEqual({ok, 1, 1}, {Pong, Size, Count}).
-
-should_be_closed({Q, _, Consumers}) when is_list(Consumers) ->
- ok = close_queue(Q),
-
- [consume(C, 1) || C <- Consumers],
-
- LastConsumerItems = [last_consumer_items(C) || C <- Consumers],
- ItemsCount = couch_work_queue:item_count(Q),
- Size = couch_work_queue:size(Q),
-
- ?_assertEqual({[closed, closed, closed], closed, closed},
- {LastConsumerItems, ItemsCount, Size});
-should_be_closed({Q, _, Consumer}) ->
- ok = close_queue(Q),
-
- consume(Consumer, 1),
-
- LastConsumerItems = last_consumer_items(Consumer),
- ItemsCount = couch_work_queue:item_count(Q),
- Size = couch_work_queue:size(Q),
-
- ?_assertEqual({closed, closed, closed},
- {LastConsumerItems, ItemsCount, Size}).
-
-
-close_queue(Q) ->
- ok = couch_work_queue:close(Q),
- MonRef = erlang:monitor(process, Q),
- receive
- {'DOWN', MonRef, process, Q, _Reason} -> ok
- after ?TIMEOUT ->
- erlang:demonitor(MonRef),
- timeout
- end.
-
-spawn_consumer(Q) ->
- Parent = self(),
- spawn(fun() -> consumer_loop(Parent, Q, nil) end).
-
-consumer_loop(Parent, Q, PrevItem) ->
- receive
- {stop, Ref} ->
- Parent ! {ok, Ref};
- {ping, Ref} ->
- Parent ! {pong, Ref},
- consumer_loop(Parent, Q, PrevItem);
- {last_item, Ref} ->
- Parent ! {item, Ref, PrevItem},
- consumer_loop(Parent, Q, PrevItem);
- {consume, N} ->
- Result = couch_work_queue:dequeue(Q, N),
- consumer_loop(Parent, Q, Result)
- end.
-
-spawn_producer(Q) ->
- Parent = self(),
- spawn(fun() -> producer_loop(Parent, Q) end).
-
-producer_loop(Parent, Q) ->
- receive
- {stop, Ref} ->
- Parent ! {ok, Ref};
- {ping, Ref} ->
- Parent ! {pong, Ref},
- producer_loop(Parent, Q);
- {produce, Ref, Size} ->
- Item = crypto:rand_bytes(Size),
- Parent ! {item, Ref, Item},
- ok = couch_work_queue:queue(Q, Item),
- producer_loop(Parent, Q)
- end.
-
-consume(Consumer, N) ->
- Consumer ! {consume, N}.
-
-last_consumer_items(Consumer) ->
- Ref = make_ref(),
- Consumer ! {last_item, Ref},
- receive
- {item, Ref, Items} ->
- Items
- after ?TIMEOUT ->
- timeout
- end.
-
-produce(Producer, Size) ->
- Ref = make_ref(),
- Producer ! {produce, Ref, Size},
- receive
- {item, Ref, Item} ->
- Item
- after ?TIMEOUT ->
- erlang:error({assertion_failed,
- [{module, ?MODULE},
- {line, ?LINE},
- {reason, "Timeout asking producer to produce an item"}]})
- end.
-
-ping(Pid) ->
- Ref = make_ref(),
- Pid ! {ping, Ref},
- receive
- {pong, Ref} ->
- ok
- after ?TIMEOUT ->
- timeout
- end.
-
-stop(Pid, Name) ->
- Ref = make_ref(),
- Pid ! {stop, Ref},
- receive
- {ok, Ref} -> ok
- after ?TIMEOUT ->
- ?debugMsg("Timeout stopping " ++ Name),
- timeout
- end.
http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/0ce84d8e/test/couchdb/couchdb_attachments_tests.erl
----------------------------------------------------------------------
diff --git a/test/couchdb/couchdb_attachments_tests.erl b/test/couchdb/couchdb_attachments_tests.erl
deleted file mode 100644
index cf59785..0000000
--- a/test/couchdb/couchdb_attachments_tests.erl
+++ /dev/null
@@ -1,638 +0,0 @@
-% 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(couchdb_attachments_tests).
-
--include("couch_eunit.hrl").
--include_lib("couchdb/couch_db.hrl").
-
--define(COMPRESSION_LEVEL, 8).
--define(ATT_BIN_NAME, <<"logo.png">>).
--define(ATT_TXT_NAME, <<"file.erl">>).
--define(FIXTURE_PNG, filename:join([?FIXTURESDIR, "logo.png"])).
--define(FIXTURE_TXT, ?FILE).
--define(TIMEOUT, 1000).
--define(TIMEOUT_EUNIT, 10).
--define(TIMEWAIT, 100).
--define(i2l(I), integer_to_list(I)).
-
-
-start() ->
- {ok, Pid} = couch_server_sup:start_link(?CONFIG_CHAIN),
- % ensure in default compression settings for attachments_compression_tests
- couch_config:set("attachments", "compression_level",
- ?i2l(?COMPRESSION_LEVEL), false),
- couch_config:set("attachments", "compressible_types", "text/*", false),
- Pid.
-
-stop(Pid) ->
- erlang:monitor(process, Pid),
- couch_server_sup:stop(),
- receive
- {'DOWN', _, _, Pid, _} ->
- ok
- after ?TIMEOUT ->
- throw({timeout, server_stop})
- end.
-
-setup() ->
- DbName = ?tempdb(),
- {ok, Db} = couch_db:create(DbName, []),
- ok = couch_db:close(Db),
- Addr = couch_config:get("httpd", "bind_address", any),
- Port = mochiweb_socket_server:get(couch_httpd, port),
- Host = Addr ++ ":" ++ ?i2l(Port),
- {Host, ?b2l(DbName)}.
-
-setup({binary, standalone}) ->
- {Host, DbName} = setup(),
- setup_att(fun create_standalone_png_att/2, Host, DbName, ?FIXTURE_PNG);
-setup({text, standalone}) ->
- {Host, DbName} = setup(),
- setup_att(fun create_standalone_text_att/2, Host, DbName, ?FIXTURE_TXT);
-setup({binary, inline}) ->
- {Host, DbName} = setup(),
- setup_att(fun create_inline_png_att/2, Host, DbName, ?FIXTURE_PNG);
-setup({text, inline}) ->
- {Host, DbName} = setup(),
- setup_att(fun create_inline_text_att/2, Host, DbName, ?FIXTURE_TXT);
-setup(compressed) ->
- {Host, DbName} = setup(),
- setup_att(fun create_already_compressed_att/2, Host, DbName, ?FIXTURE_TXT).
-setup_att(Fun, Host, DbName, File) ->
- HttpHost = "http://" ++ Host,
- AttUrl = Fun(HttpHost, DbName),
- {ok, Data} = file:read_file(File),
- DocUrl = string:join([HttpHost, DbName, "doc"], "/"),
- Helpers = {DbName, DocUrl, AttUrl},
- {Data, Helpers}.
-
-teardown(_, {_, {DbName, _, _}}) ->
- teardown(DbName).
-
-teardown({_, DbName}) ->
- teardown(DbName);
-teardown(DbName) ->
- ok = couch_server:delete(?l2b(DbName), []),
- ok.
-
-
-attachments_test_() ->
- {
- "Attachments tests",
- {
- setup,
- fun start/0, fun stop/1,
- [
- attachments_md5_tests(),
- attachments_compression_tests()
- ]
- }
- }.
-
-attachments_md5_tests() ->
- {
- "Attachments MD5 tests",
- {
- foreach,
- fun setup/0, fun teardown/1,
- [
- fun should_upload_attachment_without_md5/1,
- fun should_upload_attachment_by_chunks_without_md5/1,
- fun should_upload_attachment_with_valid_md5_header/1,
- fun should_upload_attachment_by_chunks_with_valid_md5_header/1,
- fun should_upload_attachment_by_chunks_with_valid_md5_trailer/1,
- fun should_reject_attachment_with_invalid_md5/1,
- fun should_reject_chunked_attachment_with_invalid_md5/1,
- fun should_reject_chunked_attachment_with_invalid_md5_trailer/1
- ]
- }
- }.
-
-attachments_compression_tests() ->
- Funs = [
- fun should_get_att_without_accept_gzip_encoding/2,
- fun should_get_att_with_accept_gzip_encoding/2,
- fun should_get_att_with_accept_deflate_encoding/2,
- fun should_return_406_response_on_unsupported_encoding/2,
- fun should_get_doc_with_att_data/2,
- fun should_get_doc_with_att_data_stub/2
- ],
- {
- "Attachments compression tests",
- [
- {
- "Created via Attachments API",
- created_attachments_compression_tests(standalone, Funs)
- },
- {
- "Created inline via Document API",
- created_attachments_compression_tests(inline, Funs)
- },
- {
- "Created already been compressed via Attachments API",
- {
- foreachx,
- fun setup/1, fun teardown/2,
- [{compressed, Fun} || Fun <- Funs]
- }
- },
- {
- foreach,
- fun setup/0, fun teardown/1,
- [
- fun should_not_create_compressed_att_with_deflate_encoding/1,
- fun should_not_create_compressed_att_with_compress_encoding/1,
- fun should_create_compressible_att_with_ctype_params/1
- ]
- }
- ]
- }.
-
-created_attachments_compression_tests(Mod, Funs) ->
- [
- {
- "Compressiable attachments",
- {
- foreachx,
- fun setup/1, fun teardown/2,
- [{{text, Mod}, Fun} || Fun <- Funs]
- }
- },
- {
- "Uncompressiable attachments",
- {
- foreachx,
- fun setup/1, fun teardown/2,
- [{{binary, Mod}, Fun} || Fun <- Funs]
- }
- }
- ].
-
-
-
-should_upload_attachment_without_md5({Host, DbName}) ->
- ?_test(begin
- AttUrl = string:join(["", DbName, ?docid(), "readme.txt"], "/"),
- Body = "We all live in a yellow submarine!",
- Headers = [
- {"Content-Length", "34"},
- {"Content-Type", "text/plain"},
- {"Host", Host}
- ],
- {ok, Code, Json} = request("PUT", AttUrl, Headers, Body),
- ?assertEqual(201, Code),
- ?assertEqual(true, get_json(Json, [<<"ok">>]))
- end).
-
-should_upload_attachment_by_chunks_without_md5({Host, DbName}) ->
- ?_test(begin
- AttUrl = string:join(["", DbName, ?docid(), "readme.txt"], "/"),
- AttData = <<"We all live in a yellow submarine!">>,
- <<Part1:21/binary, Part2:13/binary>> = AttData,
- Body = chunked_body([Part1, Part2]),
- Headers = [
- {"Content-Type", "text/plain"},
- {"Transfer-Encoding", "chunked"},
- {"Host", Host}
- ],
- {ok, Code, Json} = request("PUT", AttUrl, Headers, Body),
- ?assertEqual(201, Code),
- ?assertEqual(true, get_json(Json, [<<"ok">>]))
- end).
-
-should_upload_attachment_with_valid_md5_header({Host, DbName}) ->
- ?_test(begin
- AttUrl = string:join(["", DbName, ?docid(), "readme.txt"], "/"),
- Body = "We all live in a yellow submarine!",
- Headers = [
- {"Content-Length", "34"},
- {"Content-Type", "text/plain"},
- {"Content-MD5", ?b2l(base64:encode(couch_util:md5(Body)))},
- {"Host", Host}
- ],
- {ok, Code, Json} = request("PUT", AttUrl, Headers, Body),
- ?assertEqual(201, Code),
- ?assertEqual(true, get_json(Json, [<<"ok">>]))
- end).
-
-should_upload_attachment_by_chunks_with_valid_md5_header({Host, DbName}) ->
- ?_test(begin
- AttUrl = string:join(["", DbName, ?docid(), "readme.txt"], "/"),
- AttData = <<"We all live in a yellow submarine!">>,
- <<Part1:21/binary, Part2:13/binary>> = AttData,
- Body = chunked_body([Part1, Part2]),
- Headers = [
- {"Content-Type", "text/plain"},
- {"Content-MD5", ?b2l(base64:encode(couch_util:md5(AttData)))},
- {"Host", Host},
- {"Transfer-Encoding", "chunked"}
- ],
- {ok, Code, Json} = request("PUT", AttUrl, Headers, Body),
- ?assertEqual(201, Code),
- ?assertEqual(true, get_json(Json, [<<"ok">>]))
- end).
-
-should_upload_attachment_by_chunks_with_valid_md5_trailer({Host, DbName}) ->
- ?_test(begin
- AttUrl = string:join(["", DbName, ?docid(), "readme.txt"], "/"),
- AttData = <<"We all live in a yellow submarine!">>,
- <<Part1:21/binary, Part2:13/binary>> = AttData,
- Body = [chunked_body([Part1, Part2]),
- "Content-MD5: ", base64:encode(couch_util:md5(AttData)),
- "\r\n"],
- Headers = [
- {"Content-Type", "text/plain"},
- {"Host", Host},
- {"Trailer", "Content-MD5"},
- {"Transfer-Encoding", "chunked"}
- ],
- {ok, Code, Json} = request("PUT", AttUrl, Headers, Body),
- ?assertEqual(201, Code),
- ?assertEqual(true, get_json(Json, [<<"ok">>]))
- end).
-
-should_reject_attachment_with_invalid_md5({Host, DbName}) ->
- ?_test(begin
- AttUrl = string:join(["", DbName, ?docid(), "readme.txt"], "/"),
- Body = "We all live in a yellow submarine!",
- Headers = [
- {"Content-Length", "34"},
- {"Content-Type", "text/plain"},
- {"Content-MD5", ?b2l(base64:encode(<<"foobar!">>))},
- {"Host", Host}
- ],
- {ok, Code, Json} = request("PUT", AttUrl, Headers, Body),
- ?assertEqual(400, Code),
- ?assertEqual(<<"content_md5_mismatch">>,
- get_json(Json, [<<"error">>]))
- end).
-
-
-should_reject_chunked_attachment_with_invalid_md5({Host, DbName}) ->
- ?_test(begin
- AttUrl = string:join(["", DbName, ?docid(), "readme.txt"], "/"),
- AttData = <<"We all live in a yellow submarine!">>,
- <<Part1:21/binary, Part2:13/binary>> = AttData,
- Body = chunked_body([Part1, Part2]),
- Headers = [
- {"Content-Type", "text/plain"},
- {"Content-MD5", ?b2l(base64:encode(<<"foobar!">>))},
- {"Host", Host},
- {"Transfer-Encoding", "chunked"}
- ],
- {ok, Code, Json} = request("PUT", AttUrl, Headers, Body),
- ?assertEqual(400, Code),
- ?assertEqual(<<"content_md5_mismatch">>,
- get_json(Json, [<<"error">>]))
- end).
-
-should_reject_chunked_attachment_with_invalid_md5_trailer({Host, DbName}) ->
- ?_test(begin
- AttUrl = string:join(["", DbName, ?docid(), "readme.txt"], "/"),
- AttData = <<"We all live in a yellow submarine!">>,
- <<Part1:21/binary, Part2:13/binary>> = AttData,
- Body = [chunked_body([Part1, Part2]),
- "Content-MD5: ", base64:encode(<<"foobar!">>),
- "\r\n"],
- Headers = [
- {"Content-Type", "text/plain"},
- {"Host", Host},
- {"Trailer", "Content-MD5"},
- {"Transfer-Encoding", "chunked"}
- ],
- {ok, Code, Json} = request("PUT", AttUrl, Headers, Body),
- ?assertEqual(400, Code),
- ?assertEqual(<<"content_md5_mismatch">>, get_json(Json, [<<"error">>]))
- end).
-
-should_get_att_without_accept_gzip_encoding(_, {Data, {_, _, AttUrl}}) ->
- ?_test(begin
- {ok, Code, Headers, Body} = test_request:get(AttUrl),
- ?assertEqual(200, Code),
- ?assertNot(lists:member({"Content-Encoding", "gzip"}, Headers)),
- ?assertEqual(Data, iolist_to_binary(Body))
- end).
-
-should_get_att_with_accept_gzip_encoding(compressed, {Data, {_, _, AttUrl}}) ->
- ?_test(begin
- {ok, Code, Headers, Body} = test_request:get(
- AttUrl, [{"Accept-Encoding", "gzip"}]),
- ?assertEqual(200, Code),
- ?assert(lists:member({"Content-Encoding", "gzip"}, Headers)),
- ?assertEqual(Data, zlib:gunzip(iolist_to_binary(Body)))
- end);
-should_get_att_with_accept_gzip_encoding({text, _}, {Data, {_, _, AttUrl}}) ->
- ?_test(begin
- {ok, Code, Headers, Body} = test_request:get(
- AttUrl, [{"Accept-Encoding", "gzip"}]),
- ?assertEqual(200, Code),
- ?assert(lists:member({"Content-Encoding", "gzip"}, Headers)),
- ?assertEqual(Data, zlib:gunzip(iolist_to_binary(Body)))
- end);
-should_get_att_with_accept_gzip_encoding({binary, _}, {Data, {_, _, AttUrl}}) ->
- ?_test(begin
- {ok, Code, Headers, Body} = test_request:get(
- AttUrl, [{"Accept-Encoding", "gzip"}]),
- ?assertEqual(200, Code),
- ?assertEqual(undefined,
- couch_util:get_value("Content-Encoding", Headers)),
- ?assertEqual(Data, iolist_to_binary(Body))
- end).
-
-should_get_att_with_accept_deflate_encoding(_, {Data, {_, _, AttUrl}}) ->
- ?_test(begin
- {ok, Code, Headers, Body} = test_request:get(
- AttUrl, [{"Accept-Encoding", "deflate"}]),
- ?assertEqual(200, Code),
- ?assertEqual(undefined,
- couch_util:get_value("Content-Encoding", Headers)),
- ?assertEqual(Data, iolist_to_binary(Body))
- end).
-
-should_return_406_response_on_unsupported_encoding(_, {_, {_, _, AttUrl}}) ->
- ?_assertEqual(406,
- begin
- {ok, Code, _, _} = test_request:get(
- AttUrl, [{"Accept-Encoding", "deflate, *;q=0"}]),
- Code
- end).
-
-should_get_doc_with_att_data(compressed, {Data, {_, DocUrl, _}}) ->
- ?_test(begin
- Url = DocUrl ++ "?attachments=true",
- {ok, Code, _, Body} = test_request:get(
- Url, [{"Accept", "application/json"}]),
- ?assertEqual(200, Code),
- Json = ejson:decode(Body),
- AttJson = couch_util:get_nested_json_value(
- Json, [<<"_attachments">>, ?ATT_TXT_NAME]),
- AttData = couch_util:get_nested_json_value(
- AttJson, [<<"data">>]),
- ?assertEqual(
- <<"text/plain">>,
- couch_util:get_nested_json_value(AttJson,[<<"content_type">>])),
- ?assertEqual(Data, base64:decode(AttData))
- end);
-should_get_doc_with_att_data({text, _}, {Data, {_, DocUrl, _}}) ->
- ?_test(begin
- Url = DocUrl ++ "?attachments=true",
- {ok, Code, _, Body} = test_request:get(
- Url, [{"Accept", "application/json"}]),
- ?assertEqual(200, Code),
- Json = ejson:decode(Body),
- AttJson = couch_util:get_nested_json_value(
- Json, [<<"_attachments">>, ?ATT_TXT_NAME]),
- AttData = couch_util:get_nested_json_value(
- AttJson, [<<"data">>]),
- ?assertEqual(
- <<"text/plain">>,
- couch_util:get_nested_json_value(AttJson,[<<"content_type">>])),
- ?assertEqual(Data, base64:decode(AttData))
- end);
-should_get_doc_with_att_data({binary, _}, {Data, {_, DocUrl, _}}) ->
- ?_test(begin
- Url = DocUrl ++ "?attachments=true",
- {ok, Code, _, Body} = test_request:get(
- Url, [{"Accept", "application/json"}]),
- ?assertEqual(200, Code),
- Json = ejson:decode(Body),
- AttJson = couch_util:get_nested_json_value(
- Json, [<<"_attachments">>, ?ATT_BIN_NAME]),
- AttData = couch_util:get_nested_json_value(
- AttJson, [<<"data">>]),
- ?assertEqual(
- <<"image/png">>,
- couch_util:get_nested_json_value(AttJson,[<<"content_type">>])),
- ?assertEqual(Data, base64:decode(AttData))
- end).
-
-should_get_doc_with_att_data_stub(compressed, {Data, {_, DocUrl, _}}) ->
- ?_test(begin
- Url = DocUrl ++ "?att_encoding_info=true",
- {ok, Code, _, Body} = test_request:get(
- Url, [{"Accept", "application/json"}]),
- ?assertEqual(200, Code),
- Json = ejson:decode(Body),
- {AttJson} = couch_util:get_nested_json_value(
- Json, [<<"_attachments">>, ?ATT_TXT_NAME]),
- ?assertEqual(<<"gzip">>,
- couch_util:get_value(<<"encoding">>, AttJson)),
- AttLength = couch_util:get_value(<<"length">>, AttJson),
- EncLength = couch_util:get_value(<<"encoded_length">>, AttJson),
- ?assertEqual(AttLength, EncLength),
- ?assertEqual(iolist_size(zlib:gzip(Data)), AttLength)
- end);
-should_get_doc_with_att_data_stub({text, _}, {Data, {_, DocUrl, _}}) ->
- ?_test(begin
- Url = DocUrl ++ "?att_encoding_info=true",
- {ok, Code, _, Body} = test_request:get(
- Url, [{"Accept", "application/json"}]),
- ?assertEqual(200, Code),
- Json = ejson:decode(Body),
- {AttJson} = couch_util:get_nested_json_value(
- Json, [<<"_attachments">>, ?ATT_TXT_NAME]),
- ?assertEqual(<<"gzip">>,
- couch_util:get_value(<<"encoding">>, AttJson)),
- AttEncLength = iolist_size(gzip(Data)),
- ?assertEqual(AttEncLength,
- couch_util:get_value(<<"encoded_length">>, AttJson)),
- ?assertEqual(byte_size(Data),
- couch_util:get_value(<<"length">>, AttJson))
- end);
-should_get_doc_with_att_data_stub({binary, _}, {Data, {_, DocUrl, _}}) ->
- ?_test(begin
- Url = DocUrl ++ "?att_encoding_info=true",
- {ok, Code, _, Body} = test_request:get(
- Url, [{"Accept", "application/json"}]),
- ?assertEqual(200, Code),
- Json = ejson:decode(Body),
- {AttJson} = couch_util:get_nested_json_value(
- Json, [<<"_attachments">>, ?ATT_BIN_NAME]),
- ?assertEqual(undefined,
- couch_util:get_value(<<"encoding">>, AttJson)),
- ?assertEqual(undefined,
- couch_util:get_value(<<"encoded_length">>, AttJson)),
- ?assertEqual(byte_size(Data),
- couch_util:get_value(<<"length">>, AttJson))
- end).
-
-should_not_create_compressed_att_with_deflate_encoding({Host, DbName}) ->
- ?_assertEqual(415,
- begin
- HttpHost = "http://" ++ Host,
- AttUrl = string:join([HttpHost, DbName, ?docid(), "file.txt"], "/"),
- {ok, Data} = file:read_file(?FIXTURE_TXT),
- Body = zlib:compress(Data),
- Headers = [
- {"Content-Encoding", "deflate"},
- {"Content-Type", "text/plain"}
- ],
- {ok, Code, _, _} = test_request:put(AttUrl, Headers, Body),
- Code
- end).
-
-should_not_create_compressed_att_with_compress_encoding({Host, DbName}) ->
- % Note: As of OTP R13B04, it seems there's no LZW compression
- % (i.e. UNIX compress utility implementation) lib in OTP.
- % However there's a simple working Erlang implementation at:
- % http://scienceblogs.com/goodmath/2008/01/simple_lempelziv_compression_i.php
- ?_assertEqual(415,
- begin
- HttpHost = "http://" ++ Host,
- AttUrl = string:join([HttpHost, DbName, ?docid(), "file.txt"], "/"),
- {ok, Data} = file:read_file(?FIXTURE_TXT),
- Headers = [
- {"Content-Encoding", "compress"},
- {"Content-Type", "text/plain"}
- ],
- {ok, Code, _, _} = test_request:put(AttUrl, Headers, Data),
- Code
- end).
-
-should_create_compressible_att_with_ctype_params({Host, DbName}) ->
- {timeout, ?TIMEOUT_EUNIT, ?_test(begin
- HttpHost = "http://" ++ Host,
- DocUrl = string:join([HttpHost, DbName, ?docid()], "/"),
- AttUrl = string:join([DocUrl, ?b2l(?ATT_TXT_NAME)], "/"),
- {ok, Data} = file:read_file(?FIXTURE_TXT),
- Headers = [{"Content-Type", "text/plain; charset=UTF-8"}],
- {ok, Code0, _, _} = test_request:put(AttUrl, Headers, Data),
- ?assertEqual(201, Code0),
-
- {ok, Code1, _, Body} = test_request:get(
- DocUrl ++ "?att_encoding_info=true"),
- ?assertEqual(200, Code1),
- Json = ejson:decode(Body),
- {AttJson} = couch_util:get_nested_json_value(
- Json, [<<"_attachments">>, ?ATT_TXT_NAME]),
- ?assertEqual(<<"gzip">>,
- couch_util:get_value(<<"encoding">>, AttJson)),
- AttEncLength = iolist_size(gzip(Data)),
- ?assertEqual(AttEncLength,
- couch_util:get_value(<<"encoded_length">>, AttJson)),
- ?assertEqual(byte_size(Data),
- couch_util:get_value(<<"length">>, AttJson))
- end)}.
-
-
-get_json(Json, Path) ->
- couch_util:get_nested_json_value(Json, Path).
-
-to_hex(Val) ->
- to_hex(Val, []).
-
-to_hex(0, Acc) ->
- Acc;
-to_hex(Val, Acc) ->
- to_hex(Val div 16, [hex_char(Val rem 16) | Acc]).
-
-hex_char(V) when V < 10 -> $0 + V;
-hex_char(V) -> $A + V - 10.
-
-chunked_body(Chunks) ->
- chunked_body(Chunks, []).
-
-chunked_body([], Acc) ->
- iolist_to_binary(lists:reverse(Acc, "0\r\n"));
-chunked_body([Chunk | Rest], Acc) ->
- Size = to_hex(size(Chunk)),
- chunked_body(Rest, ["\r\n", Chunk, "\r\n", Size | Acc]).
-
-get_socket() ->
- Options = [binary, {packet, 0}, {active, false}],
- Addr = couch_config:get("httpd", "bind_address", any),
- Port = mochiweb_socket_server:get(couch_httpd, port),
- {ok, Sock} = gen_tcp:connect(Addr, Port, Options),
- Sock.
-
-request(Method, Url, Headers, Body) ->
- RequestHead = [Method, " ", Url, " HTTP/1.1"],
- RequestHeaders = [[string:join([Key, Value], ": "), "\r\n"]
- || {Key, Value} <- Headers],
- Request = [RequestHead, "\r\n", RequestHeaders, "\r\n", Body, "\r\n"],
- Sock = get_socket(),
- gen_tcp:send(Sock, list_to_binary(lists:flatten(Request))),
- timer:sleep(?TIMEWAIT), % must wait to receive complete response
- {ok, R} = gen_tcp:recv(Sock, 0),
- gen_tcp:close(Sock),
- [Header, Body1] = re:split(R, "\r\n\r\n", [{return, binary}]),
- {ok, {http_response, _, Code, _}, _} =
- erlang:decode_packet(http, Header, []),
- Json = ejson:decode(Body1),
- {ok, Code, Json}.
-
-create_standalone_text_att(Host, DbName) ->
- {ok, Data} = file:read_file(?FIXTURE_TXT),
- Url = string:join([Host, DbName, "doc", ?b2l(?ATT_TXT_NAME)], "/"),
- {ok, Code, _Headers, _Body} = test_request:put(
- Url, [{"Content-Type", "text/plain"}], Data),
- ?assertEqual(201, Code),
- Url.
-
-create_standalone_png_att(Host, DbName) ->
- {ok, Data} = file:read_file(?FIXTURE_PNG),
- Url = string:join([Host, DbName, "doc", ?b2l(?ATT_BIN_NAME)], "/"),
- {ok, Code, _Headers, _Body} = test_request:put(
- Url, [{"Content-Type", "image/png"}], Data),
- ?assertEqual(201, Code),
- Url.
-
-create_inline_text_att(Host, DbName) ->
- {ok, Data} = file:read_file(?FIXTURE_TXT),
- Url = string:join([Host, DbName, "doc"], "/"),
- Doc = {[
- {<<"_attachments">>, {[
- {?ATT_TXT_NAME, {[
- {<<"content_type">>, <<"text/plain">>},
- {<<"data">>, base64:encode(Data)}
- ]}
- }]}}
- ]},
- {ok, Code, _Headers, _Body} = test_request:put(
- Url, [{"Content-Type", "application/json"}], ejson:encode(Doc)),
- ?assertEqual(201, Code),
- string:join([Url, ?b2l(?ATT_TXT_NAME)], "/").
-
-create_inline_png_att(Host, DbName) ->
- {ok, Data} = file:read_file(?FIXTURE_PNG),
- Url = string:join([Host, DbName, "doc"], "/"),
- Doc = {[
- {<<"_attachments">>, {[
- {?ATT_BIN_NAME, {[
- {<<"content_type">>, <<"image/png">>},
- {<<"data">>, base64:encode(Data)}
- ]}
- }]}}
- ]},
- {ok, Code, _Headers, _Body} = test_request:put(
- Url, [{"Content-Type", "application/json"}], ejson:encode(Doc)),
- ?assertEqual(201, Code),
- string:join([Url, ?b2l(?ATT_BIN_NAME)], "/").
-
-create_already_compressed_att(Host, DbName) ->
- {ok, Data} = file:read_file(?FIXTURE_TXT),
- Url = string:join([Host, DbName, "doc", ?b2l(?ATT_TXT_NAME)], "/"),
- {ok, Code, _Headers, _Body} = test_request:put(
- Url, [{"Content-Type", "text/plain"}, {"Content-Encoding", "gzip"}],
- zlib:gzip(Data)),
- ?assertEqual(201, Code),
- Url.
-
-gzip(Data) ->
- Z = zlib:open(),
- ok = zlib:deflateInit(Z, ?COMPRESSION_LEVEL, deflated, 16 + 15, 8, default),
- zlib:deflate(Z, Data),
- Last = zlib:deflate(Z, [], finish),
- ok = zlib:deflateEnd(Z),
- ok = zlib:close(Z),
- Last.
http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/0ce84d8e/test/couchdb/couchdb_compaction_daemon.erl
----------------------------------------------------------------------
diff --git a/test/couchdb/couchdb_compaction_daemon.erl b/test/couchdb/couchdb_compaction_daemon.erl
deleted file mode 100644
index 725a97b..0000000
--- a/test/couchdb/couchdb_compaction_daemon.erl
+++ /dev/null
@@ -1,231 +0,0 @@
-% 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(couchdb_compaction_daemon).
-
--include("couch_eunit.hrl").
--include_lib("couchdb/couch_db.hrl").
-
--define(ADMIN_USER, {user_ctx, #user_ctx{roles=[<<"_admin">>]}}).
--define(DELAY, 100).
--define(TIMEOUT, 30000).
--define(TIMEOUT_S, ?TIMEOUT div 1000).
-
-
-start() ->
- {ok, Pid} = couch_server_sup:start_link(?CONFIG_CHAIN),
- couch_config:set("compaction_daemon", "check_interval", "3", false),
- couch_config:set("compaction_daemon", "min_file_size", "100000", false),
- Pid.
-
-stop(Pid) ->
- erlang:monitor(process, Pid),
- couch_server_sup:stop(),
- receive
- {'DOWN', _, _, Pid, _} ->
- ok
- after ?TIMEOUT ->
- throw({timeout, server_stop})
- end.
-
-setup() ->
- DbName = ?tempdb(),
- {ok, Db} = couch_db:create(DbName, [?ADMIN_USER]),
- create_design_doc(Db),
- ok = couch_db:close(Db),
- DbName.
-
-teardown(DbName) ->
- Configs = couch_config:get("compactions"),
- lists:foreach(
- fun({Key, _}) ->
- ok = couch_config:delete("compactions", Key, false)
- end,
- Configs),
- couch_server:delete(DbName, [?ADMIN_USER]),
- ok.
-
-
-compaction_daemon_test_() ->
- {
- "Compaction daemon tests",
- {
- setup,
- fun start/0, fun stop/1,
- {
- foreach,
- fun setup/0, fun teardown/1,
- [
- fun should_compact_by_default_rule/1,
- fun should_compact_by_dbname_rule/1
- ]
- }
- }
- }.
-
-
-should_compact_by_default_rule(DbName) ->
- {timeout, ?TIMEOUT_S, ?_test(begin
- {ok, Db} = couch_db:open_int(DbName, []),
- populate(DbName, 70, 70, 200 * 1024),
-
- {_, DbFileSize} = get_db_frag(DbName),
- {_, ViewFileSize} = get_view_frag(DbName),
-
- ok = couch_config:set("compactions", "_default",
- "[{db_fragmentation, \"70%\"}, {view_fragmentation, \"70%\"}]",
- false),
-
- ok = timer:sleep(4000), % something >= check_interval
- wait_compaction_finished(DbName),
- ok = couch_config:delete("compactions", "_default", false),
-
- {DbFrag2, DbFileSize2} = get_db_frag(DbName),
- {ViewFrag2, ViewFileSize2} = get_view_frag(DbName),
-
- ?assert(DbFrag2 < 70),
- ?assert(ViewFrag2 < 70),
-
- ?assert(DbFileSize > DbFileSize2),
- ?assert(ViewFileSize > ViewFileSize2),
-
- ?assert(couch_db:is_idle(Db)),
- ok = couch_db:close(Db)
- end)}.
-
-should_compact_by_dbname_rule(DbName) ->
- {timeout, ?TIMEOUT_S, ?_test(begin
- {ok, Db} = couch_db:open_int(DbName, []),
- populate(DbName, 70, 70, 200 * 1024),
-
- {_, DbFileSize} = get_db_frag(DbName),
- {_, ViewFileSize} = get_view_frag(DbName),
-
- ok = couch_config:set("compactions", ?b2l(DbName),
- "[{db_fragmentation, \"70%\"}, {view_fragmentation, \"70%\"}]",
- false),
-
- ok = timer:sleep(4000), % something >= check_interval
- wait_compaction_finished(DbName),
- ok = couch_config:delete("compactions", ?b2l(DbName), false),
-
- {DbFrag2, DbFileSize2} = get_db_frag(DbName),
- {ViewFrag2, ViewFileSize2} = get_view_frag(DbName),
-
- ?assert(DbFrag2 < 70),
- ?assert(ViewFrag2 < 70),
-
- ?assert(DbFileSize > DbFileSize2),
- ?assert(ViewFileSize > ViewFileSize2),
-
- ?assert(couch_db:is_idle(Db)),
- ok = couch_db:close(Db)
- end)}.
-
-
-create_design_doc(Db) ->
- DDoc = couch_doc:from_json_obj({[
- {<<"_id">>, <<"_design/foo">>},
- {<<"language">>, <<"javascript">>},
- {<<"views">>, {[
- {<<"foo">>, {[
- {<<"map">>, <<"function(doc) { emit(doc._id, doc); }">>}
- ]}},
- {<<"foo2">>, {[
- {<<"map">>, <<"function(doc) { emit(doc._id, doc); }">>}
- ]}},
- {<<"foo3">>, {[
- {<<"map">>, <<"function(doc) { emit(doc._id, doc); }">>}
- ]}}
- ]}}
- ]}),
- {ok, _} = couch_db:update_docs(Db, [DDoc]),
- {ok, _} = couch_db:ensure_full_commit(Db),
- ok.
-
-populate(DbName, DbFrag, ViewFrag, MinFileSize) ->
- {CurDbFrag, DbFileSize} = get_db_frag(DbName),
- {CurViewFrag, ViewFileSize} = get_view_frag(DbName),
- populate(DbName, DbFrag, ViewFrag, MinFileSize, CurDbFrag, CurViewFrag,
- lists:min([DbFileSize, ViewFileSize])).
-
-populate(_Db, DbFrag, ViewFrag, MinFileSize, CurDbFrag, CurViewFrag, FileSize)
- when CurDbFrag >= DbFrag, CurViewFrag >= ViewFrag, FileSize >= MinFileSize ->
- ok;
-populate(DbName, DbFrag, ViewFrag, MinFileSize, _, _, _) ->
- update(DbName),
- {CurDbFrag, DbFileSize} = get_db_frag(DbName),
- {CurViewFrag, ViewFileSize} = get_view_frag(DbName),
- populate(DbName, DbFrag, ViewFrag, MinFileSize, CurDbFrag, CurViewFrag,
- lists:min([DbFileSize, ViewFileSize])).
-
-update(DbName) ->
- {ok, Db} = couch_db:open_int(DbName, []),
- lists:foreach(fun(_) ->
- Doc = couch_doc:from_json_obj({[{<<"_id">>, couch_uuids:new()}]}),
- {ok, _} = couch_db:update_docs(Db, [Doc]),
- query_view(Db#db.name)
- end, lists:seq(1, 200)),
- couch_db:close(Db).
-
-db_url(DbName) ->
- Addr = couch_config:get("httpd", "bind_address", "127.0.0.1"),
- Port = integer_to_list(mochiweb_socket_server:get(couch_httpd, port)),
- "http://" ++ Addr ++ ":" ++ Port ++ "/" ++ ?b2l(DbName).
-
-query_view(DbName) ->
- {ok, Code, _Headers, _Body} = test_request:get(
- db_url(DbName) ++ "/_design/foo/_view/foo"),
- ?assertEqual(200, Code).
-
-get_db_frag(DbName) ->
- {ok, Db} = couch_db:open_int(DbName, []),
- {ok, Info} = couch_db:get_db_info(Db),
- couch_db:close(Db),
- FileSize = couch_util:get_value(disk_size, Info),
- DataSize = couch_util:get_value(data_size, Info),
- {round((FileSize - DataSize) / FileSize * 100), FileSize}.
-
-get_view_frag(DbName) ->
- {ok, Db} = couch_db:open_int(DbName, []),
- {ok, Info} = couch_mrview:get_info(Db, <<"_design/foo">>),
- couch_db:close(Db),
- FileSize = couch_util:get_value(disk_size, Info),
- DataSize = couch_util:get_value(data_size, Info),
- {round((FileSize - DataSize) / FileSize * 100), FileSize}.
-
-wait_compaction_finished(DbName) ->
- Parent = self(),
- Loop = spawn_link(fun() -> wait_loop(DbName, Parent) end),
- receive
- {done, Loop} ->
- ok
- after ?TIMEOUT ->
- erlang:error(
- {assertion_failed,
- [{module, ?MODULE}, {line, ?LINE},
- {reason, "Compaction timeout"}]})
- end.
-
-wait_loop(DbName, Parent) ->
- {ok, Db} = couch_db:open_int(DbName, []),
- {ok, DbInfo} = couch_db:get_db_info(Db),
- {ok, ViewInfo} = couch_mrview:get_info(Db, <<"_design/foo">>),
- couch_db:close(Db),
- case (couch_util:get_value(compact_running, ViewInfo) =:= true) orelse
- (couch_util:get_value(compact_running, DbInfo) =:= true) of
- false ->
- Parent ! {done, self()};
- true ->
- ok = timer:sleep(?DELAY),
- wait_loop(DbName, Parent)
- end.