github.com/asynkron/protoactor-go@v0.0.0-20240308120642-ef91a6abee75/router/routeractor_group_test.go (about)

     1  package router
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/asynkron/protoactor-go/actor"
     7  	"github.com/stretchr/testify/mock"
     8  )
     9  
    10  func TestGroupRouterActor_Receive_AddRoute(t *testing.T) {
    11  	state := new(testRouterState)
    12  
    13  	a := groupRouterActor{state: state}
    14  
    15  	p1 := system.NewLocalPID("p1")
    16  	c := new(mockContext)
    17  	c.On("Message").Return(&AddRoutee{PID: p1})
    18  	c.On("Watch", p1).Once()
    19  
    20  	state.On("GetRoutees").Return(&actor.PIDSet{})
    21  	state.On("SetRoutees", actor.NewPIDSet(p1)).Once()
    22  
    23  	a.Receive(c)
    24  	mock.AssertExpectationsForObjects(t, state, c)
    25  }
    26  
    27  func TestGroupRouterActor_Receive_AddRoute_NoDuplicates(t *testing.T) {
    28  	state := new(testRouterState)
    29  
    30  	a := groupRouterActor{state: state}
    31  
    32  	p1 := system.NewLocalPID("p1")
    33  	c := new(mockContext)
    34  	c.On("Message").Return(&AddRoutee{PID: p1})
    35  
    36  	state.On("GetRoutees").Return(actor.NewPIDSet(p1))
    37  
    38  	a.Receive(c)
    39  	mock.AssertExpectationsForObjects(t, state, c)
    40  }
    41  
    42  func TestGroupRouterActor_Receive_RemoveRoute(t *testing.T) {
    43  	state := new(testRouterState)
    44  
    45  	a := groupRouterActor{state: state}
    46  
    47  	p1, _ := spawnMockProcess("p1")
    48  	defer removeMockProcess(p1)
    49  
    50  	p2 := system.NewLocalPID("p2")
    51  	c := new(mockContext)
    52  	c.On("Message").Return(&RemoveRoutee{PID: p1})
    53  	c.On("Unwatch", p1).
    54  		Run(func(args mock.Arguments) {
    55  		}).Once()
    56  
    57  	state.On("GetRoutees").Return(actor.NewPIDSet(p1, p2))
    58  	state.On("SetRoutees", actor.NewPIDSet(p2)).Once()
    59  
    60  	a.Receive(c)
    61  	mock.AssertExpectationsForObjects(t, state, c)
    62  }
    63  
    64  func TestGroupRouterActor_Receive_BroadcastMessage(t *testing.T) {
    65  	state := new(testRouterState)
    66  	a := groupRouterActor{state: state}
    67  
    68  	p1 := system.NewLocalPID("p1")
    69  	p2 := system.NewLocalPID("p2")
    70  
    71  	child := new(mockProcess)
    72  	child.On("SendUserMessage", mock.Anything, mock.Anything).Times(2)
    73  
    74  	system.ProcessRegistry.Add(child, "p1")
    75  	system.ProcessRegistry.Add(child, "p2")
    76  	defer func() {
    77  		system.ProcessRegistry.Remove(&actor.PID{Id: "p1"})
    78  		system.ProcessRegistry.Remove(&actor.PID{Id: "p2"})
    79  	}()
    80  
    81  	c := new(mockContext)
    82  	c.On("Message").Return(&BroadcastMessage{"hi"})
    83  	c.On("Sender").Return((*actor.PID)(nil))
    84  	c.On("RequestWithCustomSender").Twice()
    85  
    86  	state.On("GetRoutees").Return(actor.NewPIDSet(p1, p2))
    87  
    88  	a.Receive(c)
    89  	mock.AssertExpectationsForObjects(t, state, c, child)
    90  }