Skip to content

Commit f6a5c3a

Browse files
committed
feat: allow parallel execution of eunit tests
This is achived by configuring each couch_server launched with separate etc, data and log directories. These directories are cleaned up after successful test runs.
1 parent d223392 commit f6a5c3a

1 file changed

Lines changed: 43 additions & 5 deletions

File tree

src/couch/src/test_util.erl

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
-include("couch_db_int.hrl").
4545
-include("couch_bt_engine.hrl").
4646

47-
-record(test_context, {mocked = [], started = [], module}).
47+
-record(test_context, {mocked = [], started = [], module, dir}).
4848

4949
-define(DEFAULT_APPS, [inets, ibrowse, ssl, config, couch_epi, couch_event, couch]).
5050

@@ -81,21 +81,58 @@ start_couch() ->
8181
start_couch(ExtraApps) ->
8282
start_couch(?CONFIG_CHAIN, ExtraApps).
8383

84+
% This function starts CouchDB with optional extra apps in a dedicated
85+
% directory under tmp/couchdb-tests/<uuid>/ — That is, all instances
86+
% run isolated so that if more than one runs at a time, it does not
87+
% interfere with any of the others.
88+
% The sub-directories here are etc/ log/ and data/ for configuration
89+
% logs and databases and view indexes respectively.
90+
% The function copies the initial bespoke test-ini files to the new
91+
% random directory and configures CouchDB to use those.
92+
% The two pieces of state created here are the random dir in the file system and
93+
% the process env var {config, ini_files} that is read by couch_config on
94+
% startup. These need to be reset at the right points in time.
95+
% the random dir is deleted and the env var reset when CouchDB is stopped.
96+
% Note: there is currently a case where stop_couch/0 could be called and
97+
% the file system cleanup can’t be run because we don’t get passed the test
98+
% context with the random directory value. At the moment stop_couch/0 is not
99+
% used anywhere, tho. Maybe we should remove it?
84100
start_couch(IniFiles, ExtraApps) ->
101+
RandomDir = filename:join([builddir(), "tmp", "couchdb-tests", couch_uuids:random()]),
102+
RandomEtcDir = filename:join([RandomDir, "etc"]),
103+
RandomDataDir = ?b2l(filename:join([RandomDir, "data"])),
104+
RandomLogFile = ?b2l(filename:join([RandomDir, "log", "couch.log"])),
105+
106+
ok = filelib:ensure_path(RandomDir),
107+
ok = filelib:ensure_path(RandomEtcDir),
108+
ok = filelib:ensure_path(RandomDataDir),
109+
ok = filelib:ensure_dir(RandomLogFile),
110+
111+
RandomIniFiles = lists:map(fun(SourceFile) ->
112+
TargetFileName = lists:last(filename:split(SourceFile)),
113+
TargetFile = filename:join([RandomEtcDir, TargetFileName]),
114+
{ok, _} = file:copy(SourceFile, TargetFile),
115+
?b2l(TargetFile)
116+
end, IniFiles),
85117
load_applications_with_stats(),
86-
ok = application:set_env(config, ini_files, IniFiles),
118+
ok = application:set_env(config, ini_files, RandomIniFiles),
87119
Apps = start_applications(?DEFAULT_APPS ++ ExtraApps),
120+
121+
ok = config:set("couchdb", "database_dir", RandomDataDir, false),
122+
ok = config:set("couchdb", "view_index_dir", RandomDataDir, false),
123+
ok = config:set("log", "file", RandomLogFile, false),
88124
ok = config:delete("compactions", "_default", false),
89-
#test_context{started = Apps}.
125+
#test_context{started = Apps, dir = RandomDir}.
90126

91127
stop_couch() ->
92128
ok = stop_applications(?DEFAULT_APPS).
93129

94-
stop_couch(#test_context{started = Apps}) ->
130+
stop_couch(#test_context{started = Apps, dir = RandomDir }) ->
131+
file:del_dir_r(RandomDir),
95132
stop_applications(Apps);
133+
96134
stop_couch(_) ->
97135
stop_couch().
98-
99136
with_couch_server_restart(Fun) ->
100137
Servers = couch_server:names(),
101138
test_util:with_processes_restart(Servers, Fun).
@@ -125,6 +162,7 @@ start_applications([App | Apps], Acc) ->
125162

126163
stop_applications(Apps) ->
127164
[application:stop(App) || App <- lists:reverse(Apps)],
165+
ok = application:unset_env(config, ini_files),
128166
ok.
129167

130168
start_config(Chain) ->

0 commit comments

Comments
 (0)