Browse Source

Merge pull request #14 from nevar/embed_pool

Added the ability to embed a pool to another supervisor
Seth Falcon 11 years ago
parent
commit
0833bc3b19
4 changed files with 55 additions and 2 deletions
  1. 8 0
      src/pooler.erl
  2. 8 2
      src/pooler_sup.erl
  3. 10 0
      test/fake_external_supervisor.erl
  4. 29 0
      test/pooler_tests.erl

+ 8 - 0
src/pooler.erl

@@ -38,6 +38,7 @@
          pool_stats/1,
          manual_start/0,
          new_pool/1,
+         pool_child_spec/1,
          rm_pool/1]).
 
 %% ------------------------------------------------------------------
@@ -119,6 +120,13 @@ new_pool(PoolConfig) ->
 rm_pool(PoolName) ->
     pooler_sup:rm_pool(PoolName).
 
+%% @doc Get child spec described by the proplist `PoolConfig'.
+%%
+%% See {@link pooler:new_pool/1} for info about `PoolConfig'.
+-spec pool_child_spec([{atom(), term()}]) -> supervisor:child_spec().
+pool_child_spec(PoolConfig) ->
+    pooler_sup:pool_child_spec(PoolConfig).
+
 %% @doc For INTERNAL use. Adds `MemberPid' to the pool.
 -spec accept_member(atom() | pid(), pid() | {noproc, _}) -> ok.
 accept_member(PoolName, MemberPid) ->

+ 8 - 2
src/pooler_sup.erl

@@ -5,6 +5,7 @@
 -export([init/1,
          new_pool/1,
          rm_pool/1,
+         pool_child_spec/1,
          start_link/0]).
 
 -include("pooler.hrl").
@@ -29,10 +30,15 @@ init([]) ->
 %% @doc Create a new pool from proplist pool config `PoolConfig'. The
 %% public API for this functionality is {@link pooler:new_pool/1}.
 new_pool(PoolConfig) ->
+    Spec = pool_child_spec(PoolConfig),
+    supervisor:start_child(?MODULE, Spec).
+
+%% @doc Create a child spec for new pool from proplist pool config `PoolConfig'. The
+%% public API for this functionality is {@link pooler:pool_child_spec/1}.
+pool_child_spec(PoolConfig) ->
     MetricsConfig = {metrics_mod, metrics_module()},
     NewPool = pooler_config:list_to_pool([MetricsConfig | PoolConfig]),
-    Spec = pool_sup_spec(NewPool),
-    supervisor:start_child(?MODULE, Spec).
+    pool_sup_spec(NewPool).
 
 %% @doc Shutdown the named pool.
 rm_pool(Name) ->

+ 10 - 0
test/fake_external_supervisor.erl

@@ -0,0 +1,10 @@
+%% @author Slava Yurin <v.yurin@office.ngs.ru>
+%% @doc Fake supervisor that not under pooler_sup
+-module(fake_external_supervisor).
+
+-behaviour(supervisor).
+
+-export([init/1]).
+
+init(Pool) ->
+	{ok, {{one_for_one, 1, 1}, [pooler:pool_child_spec(Pool)]}}.

+ 29 - 0
test/pooler_tests.erl

@@ -155,6 +155,35 @@ pooler_basics_dynamic_test_() ->
      end,
      basic_tests()}}.
 
+pooler_basics_integration_to_other_supervisor_test_() ->
+    {setup,
+     fun() ->
+             application:set_env(pooler, metrics_module, fake_metrics),
+             fake_metrics:start_link()
+     end,
+     fun(_X) ->
+             fake_metrics:stop()
+     end,
+    {foreach,
+     % setup
+     fun() ->
+             Pool = [{name, test_pool_1},
+                     {max_count, 3},
+                     {init_count, 2},
+                     {start_mfa,
+                      {pooled_gs, start_link, [{"type-0"}]}}],
+             application:unset_env(pooler, pools),
+             error_logger:delete_report_handler(error_logger_tty_h),
+             application:start(pooler),
+             supervisor:start_link(fake_external_supervisor, Pool)
+     end,
+     fun({ok, SupPid}) ->
+             exit(SupPid, normal),
+             application:stop(pooler)
+     end,
+     basic_tests()}}.
+
+
 basic_tests() ->
      [
       {"there are init_count members at start",