|
4 | 4 |
|
5 | 5 | %% Test initialization with environment variable |
6 | 6 | init_with_env_test() -> |
| 7 | + %% Ensure a clean state |
| 8 | + application:stop(guanco), |
| 9 | + application:unset_env(guanco, ollama_api_url), |
| 10 | + |
7 | 11 | {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 | + |
9 | 17 | %% Set the environment variable |
10 | 18 | application:set_env(guanco, ollama_api_url, "http://env-url"), |
11 | 19 | 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), |
17 | 35 | {ok, State} = Result, |
18 | 36 | io:format("State received: ~p~n", [State]), |
19 | 37 | BaseUrl = maps:get(base_url, State), |
20 | 38 | io:format("Base URL in state: ~p~n", [BaseUrl]), |
21 | 39 | ?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). |
23 | 44 |
|
24 | 45 | %% Test initialization with default value from app.src |
25 | 46 | init_with_default_test() -> |
| 47 | + %% Re-load the configuration file |
| 48 | + ok = application:unload(guanco), |
| 49 | + ok = application:load(guanco), |
| 50 | + |
26 | 51 | {ok, _} = application:ensure_all_started(guanco), |
27 | 52 | Result = poolboy:transaction(guanco_worker_pool, fun(Pid) -> |
28 | 53 | gen_server:call(Pid, {get_state}) |
|
0 commit comments