couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kxe...@apache.org
Subject [14/48] couch commit: updated refs/heads/master to 7776921
Date Sat, 04 Apr 2015 23:29:09 GMT
Add test_util:start/stop for minimal setup

The idea is to use test_util:start to setup minimal environment which
starts or mocks necessary dependencies. test_util:start returns
`#test_context{}` to be passed to test_util:stop/1.

COUCHDB-2547


Project: http://git-wip-us.apache.org/repos/asf/couchdb-couch/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-couch/commit/9e76a393
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-couch/tree/9e76a393
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-couch/diff/9e76a393

Branch: refs/heads/master
Commit: 9e76a39369fcd1405d7f4b3b0da025a08cb5b2e0
Parents: 74f539a
Author: ILYA Khlopotov <iilyak@ca.ibm.com>
Authored: Tue Jan 27 13:38:40 2015 -0800
Committer: ILYA Khlopotov <iilyak@ca.ibm.com>
Committed: Tue Feb 10 10:46:59 2015 -0800

----------------------------------------------------------------------
 src/test_util.erl | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/9e76a393/src/test_util.erl
----------------------------------------------------------------------
diff --git a/src/test_util.erl b/src/test_util.erl
index 0b96095..a4ab007 100644
--- a/src/test_util.erl
+++ b/src/test_util.erl
@@ -29,6 +29,10 @@
 -export([with_process_restart/1, with_process_restart/2, with_process_restart/3]).
 -export([wait_process/1, wait_process/2]).
 
+-export([start/1, start/2, start/3, stop/1]).
+
+-record(test_context, {mocked = [], started = [], module}).
+
 srcdir() ->
     code:priv_dir(couch) ++ "/../../".
 
@@ -189,6 +193,39 @@ wait_process(Name, Timeout, Started, _Prev) ->
         Pid
     end.
 
+start(Module) ->
+    start(Module, [], []).
+
+start(Module, ExtraApps) ->
+    start(Module, ExtraApps, []).
+
+start(Module, ExtraApps, Options) ->
+    Apps = start_applications([config, ioq|ExtraApps]),
+    ToMock = [config, couch_stats] -- proplists:get_value(dont_mock, Options, []),
+    mock(ToMock),
+    #test_context{module = Module, mocked = ToMock, started = Apps}.
+
+stop(#test_context{mocked = Mocked, started = Apps}) ->
+    meck:unload(Mocked),
+    stop_applications(Apps).
+
 now_us() ->
     {MegaSecs, Secs, MicroSecs} = now(),
     (MegaSecs * 1000000 + Secs) * 1000000 + MicroSecs.
+
+mock(Modules) when is_list(Modules) ->
+    [mock(Module) || Module <- Modules];
+mock(config) ->
+    meck:new(config, [passthrough]),
+    meck:expect(config, get, fun(_, _) -> undefined end),
+    meck:expect(config, get, fun(_, _, Default) -> Default end),
+    ok;
+mock(couch_stats) ->
+    meck:new(couch_stats, [passthrough]),
+    meck:expect(couch_stats, increment_counter, fun(_) -> ok end),
+    meck:expect(couch_stats, increment_counter, fun(_, _) -> ok end),
+    meck:expect(couch_stats, decrement_counter, fun(_) -> ok end),
+    meck:expect(couch_stats, decrement_counter, fun(_, _) -> ok end),
+    meck:expect(couch_stats, update_histogram, fun(_, _) -> ok end),
+    meck:expect(couch_stats, update_gauge, fun(_, _) -> ok end),
+    ok.


Mime
View raw message