From 851a1d5c030c8c8ff600fdb4f243df1dca2f2aa5 Mon Sep 17 00:00:00 2001 From: evilolipop Date: Wed, 27 Dec 2023 09:40:05 +0800 Subject: [PATCH 1/3] feat: add max retries --- cluster/cluster_test.go | 10 +++++++++- cluster/default_context.go | 9 +++++++++ cluster/grain.go | 4 ++-- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/cluster/cluster_test.go b/cluster/cluster_test.go index a0ccdaa7..4846e592 100644 --- a/cluster/cluster_test.go +++ b/cluster/cluster_test.go @@ -112,11 +112,12 @@ func newClusterForTest(name string, cp ClusterProvider, opts ...ConfigOption) *C c.MemberList = NewMemberList(c) c.Config.RequestTimeoutTime = 1 * time.Second c.Remote = remote.NewRemote(system, c.Config.RemoteConfig) + c.IdentityLookup = &lookup return c } func TestCluster_Call(t *testing.T) { - t.Skipf("Maintaining") + // t.Skipf("Maintaining") assert := assert.New(t) members := Members{ @@ -136,6 +137,13 @@ func TestCluster_Call(t *testing.T) { assert.Nil(resp) }) + t.Run("retry", func(t *testing.T) { + msg := struct{}{} + resp, err := c.Request("nonexists", "kind", &msg) + assert.ErrorContains(err, "max retries") + assert.Nil(resp) + }) + // FIXME: testcase // t.Run("timeout", func(t *testing.T) { // msg := struct{}{} diff --git a/cluster/default_context.go b/cluster/default_context.go index fadb02e8..50f0afaa 100644 --- a/cluster/default_context.go +++ b/cluster/default_context.go @@ -67,6 +67,11 @@ selectloop: break selectloop default: + if counter >= callConfig.RetryCount { + err = fmt.Errorf("have reached max retries: %v", callConfig.RetryCount) + + break selectloop + } pid := dcc.getPid(identity, kind) if pid == nil { dcc.cluster.Logger().Debug("Requesting PID from IdentityLookup but got nil", slog.String("identity", identity), slog.String("kind", kind)) @@ -130,6 +135,10 @@ func (dcc *DefaultContext) RequestFuture(identity string, kind string, message i err := fmt.Errorf("request failed: %w", ctx.Err()) return nil, err default: + if counter >= callConfig.RetryCount { + return nil, fmt.Errorf("have reached max retries: %v", callConfig.RetryCount) + } + pid := dcc.getPid(identity, kind) if pid == nil { dcc.cluster.Logger().Debug("Requesting PID from IdentityLookup but got nil", slog.String("identity", identity), slog.String("kind", kind)) diff --git a/cluster/grain.go b/cluster/grain.go index 912ca8e0..f7a3ff01 100644 --- a/cluster/grain.go +++ b/cluster/grain.go @@ -26,8 +26,8 @@ func DefaultGrainCallConfig(cluster *Cluster) *GrainCallConfig { func NewGrainCallOptions(cluster *Cluster) *GrainCallConfig { return &GrainCallConfig{ - //TODO: set default in config - RetryCount: 10, + // TODO: set default in config + RetryCount: 3, Context: cluster.ActorSystem.Root, Timeout: cluster.Config.RequestTimeoutTime, RetryAction: func(i int) int { From 9216787516aa05341fcc44494b81c012e6b7a72a Mon Sep 17 00:00:00 2001 From: evilolipop Date: Wed, 27 Dec 2023 09:56:36 +0800 Subject: [PATCH 2/3] test: fix test --- cluster/cluster_test.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/cluster/cluster_test.go b/cluster/cluster_test.go index 4846e592..e3935aac 100644 --- a/cluster/cluster_test.go +++ b/cluster/cluster_test.go @@ -133,17 +133,17 @@ func TestCluster_Call(t *testing.T) { t.Run("invalid kind", func(t *testing.T) { msg := struct{}{} resp, err := c.Request("name", "nonkind", &msg) - assert.Equal(remote.ErrUnAvailable, err) - assert.Nil(resp) - }) - - t.Run("retry", func(t *testing.T) { - msg := struct{}{} - resp, err := c.Request("nonexists", "kind", &msg) assert.ErrorContains(err, "max retries") assert.Nil(resp) }) + // t.Run("nonexist", func(t *testing.T) { + // msg := struct{}{} + // resp, err := c.Request("nonexists", "kind", &msg) + // assert.ErrorContains(err, "max retries") + // assert.Nil(resp) + // }) + // FIXME: testcase // t.Run("timeout", func(t *testing.T) { // msg := struct{}{} From f18daa787a46fbd25a975648b6e5b39b75be9e36 Mon Sep 17 00:00:00 2001 From: evilolipop Date: Wed, 27 Dec 2023 15:46:34 +0800 Subject: [PATCH 3/3] test: add request timeout test --- cluster/cluster_test.go | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/cluster/cluster_test.go b/cluster/cluster_test.go index e3935aac..f747eee1 100644 --- a/cluster/cluster_test.go +++ b/cluster/cluster_test.go @@ -1,6 +1,7 @@ package cluster import ( + "context" "fmt" "sync" "testing" @@ -117,7 +118,6 @@ func newClusterForTest(name string, cp ClusterProvider, opts ...ConfigOption) *C } func TestCluster_Call(t *testing.T) { - // t.Skipf("Maintaining") assert := assert.New(t) members := Members{ @@ -137,22 +137,6 @@ func TestCluster_Call(t *testing.T) { assert.Nil(resp) }) - // t.Run("nonexist", func(t *testing.T) { - // msg := struct{}{} - // resp, err := c.Request("nonexists", "kind", &msg) - // assert.ErrorContains(err, "max retries") - // assert.Nil(resp) - // }) - - // FIXME: testcase - // t.Run("timeout", func(t *testing.T) { - // msg := struct{}{} - // callopts := NewGrainCallOptions(c).WithRetryCount(2).WithRequestTimeout(1 * time.Second) - // resp, err := c.Call("name", "kind", &msg, callopts) - // assert.Equalf(Remote.ErrUnknownError, err, "%v", err) - // assert.Nil(resp) - // }) - testProps := actor.PropsFromFunc( func(context actor.Context) { switch msg := context.Message().(type) { @@ -170,7 +154,13 @@ func TestCluster_Call(t *testing.T) { assert.NoError(err) assert.Equal(&struct{ Code int }{9528}, resp) }) - // t.Fatalf("need more testcases for cluster.Call") + + t.Run("timeout", func(t *testing.T) { + msg := struct{}{} + resp, err := c.Request("name", "kind", &msg, WithTimeout(time.Millisecond)) + assert.ErrorIs(err, context.DeadlineExceeded) + assert.Nil(resp) + }) } func TestCluster_Get(t *testing.T) {