@@ -201,7 +201,7 @@ func TestActivate(t *testing.T) {
201201 wg .Wait ()
202202 assert .Equal (t , len (c1 .Members ()), 2 )
203203 assert .True (t , c1 .HasKind ("player" ))
204- assert .True (t , c1 .GetActivated ("player/1" ).Equals (expectedPID ))
204+ assert .True (t , c1 .GetActiveByID ("player/1" ).Equals (expectedPID ))
205205
206206 c1 .Stop ()
207207 c2 .Stop ()
@@ -237,7 +237,7 @@ func TestDeactivate(t *testing.T) {
237237
238238 assert .Equal (t , len (c1 .Members ()), 2 )
239239 assert .True (t , c1 .HasKind ("player" ))
240- assert .Nil (t , c1 .GetActivated ("player/1" ))
240+ assert .Nil (t , c1 .GetActiveByID ("player/1" ))
241241
242242 c1 .Stop ()
243243 c2 .Stop ()
@@ -317,6 +317,99 @@ func TestMembersExcept(t *testing.T) {
317317 assert .Equal (t , am [0 ].ID , "C" )
318318}
319319
320+ func TestGetActiveByID (t * testing.T ) {
321+ c1Addr := getRandomLocalhostAddr ()
322+ c2Addr := getRandomLocalhostAddr ()
323+
324+ c1 := makeCluster (t , c1Addr , "A" , "eu" )
325+ c1 .RegisterKind ("player" , NewPlayer , NewKindConfig ())
326+ c1 .Start ()
327+
328+ c2 := makeCluster (t , c2Addr , "B" , "eu" )
329+ c2 .RegisterKind ("player" , NewPlayer , NewKindConfig ())
330+ c2 .Start ()
331+
332+ pid1 := c1 .Activate ("player" , NewActivationConfig ().WithID ("1" ))
333+ pid2 := c2 .Activate ("player" , NewActivationConfig ().WithID ("2" ))
334+ time .Sleep (time .Millisecond * 10 )
335+
336+ pid := c1 .GetActiveByID ("player/1" )
337+ assert .NotNil (t , pid )
338+ assert .Equal (t , pid .ID , pid1 .ID )
339+
340+ pid = c1 .GetActiveByID ("player/2" )
341+ assert .NotNil (t , pid )
342+ assert .Equal (t , pid .ID , pid2 .ID )
343+
344+ pid = c1 .GetActiveByID ("player/3" )
345+ assert .Nil (t , pid )
346+
347+ c1 .Stop ()
348+ c2 .Stop ()
349+ }
350+
351+ func TestGetActiveByKind (t * testing.T ) {
352+ c1Addr := getRandomLocalhostAddr ()
353+ c2Addr := getRandomLocalhostAddr ()
354+
355+ c1 := makeCluster (t , c1Addr , "A" , "eu" )
356+ c1 .RegisterKind ("player" , NewPlayer , NewKindConfig ())
357+ c1 .Start ()
358+
359+ c2 := makeCluster (t , c2Addr , "B" , "eu" )
360+ c2 .RegisterKind ("player" , NewPlayer , NewKindConfig ())
361+ c2 .Start ()
362+
363+ pid1 := c1 .Activate ("player" , NewActivationConfig ().WithID ("1" ))
364+ pid2 := c2 .Activate ("player" , NewActivationConfig ().WithID ("2" ))
365+ c1 .Activate ("foo" , NewActivationConfig ().WithID ("2" ))
366+ c1 .Activate ("bar" , NewActivationConfig ().WithID ("2" ))
367+ time .Sleep (time .Millisecond * 10 )
368+
369+ pids := c1 .GetActiveByKind ("player" )
370+ assert .Len (t , pids , 2 )
371+ pidsStr := make ([]string , 2 )
372+ pidsStr [0 ] = pids [0 ].String ()
373+ pidsStr [1 ] = pids [1 ].String ()
374+ assert .Contains (t , pidsStr , pid1 .String ())
375+ assert .Contains (t , pidsStr , pid2 .String ())
376+
377+ c1 .Stop ()
378+ c2 .Stop ()
379+ }
380+
381+ func TestCannotDuplicateActor (t * testing.T ) {
382+ c1Addr := getRandomLocalhostAddr ()
383+ c2Addr := getRandomLocalhostAddr ()
384+
385+ c1 := makeCluster (t , c1Addr , "A" , "eu" )
386+ c1 .RegisterKind ("player" , NewPlayer , NewKindConfig ())
387+ c1 .Start ()
388+
389+ c2 := makeCluster (t , c2Addr , "B" , "eu" )
390+ c2 .RegisterKind ("player" , NewPlayer , NewKindConfig ())
391+ c2 .Start ()
392+
393+ pid := c1 .Activate ("player" , NewActivationConfig ().WithID ("1" ))
394+ time .Sleep (10 * time .Millisecond )
395+ // Lets make sure we spawn the actor on "our" node. Why?
396+ // Because when we randomly selected the other node to spawn the actor
397+ // with the same id on the test will pass.
398+ // Local registry will prevent duplicated actor IDs from the get go.
399+ pid2 := c2 .Activate ("player" , NewActivationConfig ().WithID ("1" ).WithSelectMemberFunc (func (_ ActivationDetails ) * Member {
400+ return c2 .Member ()
401+ }))
402+ fmt .Println (pid2 )
403+ time .Sleep (time .Millisecond * 10 )
404+
405+ pids := c1 .GetActiveByKind ("player" )
406+ assert .Len (t , pids , 1 )
407+ assert .Equal (t , pids [0 ].String (), pid .String ())
408+
409+ c1 .Stop ()
410+ c2 .Stop ()
411+ }
412+
320413func makeCluster (t * testing.T , addr , id , region string ) * Cluster {
321414 config := NewConfig ().
322415 WithID (id ).
0 commit comments