Skip to content

Commit

Permalink
Fix panic due to early onEvent Logger call
Browse files Browse the repository at this point in the history
  • Loading branch information
ytake committed Feb 4, 2024
1 parent f36d1cf commit ef2eb45
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 78 deletions.
2 changes: 0 additions & 2 deletions cluster/clusterproviders/zk/zk_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,14 +339,12 @@ func (p *Provider) updateLeadership(ns []*Node) {
}

func (p *Provider) onEvent(evt zk.Event) {
p.cluster.Logger().Debug("Zookeeper event.", slog.String("type", evt.Type.String()), slog.String("state", evt.State.String()), slog.String("path", evt.Path))
if evt.Type != zk.EventSession {
return
}
switch evt.State {
case zk.StateConnecting, zk.StateDisconnected, zk.StateExpired:
if p.role == Leader {
p.cluster.Logger().Info("Role changed.", slog.String("from", Leader.String()), slog.String("to", Follower.String()))
p.role = Follower
p.roleChangedChan <- Follower
}
Expand Down
150 changes: 74 additions & 76 deletions cluster/clusterproviders/zk/zk_provider_test.go
Original file line number Diff line number Diff line change
@@ -1,78 +1,76 @@
package zk

//TODO: fix this
//
//import (
// "sync/atomic"
// "testing"
// "time"
//
// "github.com/asynkron/protoactor-go/actor"
// "github.com/asynkron/protoactor-go/cluster"
// "github.com/asynkron/protoactor-go/cluster/identitylookup/disthash"
// "github.com/asynkron/protoactor-go/remote"
// "github.com/stretchr/testify/suite"
//)
//
//type ZookeeperTestSuite struct {
// suite.Suite
//}
//
//func (suite *ZookeeperTestSuite) SetupTest() {
//
//}
//
//func (suite *ZookeeperTestSuite) TearDownTest() {
//}
//
//func TestZookeeperTestSuite(t *testing.T) {
// suite.Run(t, new(ZookeeperTestSuite))
//}
//
//type ClusterAndSystem struct {
// Cluster *cluster.Cluster
// System *actor.ActorSystem
//}
//
//func (self *ClusterAndSystem) Shutdown() {
// self.Cluster.Shutdown(true)
//}
//
//func (suite *ZookeeperTestSuite) start(name string, opts ...cluster.ConfigOption) *ClusterAndSystem {
// cp, _ := New([]string{`localhost:8000`})
// remoteConfig := remote.Configure("localhost", 0)
// config := cluster.Configure(name, cp, disthash.New(), remoteConfig, opts...)
// system := actor.NewActorSystem()
// c := cluster.New(system, config)
// c.StartMember()
// return &ClusterAndSystem{Cluster: c, System: system}
//}
//
//func (suite *ZookeeperTestSuite) TestEmptyExecute() {
// name := `cluster0`
// suite.start(name).Shutdown()
//}
//
//func (suite *ZookeeperTestSuite) TestMultiNodes() {
// var actorCount int32
// props := actor.PropsFromFunc(func(ctx actor.Context) {
// switch ctx.Message().(type) {
// case *actor.Started:
// atomic.AddInt32(&actorCount, 1)
// }
// })
// helloKind := cluster.NewKind("hello", props)
//
// name := `cluster1`
// c1 := suite.start(name, cluster.WithKinds(helloKind))
// defer c1.Shutdown()
// c2 := suite.start(name, cluster.WithKinds(helloKind))
// defer c2.Shutdown()
// c1.Cluster.Get(`a1`, `hello`)
// c2.Cluster.Get(`a2`, `hello`)
// for actorCount != 2 {
// time.Sleep(time.Microsecond * 5)
// }
// suite.Assert().Equal(2, c1.Cluster.MemberList.Members().Len(), "Expected 2 members in the cluster")
// suite.Assert().Equal(2, c2.Cluster.MemberList.Members().Len(), "Expected 2 members in the cluster")
//}
import (
"sync/atomic"
"testing"
"time"

"github.com/asynkron/protoactor-go/actor"
"github.com/asynkron/protoactor-go/cluster"
"github.com/asynkron/protoactor-go/cluster/identitylookup/disthash"
"github.com/asynkron/protoactor-go/remote"
"github.com/stretchr/testify/suite"
)

type ZookeeperTestSuite struct {
suite.Suite
}

func (suite *ZookeeperTestSuite) SetupTest() {

}

func (suite *ZookeeperTestSuite) TearDownTest() {
}

func TestZookeeperTestSuite(t *testing.T) {
suite.Run(t, new(ZookeeperTestSuite))
}

type ClusterAndSystem struct {
Cluster *cluster.Cluster
System *actor.ActorSystem
}

func (self *ClusterAndSystem) Shutdown() {
self.Cluster.Shutdown(true)
}

func (suite *ZookeeperTestSuite) start(name string, opts ...cluster.ConfigOption) *ClusterAndSystem {
cp, _ := New([]string{`localhost:8000`})
remoteConfig := remote.Configure("localhost", 0)
config := cluster.Configure(name, cp, disthash.New(), remoteConfig, opts...)
system := actor.NewActorSystem()
c := cluster.New(system, config)
c.StartMember()
return &ClusterAndSystem{Cluster: c, System: system}
}

func (suite *ZookeeperTestSuite) TestEmptyExecute() {
name := `cluster0`
suite.start(name).Shutdown()
}

func (suite *ZookeeperTestSuite) TestMultiNodes() {
var actorCount int32
props := actor.PropsFromFunc(func(ctx actor.Context) {
switch ctx.Message().(type) {
case *actor.Started:
atomic.AddInt32(&actorCount, 1)
}
})
helloKind := cluster.NewKind("hello", props)

name := `cluster1`
c1 := suite.start(name, cluster.WithKinds(helloKind))
defer c1.Shutdown()
c2 := suite.start(name, cluster.WithKinds(helloKind))
defer c2.Shutdown()
c1.Cluster.Get(`a1`, `hello`)
c2.Cluster.Get(`a2`, `hello`)
for actorCount != 2 {
time.Sleep(time.Microsecond * 5)
}
suite.Assert().Equal(2, c1.Cluster.MemberList.Members().Len(), "Expected 2 members in the cluster")
suite.Assert().Equal(2, c2.Cluster.MemberList.Members().Len(), "Expected 2 members in the cluster")
}

0 comments on commit ef2eb45

Please sign in to comment.