github.com/asynkron/protoactor-go@v0.0.0-20240308120642-ef91a6abee75/cluster/clusterproviders/zk/zk_provider_test.go (about)

     1  package zk
     2  
     3  import (
     4  	"sync/atomic"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/asynkron/protoactor-go/actor"
     9  	"github.com/asynkron/protoactor-go/cluster"
    10  	"github.com/asynkron/protoactor-go/cluster/identitylookup/disthash"
    11  	"github.com/asynkron/protoactor-go/remote"
    12  	"github.com/stretchr/testify/suite"
    13  )
    14  
    15  type ZookeeperTestSuite struct {
    16  	suite.Suite
    17  }
    18  
    19  func (suite *ZookeeperTestSuite) SetupTest() {
    20  
    21  }
    22  
    23  func (suite *ZookeeperTestSuite) TearDownTest() {
    24  }
    25  
    26  func TestZookeeperTestSuite(t *testing.T) {
    27  	suite.Run(t, new(ZookeeperTestSuite))
    28  }
    29  
    30  type ClusterAndSystem struct {
    31  	Cluster *cluster.Cluster
    32  	System  *actor.ActorSystem
    33  }
    34  
    35  func (self *ClusterAndSystem) Shutdown() {
    36  	self.Cluster.Shutdown(true)
    37  }
    38  
    39  func (suite *ZookeeperTestSuite) start(name string, opts ...cluster.ConfigOption) *ClusterAndSystem {
    40  	cp, _ := New([]string{`localhost:8000`})
    41  	remoteConfig := remote.Configure("localhost", 0)
    42  	config := cluster.Configure(name, cp, disthash.New(), remoteConfig, opts...)
    43  	system := actor.NewActorSystem()
    44  	c := cluster.New(system, config)
    45  	c.StartMember()
    46  	return &ClusterAndSystem{Cluster: c, System: system}
    47  }
    48  
    49  func (suite *ZookeeperTestSuite) TestEmptyExecute() {
    50  	name := `cluster0`
    51  	suite.start(name).Shutdown()
    52  }
    53  
    54  func (suite *ZookeeperTestSuite) TestMultiNodes() {
    55  	var actorCount int32
    56  	props := actor.PropsFromFunc(func(ctx actor.Context) {
    57  		switch ctx.Message().(type) {
    58  		case *actor.Started:
    59  			atomic.AddInt32(&actorCount, 1)
    60  		}
    61  	})
    62  	helloKind := cluster.NewKind("hello", props)
    63  
    64  	name := `cluster1`
    65  	c1 := suite.start(name, cluster.WithKinds(helloKind))
    66  	defer c1.Shutdown()
    67  	c2 := suite.start(name, cluster.WithKinds(helloKind))
    68  	defer c2.Shutdown()
    69  	c1.Cluster.Get(`a1`, `hello`)
    70  	c2.Cluster.Get(`a2`, `hello`)
    71  	for actorCount != 2 {
    72  		time.Sleep(time.Microsecond * 5)
    73  	}
    74  	suite.Assert().Equal(2, c1.Cluster.MemberList.Members().Len(), "Expected 2 members in the cluster")
    75  	suite.Assert().Equal(2, c2.Cluster.MemberList.Members().Len(), "Expected 2 members in the cluster")
    76  }