Browse Source

Add test for find by pid.

Roberto Ostinelli 10 years ago
parent
commit
11d2a1531f
2 changed files with 27 additions and 4 deletions
  1. 5 1
      src/syn.erl
  2. 22 3
      test/syn_register_processes_SUITE.erl

+ 5 - 1
src/syn.erl

@@ -3,7 +3,7 @@
 %% API
 -export([start/0, stop/0]).
 -export([register/2]).
--export([find_by_key/1]).
+-export([find_by_key/1, find_by_pid/1]).
 
 
 %% ===================================================================
@@ -26,3 +26,7 @@ register(Key, Pid) ->
 -spec find_by_key(Key :: any()) -> pid() | undefined.
 find_by_key(Key) ->
     syn_backbone:find_by_key(Key).
+
+-spec find_by_pid(Pid :: pid()) -> Key :: any() | undefined.
+find_by_pid(Pid) ->
+    syn_backbone:find_by_pid(Pid).

+ 22 - 3
test/syn_register_processes_SUITE.erl

@@ -7,7 +7,8 @@
 
 %% tests
 -export([
-    single_node_when_mnesia_is_ram_simple_registration/1
+    single_node_when_mnesia_is_ram_find_by_key/1,
+    single_node_when_mnesia_is_ram_find_by_pid/1
 ]).
 
 %% include
@@ -45,7 +46,8 @@ all() ->
 groups() ->
     [
         {single_node_process_registration, [shuffle], [
-            single_node_when_mnesia_is_ram_simple_registration
+            single_node_when_mnesia_is_ram_find_by_key,
+            single_node_when_mnesia_is_ram_find_by_pid
         ]}
     ].
 %% -------------------------------------------------------------------
@@ -90,7 +92,7 @@ end_per_group(_GroupName, _Config) ->
 %% ===================================================================
 %% Tests
 %% ===================================================================
-single_node_when_mnesia_is_ram_simple_registration(_Config) ->
+single_node_when_mnesia_is_ram_find_by_key(_Config) ->
     %% set schema location
     application:set_env(mnesia, schema_location, ram),
     %% start
@@ -109,6 +111,23 @@ single_node_when_mnesia_is_ram_simple_registration(_Config) ->
     %% retrieve
     undefined = syn:find_by_key(<<"my proc">>).
 
+single_node_when_mnesia_is_ram_find_by_pid(_Config) ->
+    %% set schema location
+    application:set_env(mnesia, schema_location, ram),
+    %% start
+    ok = syn:start(),
+    %% start process
+    Pid = start_process(),
+    %% register
+    syn:register(<<"my proc">>, Pid),
+    %% retrieve
+    <<"my proc">> = syn:find_by_pid(Pid),
+    %% kill process
+    kill_process(Pid),
+    timer:sleep(100),
+    %% retrieve
+    undefined = syn:find_by_pid(Pid).
+
 %% ===================================================================
 %% Internal
 %% ===================================================================