Skip to content

Commit 2aab342

Browse files
committed
Test env var by stopping the pool
Previously taking env into account was tested via starting new process. Now we simulate real start of the app, byt stopping the whole pool of workers and applying the new ENV to them.
1 parent 858cbb5 commit 2aab342

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

test/guanco_worker_tests.erl

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,50 @@
44

55
%% Test initialization with environment variable
66
init_with_env_test() ->
7+
%% Ensure a clean state
8+
application:stop(guanco),
9+
application:unset_env(guanco, ollama_api_url),
10+
711
{ok, _} = application:ensure_all_started(guanco),
8-
12+
13+
%% Stop and delete the pool
14+
supervisor:terminate_child(guanco_sup, guanco_worker_pool),
15+
supervisor:delete_child(guanco_sup, guanco_worker_pool),
16+
917
%% Set the environment variable
1018
application:set_env(guanco, ollama_api_url, "http://env-url"),
1119
io:format("Set environment variable: ~p~n", [application:get_env(guanco, ollama_api_url)]),
12-
13-
%% Create a new worker with the updated environment variable
14-
{ok, Pid} = guanco_worker:start_link([]),
15-
16-
Result = gen_server:call(Pid, {get_state}),
20+
21+
%% Start new workers
22+
PoolSize = application:get_env(guanco, pool_size, 5),
23+
MaxOverflow = application:get_env(guanco, max_overflow, 10),
24+
PoolboyConfig = [
25+
{name, {local, guanco_worker_pool}},
26+
{worker_module, guanco_worker},
27+
{size, PoolSize},
28+
{max_overflow, MaxOverflow}
29+
],
30+
supervisor:start_child(guanco_sup, poolboy:child_spec(guanco_worker_pool, PoolboyConfig, [])),
31+
32+
Result = poolboy:transaction(guanco_worker_pool, fun(Pid) ->
33+
gen_server:call(Pid, {get_state})
34+
end),
1735
{ok, State} = Result,
1836
io:format("State received: ~p~n", [State]),
1937
BaseUrl = maps:get(base_url, State),
2038
io:format("Base URL in state: ~p~n", [BaseUrl]),
2139
?assertEqual("http://env-url", BaseUrl),
22-
application:unset_env(guanco, ollama_api_url).
40+
41+
%% Cleanup
42+
application:unset_env(guanco, ollama_api_url),
43+
application:stop(guanco).
2344

2445
%% Test initialization with default value from app.src
2546
init_with_default_test() ->
47+
%% Re-load the configuration file
48+
ok = application:unload(guanco),
49+
ok = application:load(guanco),
50+
2651
{ok, _} = application:ensure_all_started(guanco),
2752
Result = poolboy:transaction(guanco_worker_pool, fun(Pid) ->
2853
gen_server:call(Pid, {get_state})

0 commit comments

Comments
 (0)