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

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