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

     1  package remote
     2  
     3  import (
     4  	"sort"
     5  	"testing"
     6  
     7  	"github.com/asynkron/protoactor-go/actor"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestStart(t *testing.T) {
    12  	system := actor.NewActorSystem()
    13  	config := Configure("localhost", 0)
    14  	remote := NewRemote(system, config)
    15  	remote.Start()
    16  	remote.Shutdown(true)
    17  }
    18  
    19  func TestConfig_WithAdvertisedHost(t *testing.T) {
    20  	system := actor.NewActorSystem()
    21  	config := Configure("localhost", 0, WithAdvertisedHost("Banana"))
    22  	remote := NewRemote(system, config)
    23  	remote.Start()
    24  	assert.Equal(t, "Banana", system.Address())
    25  	remote.Shutdown(true)
    26  }
    27  
    28  func TestRemote_Register(t *testing.T) {
    29  	system := actor.NewActorSystem()
    30  	config := Configure("localhost", 0, WithKinds(
    31  		NewKind("someKind", actor.PropsFromProducer(nil)),
    32  		NewKind("someOther", actor.PropsFromProducer(nil)),
    33  	))
    34  	remote := NewRemote(system, config)
    35  
    36  	kinds := remote.GetKnownKinds()
    37  	assert.Equal(t, 2, len(kinds))
    38  	sort.Strings(kinds)
    39  	assert.Equal(t, "someKind", kinds[0])
    40  	assert.Equal(t, "someOther", kinds[1])
    41  }
    42  
    43  func TestRemote_RegisterViaOptions(t *testing.T) {
    44  	system := actor.NewActorSystem()
    45  	config := Configure("localhost", 0,
    46  		WithKinds(
    47  			NewKind("someKind", actor.PropsFromProducer(nil)),
    48  			NewKind("someOther", actor.PropsFromProducer(nil))))
    49  
    50  	remote := NewRemote(system, config)
    51  	kinds := remote.GetKnownKinds()
    52  	assert.Equal(t, 2, len(kinds))
    53  	sort.Strings(kinds)
    54  	assert.Equal(t, "someKind", kinds[0])
    55  	assert.Equal(t, "someOther", kinds[1])
    56  }
    57  
    58  func TestRemote_RegisterViaStruct(t *testing.T) {
    59  	system := actor.NewActorSystem()
    60  	config := &Config{
    61  		Host: "localhost",
    62  		Port: 0,
    63  		Kinds: map[string]*actor.Props{
    64  			"someKind":  actor.PropsFromProducer(nil),
    65  			"someOther": actor.PropsFromProducer(nil),
    66  		},
    67  	}
    68  
    69  	remote := NewRemote(system, config)
    70  	kinds := remote.GetKnownKinds()
    71  	assert.Equal(t, 2, len(kinds))
    72  	sort.Strings(kinds)
    73  	assert.Equal(t, "someKind", kinds[0])
    74  	assert.Equal(t, "someOther", kinds[1])
    75  }
    76  
    77  //
    78  //func (suite *ServerTestSuite) TestStart_AdvertisedAddress() {
    79  //	// Find available Port
    80  //	lis, err := net.Listen("tcp", "127.0.0.1:0") // use :0 to choose available Port
    81  //	if err != nil {
    82  //		panic(err)
    83  //	}
    84  //	//address := lis.Addr()
    85  //	_ = lis.Close()
    86  //
    87  //	AdvertisedHost := "192.0.2.1:1234"
    88  //	remote.StartMember()
    89  //
    90  //	suite.NotEmpty(system.ProcessRegistry.RemoteHandlers, "AddressResolver should be registered on server start")
    91  //	suite.Equal(AdvertisedHost, system.ProcessRegistry.Address, "WithAdvertisedHost should have higher priority")
    92  //	suite.NotNil(activatorPid, "Activator actor should be initialized on server start")
    93  //	suite.NotNil(endpointManager, "EndpointManager should be initialized on server start")
    94  //	suite.Equal(AdvertisedHost, remote.config.AdvertisedHost, "Passed configuration option should be used")
    95  //	suite.NotNil(remote.edpReader, "EndpointReader should be initialized on server start")
    96  //	suite.NotNil(remote.s, "gRPC server should be started on server start")
    97  //}
    98  //
    99  //func (suite *ServerTestSuite) TestShutdown_Graceful() {
   100  //	remote.edpReader = &endpointReader{}
   101  //	suite.False(remote.edpReader.suspended, "EndpointReader should not be suspended at beginning")
   102  //
   103  //	endpointSupervisor, endpointSupervisorProcess := spawnMockProcess("EndpointSupervisor")
   104  //	defer removeMockProcess(endpointSupervisor)
   105  //	endpointSupervisorProcess.On("SendSystemMessage", mock.Anything, mock.Anything).
   106  //		Run(func(args mock.Arguments) {
   107  //			if suite.IsType(&actor.PID{}, args.Get(0)) {
   108  //				pid := args.Get(0).(*actor.PID)
   109  //				suite.Equal(endpointSupervisor, pid)
   110  //			}
   111  //			if suite.IsType(&actor.Watch{}, args.Get(1)) {
   112  //				watch := args.Get(1).(*actor.Watch)
   113  //				system.Root.Send(watch.Watcher, &actor.Terminated{
   114  //					Who:               endpointSupervisor,
   115  //					AddressTerminated: false,
   116  //				})
   117  //			}
   118  //		}).
   119  //		Once()
   120  //	endpointSupervisorProcess.On("Stop", endpointSupervisor).Once()
   121  //
   122  //	endpointManager = &endpointManagerValue{
   123  //		connections:        &sync.Map{},
   124  //		remote:             remote,
   125  //		endpointSupervisor: endpointSupervisor,
   126  //		endpointSub:        system.EventStream.Subscribe(func(evt interface{}) {}),
   127  //	}
   128  //
   129  //	var activatorProcess *mockProcess
   130  //	activatorPid, activatorProcess = spawnMockProcess("activator")
   131  //	defer removeMockProcess(activatorPid)
   132  //	activatorProcess.On("SendSystemMessage", mock.Anything, mock.Anything).
   133  //		Run(func(args mock.Arguments) {
   134  //			if suite.IsType(&actor.PID{}, args.Get(0)) {
   135  //				pid := args.Get(0).(*actor.PID)
   136  //				suite.Equal(activatorPid, pid)
   137  //			}
   138  //			if suite.IsType(&actor.Watch{}, args.Get(1)) {
   139  //				watch := args.Get(1).(*actor.Watch)
   140  //				system.Root.Send(watch.Watcher, &actor.Terminated{
   141  //					Who:               activatorPid,
   142  //					AddressTerminated: false,
   143  //				})
   144  //			}
   145  //		}).
   146  //		Once()
   147  //	activatorProcess.On("Stop", activatorPid).Once()
   148  //
   149  //	lis, err := net.Listen("tcp", "127.0.0.1:0") // use :0 to choose available Port
   150  //	if err != nil {
   151  //		panic(err)
   152  //	}
   153  //	defer lis.Close()
   154  //
   155  //	grpcStopped := make(chan struct{}, 1)
   156  //	remote.s = grpc.NewServer()
   157  //	go func() {
   158  //		remote.s.Serve(lis)
   159  //		grpcStopped <- struct{}{}
   160  //	}()
   161  //
   162  //	remote.Shutdown(true)
   163  //
   164  //	suite.Nil(endpointManager.endpointSub, "Subscription should reset on shutdown")
   165  //	suite.Nil(endpointManager.connections, "Connections should reset on shutdown")
   166  //
   167  //	select {
   168  //	case <-time.NewTimer(15 * time.Second).C:
   169  //		suite.FailNow("gRPC server did not stop")
   170  //	case <-grpcStopped:
   171  //		// O.K.
   172  //	}
   173  //
   174  //	endpointSupervisorProcess.AssertExpectations(suite.T())
   175  //}
   176  //
   177  //func (suite *ServerTestSuite) TestShutdown() {
   178  //	remote.edpReader = &endpointReader{}
   179  //	suite.False(remote.edpReader.suspended, "EndpointReader should not be suspended at beginning")
   180  //
   181  //	endpointSupervisor, endpointSupervisorProcess := spawnMockProcess("EndpointSupervisor")
   182  //	defer removeMockProcess(endpointSupervisor)
   183  //
   184  //	var activatorProcess *mockProcess
   185  //	activatorPid, activatorProcess = spawnMockProcess("activator")
   186  //	defer removeMockProcess(activatorPid)
   187  //
   188  //	endpointManager = &endpointManagerValue{
   189  //		connections:        &sync.Map{},
   190  //		remote:             nil,
   191  //		endpointSupervisor: endpointSupervisor,
   192  //		endpointSub:        system.EventStream.Subscribe(func(evt interface{}) {}),
   193  //	}
   194  //
   195  //	lis, err := net.Listen("tcp", "127.0.0.1:0") // use :0 to choose available Port
   196  //	if err != nil {
   197  //		panic(err)
   198  //	}
   199  //	defer lis.Close()
   200  //
   201  //	grpcStopped := make(chan struct{}, 1)
   202  //	remote.s = grpc.NewServer()
   203  //	go func() {
   204  //		remote.s.Serve(lis)
   205  //		grpcStopped <- struct{}{}
   206  //	}()
   207  //
   208  //	remote.Shutdown(false)
   209  //
   210  //	suite.NotNil(endpointManager.endpointSub, "Subscription should not reset on non-graceful shutdown")
   211  //	suite.NotNil(endpointManager.connections, "Connections should not reset on non-graceful shutdown")
   212  //
   213  //	select {
   214  //	case <-time.NewTimer(1 * time.Second).C:
   215  //		suite.FailNow("gRPC server did not stop")
   216  //	case <-grpcStopped:
   217  //		// O.K.
   218  //	}
   219  //
   220  //	activatorProcess.AssertNotCalled(suite.T(), "SendSystemMessage", mock.Anything, mock.Anything)
   221  //	activatorProcess.AssertExpectations(suite.T())
   222  //	endpointSupervisorProcess.AssertNotCalled(suite.T(), "SendSystemMessage", mock.Anything, mock.Anything)
   223  //	endpointSupervisorProcess.AssertExpectations(suite.T())
   224  //}