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

     1  package router
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"log"
     7  	"log/slog"
     8  	"time"
     9  
    10  	"github.com/asynkron/protoactor-go/ctxext"
    11  
    12  	"github.com/asynkron/protoactor-go/actor"
    13  	"github.com/stretchr/testify/mock"
    14  )
    15  
    16  var nilPID *actor.PID
    17  
    18  func init() {
    19  	// discard all logging in tests
    20  	log.SetOutput(ioutil.Discard)
    21  }
    22  
    23  // mockContext
    24  type mockContext struct {
    25  	mock.Mock
    26  }
    27  
    28  func (m *mockContext) Logger() *slog.Logger {
    29  	//TODO implement me
    30  	panic("implement me")
    31  }
    32  
    33  //
    34  // Interface: Context
    35  //
    36  
    37  func (m *mockContext) Get(id ctxext.ContextExtensionID) ctxext.ContextExtension {
    38  	args := m.Called(id)
    39  	return args.Get(0).(ctxext.ContextExtension)
    40  }
    41  
    42  func (m *mockContext) Set(ext ctxext.ContextExtension) {
    43  	m.Called(ext)
    44  }
    45  
    46  func (m *mockContext) ActorSystem() *actor.ActorSystem {
    47  	args := m.Called()
    48  	return args.Get(0).(*actor.ActorSystem)
    49  }
    50  
    51  func (m *mockContext) Parent() *actor.PID {
    52  	args := m.Called()
    53  	return args.Get(0).(*actor.PID)
    54  }
    55  
    56  func (m *mockContext) Self() *actor.PID {
    57  	args := m.Called()
    58  	return args.Get(0).(*actor.PID)
    59  }
    60  
    61  func (m *mockContext) Sender() *actor.PID {
    62  	args := m.Called()
    63  	return args.Get(0).(*actor.PID)
    64  }
    65  
    66  func (m *mockContext) Actor() actor.Actor {
    67  	args := m.Called()
    68  	return args.Get(0).(actor.Actor)
    69  }
    70  
    71  func (m *mockContext) ReceiveTimeout() time.Duration {
    72  	args := m.Called()
    73  	return args.Get(0).(time.Duration)
    74  }
    75  
    76  func (m *mockContext) Children() []*actor.PID {
    77  	args := m.Called()
    78  	return args.Get(0).([]*actor.PID)
    79  }
    80  
    81  func (m *mockContext) Respond(response interface{}) {
    82  	m.Called(response)
    83  }
    84  
    85  func (m *mockContext) Stash() {
    86  	m.Called()
    87  }
    88  
    89  func (m *mockContext) Watch(pid *actor.PID) {
    90  	m.Called(pid)
    91  }
    92  
    93  func (m *mockContext) Unwatch(pid *actor.PID) {
    94  	m.Called(pid)
    95  }
    96  
    97  func (m *mockContext) SetReceiveTimeout(d time.Duration) {
    98  	m.Called(d)
    99  }
   100  
   101  func (m *mockContext) CancelReceiveTimeout() {
   102  	m.Called()
   103  }
   104  
   105  func (m *mockContext) Forward(pid *actor.PID) {
   106  	m.Called()
   107  }
   108  
   109  func (m *mockContext) ReenterAfter(f *actor.Future, cont func(res interface{}, err error)) {
   110  	m.Called(f, cont)
   111  }
   112  
   113  //
   114  // Interface: SenderContext
   115  //
   116  
   117  func (m *mockContext) Message() interface{} {
   118  	args := m.Called()
   119  	return args.Get(0)
   120  }
   121  
   122  func (m *mockContext) MessageHeader() actor.ReadonlyMessageHeader {
   123  	args := m.Called()
   124  	return args.Get(0).(actor.ReadonlyMessageHeader)
   125  }
   126  
   127  func (m *mockContext) Send(pid *actor.PID, message interface{}) {
   128  	m.Called()
   129  	p, _ := system.ProcessRegistry.Get(pid)
   130  	p.SendUserMessage(pid, message)
   131  }
   132  
   133  func (m *mockContext) Request(pid *actor.PID, message interface{}) {
   134  	args := m.Called()
   135  	p, _ := system.ProcessRegistry.Get(pid)
   136  	env := &actor.MessageEnvelope{
   137  		Header:  nil,
   138  		Message: message,
   139  		Sender:  args.Get(0).(*actor.PID),
   140  	}
   141  	p.SendUserMessage(pid, env)
   142  }
   143  
   144  func (m *mockContext) RequestWithCustomSender(pid *actor.PID, message interface{}, sender *actor.PID) {
   145  	m.Called()
   146  	p, _ := system.ProcessRegistry.Get(pid)
   147  	env := &actor.MessageEnvelope{
   148  		Header:  nil,
   149  		Message: message,
   150  		Sender:  sender,
   151  	}
   152  	p.SendUserMessage(pid, env)
   153  }
   154  
   155  func (m *mockContext) RequestFuture(pid *actor.PID, message interface{}, timeout time.Duration) *actor.Future {
   156  	args := m.Called()
   157  	m.Called()
   158  	p, _ := system.ProcessRegistry.Get(pid)
   159  	p.SendUserMessage(pid, message)
   160  	return args.Get(0).(*actor.Future)
   161  }
   162  
   163  //
   164  // Interface: ReceiverContext
   165  //
   166  
   167  func (m *mockContext) Receive(envelope *actor.MessageEnvelope) {
   168  	m.Called(envelope)
   169  }
   170  
   171  //
   172  // Interface: SpawnerContext
   173  //
   174  
   175  func (m *mockContext) Spawn(p *actor.Props) *actor.PID {
   176  	args := m.Called(p)
   177  	return args.Get(0).(*actor.PID)
   178  }
   179  
   180  func (m *mockContext) SpawnPrefix(p *actor.Props, prefix string) *actor.PID {
   181  	args := m.Called(p, prefix)
   182  	return args.Get(0).(*actor.PID)
   183  }
   184  
   185  func (m *mockContext) SpawnNamed(p *actor.Props, name string) (*actor.PID, error) {
   186  	args := m.Called(p, name)
   187  	return args.Get(0).(*actor.PID), args.Get(1).(error)
   188  }
   189  
   190  //
   191  // Interface: StopperContext
   192  //
   193  
   194  func (m *mockContext) Stop(pid *actor.PID) {
   195  	m.Called(pid)
   196  }
   197  
   198  func (m *mockContext) StopFuture(pid *actor.PID) *actor.Future {
   199  	args := m.Called(pid)
   200  	return args.Get(0).(*actor.Future)
   201  }
   202  
   203  func (m *mockContext) Poison(pid *actor.PID) {
   204  	m.Called(pid)
   205  }
   206  
   207  func (m *mockContext) PoisonFuture(pid *actor.PID) *actor.Future {
   208  	args := m.Called(pid)
   209  	return args.Get(0).(*actor.Future)
   210  }
   211  
   212  // mockProcess
   213  type mockProcess struct {
   214  	mock.Mock
   215  }
   216  
   217  func spawnMockProcess(name string) (*actor.PID, *mockProcess) {
   218  	p := &mockProcess{}
   219  	pid, ok := system.ProcessRegistry.Add(p, name)
   220  	if !ok {
   221  		panic(fmt.Errorf("did not spawn named process '%s'", name))
   222  	}
   223  
   224  	return pid, p
   225  }
   226  
   227  func removeMockProcess(pid *actor.PID) {
   228  	system.ProcessRegistry.Remove(pid)
   229  }
   230  
   231  func (m *mockProcess) SendUserMessage(pid *actor.PID, message interface{}) {
   232  	m.Called(pid, message)
   233  }
   234  
   235  func (m *mockProcess) SendSystemMessage(pid *actor.PID, message interface{}) {
   236  	m.Called(pid, message)
   237  }
   238  
   239  func (m *mockProcess) Stop(pid *actor.PID) {
   240  	m.Called(pid)
   241  }