make riak_kv_wterl_backend start the wterl app

This commit is contained in:
Steve Vinoski 2012-03-30 21:01:24 -04:00
parent ea3e73c0d7
commit 29acf9df7a
2 changed files with 43 additions and 47 deletions

View file

@ -87,23 +87,37 @@ start(Partition, Config) ->
lager:error("Failed to create wterl dir: data_root is not set"), lager:error("Failed to create wterl dir: data_root is not set"),
{error, data_root_unset}; {error, data_root_unset};
DataRoot -> DataRoot ->
ok = filelib:ensure_dir(filename:join(DataRoot, "x")), AppStart = case application:start(wterl) of
case wterl_conn:open(DataRoot) of ok ->
{ok, ConnRef} -> ok;
Table = "table:wt" ++ integer_to_list(Partition), {error, {already_started, _}} ->
{ok, SRef} = wterl:session_open(ConnRef), ok;
%% TODO: should check return value here, but we {error, Reason} ->
%% currently get an error when the table already lager:error("Failed to start wterl: ~p", [Reason]),
%% exists, so for now we ignore it. {error, Reason}
wterl:session_create(SRef, Table), end,
{ok, #state{conn=ConnRef, case AppStart of
table=Table, ok ->
session=SRef, ok = filelib:ensure_dir(filename:join(DataRoot, "x")),
partition=Partition}}; case wterl_conn:open(DataRoot) of
{error, Reason} -> {ok, ConnRef} ->
lager:error("Failed to start wterl backend: ~p\n", Table = "table:wt" ++ integer_to_list(Partition),
[Reason]), {ok, SRef} = wterl:session_open(ConnRef),
{error, Reason} %% TODO: should check return value here, but we
%% currently get an error when the table already
%% exists, so for now we ignore it.
wterl:session_create(SRef, Table),
{ok, #state{conn=ConnRef,
table=Table,
session=SRef,
partition=Partition}};
{error, ConnReason}=ConnError ->
lager:error("Failed to start wterl backend: ~p\n",
[ConnReason]),
ConnError
end;
Error ->
Error
end end
end. end.
@ -425,35 +439,13 @@ fetch_status(Cursor, {ok, Stat}, Acc) ->
-ifdef(TEST). -ifdef(TEST).
simple_test_() -> simple_test_() ->
{spawn, [{setup, SF, CF, TF}]} = riak_kv_backend:standard_test(?MODULE, []), ?assertCmd("rm -rf test/wterl-backend"),
{setup, application:set_env(wterl, data_root, "test/wterl-backend"),
fun() -> riak_kv_backend:standard_test(?MODULE, []).
?assertCmd("rm -rf test/wterl-backend"),
application:set_env(wterl, data_root, "test/wterl-backend"),
application:start(wterl),
SF()
end,
fun(X) ->
CF(X),
application:stop(wterl)
end,
fun(X) -> TF(X) end}.
custom_config_test_() -> custom_config_test_() ->
{spawn, [{setup, SF, CF, TF}]} = riak_kv_backend:standard_test( ?assertCmd("rm -rf test/wterl-backend"),
?MODULE, application:set_env(wterl, data_root, ""),
[{data_root, "test/wterl-backend"}]), riak_kv_backend:standard_test(?MODULE, [{data_root, "test/wterl-backend"}]).
{setup,
fun() ->
?assertCmd("rm -rf test/wterl-backend"),
application:set_env(wterl, data_root, ""),
application:start(wterl),
SF()
end,
fun(X) ->
CF(X),
application:stop(wterl)
end,
fun(X) -> TF(X) end}.
-endif. -endif.

View file

@ -170,8 +170,12 @@ simple_test_() ->
fun() -> fun() ->
?assertCmd("rm -rf " ++ ?DATADIR), ?assertCmd("rm -rf " ++ ?DATADIR),
?assertMatch(ok, filelib:ensure_dir(filename:join(?DATADIR, "x"))), ?assertMatch(ok, filelib:ensure_dir(filename:join(?DATADIR, "x"))),
{ok, Pid} = start_link(), case start_link() of
Pid {ok, Pid} ->
Pid;
{error, {already_started, Pid}} ->
Pid
end
end, end,
fun(_) -> fun(_) ->
stop() stop()