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

     1  package router
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/asynkron/protoactor-go/actor"
     9  	"github.com/stretchr/testify/mock"
    10  )
    11  
    12  var (
    13  	_ fmt.Formatter
    14  	_ time.Time
    15  )
    16  
    17  // TODO fix this
    18  func __TestRouterSendsUserMessageToChild(t *testing.T) {
    19  	child, p := spawnMockProcess("child")
    20  	defer removeMockProcess(child)
    21  
    22  	p.On("SendUserMessage", mock.Anything, mock.MatchedBy(func(env interface{}) bool {
    23  		_, msg, _ := actor.UnwrapEnvelope(env)
    24  		return msg.(string) == "hello"
    25  	}))
    26  	p.On("SendSystemMessage", mock.Anything, mock.Anything)
    27  
    28  	s1 := actor.NewPIDSet(child)
    29  
    30  	rs := new(testRouterState)
    31  	//	rs.On("SetSender",)
    32  	rs.On("SetRoutees", s1)
    33  	rs.On("RouteMessage", mock.MatchedBy(func(env interface{}) bool {
    34  		_, msg, _ := actor.UnwrapEnvelope(env)
    35  		return msg.(string) == "hello"
    36  	}), mock.Anything)
    37  
    38  	grc := newGroupRouterConfig(child)
    39  	grc.On("CreateRouterState").Return(rs)
    40  
    41  	routerPID := system.Root.Spawn((&actor.Props{}).Configure(actor.WithSpawnFunc(spawner(grc))))
    42  	system.Root.Send(routerPID, "hello")
    43  	system.Root.RequestWithCustomSender(routerPID, "hello", routerPID)
    44  
    45  	mock.AssertExpectationsForObjects(t, p, rs)
    46  }
    47  
    48  type testGroupRouter struct {
    49  	GroupRouter
    50  	mock.Mock
    51  }
    52  
    53  func newGroupRouterConfig(routees ...*actor.PID) *testGroupRouter {
    54  	r := new(testGroupRouter)
    55  	r.Routees = actor.NewPIDSet(routees...)
    56  	return r
    57  }
    58  
    59  func (m *testGroupRouter) CreateRouterState() State {
    60  	args := m.Called()
    61  	return args.Get(0).(*testRouterState)
    62  }
    63  
    64  type testRouterState struct {
    65  	mock.Mock
    66  	routees *actor.PIDSet
    67  	sender  actor.SenderContext
    68  }
    69  
    70  func (m *testRouterState) SetSender(sender actor.SenderContext) {
    71  	m.Called(sender)
    72  	m.sender = sender
    73  }
    74  
    75  func (m *testRouterState) SetRoutees(routees *actor.PIDSet) {
    76  	m.Called(routees)
    77  	m.routees = routees
    78  }
    79  
    80  func (m *testRouterState) RouteMessage(message interface{}) {
    81  	m.Called(message)
    82  	m.routees.ForEach(func(i int, pid *actor.PID) {
    83  		system.Root.Send(pid, message)
    84  	})
    85  }
    86  
    87  func (m *testRouterState) GetRoutees() *actor.PIDSet {
    88  	args := m.Called()
    89  	return args.Get(0).(*actor.PIDSet)
    90  }