Skip to content

Commit ba69285

Browse files
committed
Add tests for canary ping handler
Add table-driven tests covering: - Fixed executor shard ownership verification - Ephemeral executor shard ownership verification - Executor ID extraction from grpc_address metadata Achieves 93.3% test coverage. Signed-off-by: Jakob Haahr Taankvist <[email protected]>
1 parent 7e31427 commit ba69285

File tree

2 files changed

+97
-6
lines changed

2 files changed

+97
-6
lines changed

service/sharddistributor/canary/handler/ping_handler.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package handler
22

33
import (
44
"context"
5-
"fmt"
65

76
"go.uber.org/fx"
87
"go.uber.org/zap"
@@ -112,11 +111,8 @@ func (h *PingHandler) Ping(ctx context.Context, request *sharddistributorv1.Ping
112111
}
113112

114113
func getExecutorID(metadata map[string]string) string {
115-
if id, ok := metadata["executor_id"]; ok && id != "" {
116-
return id
117-
}
118114
if addr, ok := metadata["grpc_address"]; ok && addr != "" {
119-
return fmt.Sprintf("executor@%s", addr)
115+
return addr
120116
}
121-
return "unknown"
117+
return ""
122118
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package handler
2+
3+
import (
4+
"context"
5+
"errors"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
10+
"go.uber.org/mock/gomock"
11+
"go.uber.org/zap"
12+
13+
sharddistributorv1 "github.com/uber/cadence/.gen/proto/sharddistributor/v1"
14+
"github.com/uber/cadence/service/sharddistributor/canary/processor"
15+
"github.com/uber/cadence/service/sharddistributor/canary/processorephemeral"
16+
"github.com/uber/cadence/service/sharddistributor/client/executorclient"
17+
)
18+
19+
func TestPingHandler_Ping(t *testing.T) {
20+
tests := []struct {
21+
name string
22+
namespace string
23+
shardKey string
24+
setup func(*gomock.Controller) ([]executorclient.Executor[*processor.ShardProcessor], []executorclient.Executor[*processorephemeral.ShardProcessor])
25+
wantID string
26+
wantOwns bool
27+
}{
28+
{
29+
name: "fixed executor owns shard",
30+
namespace: "ns1",
31+
shardKey: "shard-1",
32+
setup: func(ctrl *gomock.Controller) ([]executorclient.Executor[*processor.ShardProcessor], []executorclient.Executor[*processorephemeral.ShardProcessor]) {
33+
exec := executorclient.NewMockExecutor[*processor.ShardProcessor](ctrl)
34+
exec.EXPECT().GetNamespace().Return("ns1").AnyTimes()
35+
exec.EXPECT().GetShardProcess(gomock.Any(), "shard-1").Return(&processor.ShardProcessor{}, nil)
36+
exec.EXPECT().GetMetadata().Return(map[string]string{"grpc_address": "127.0.0.1:7953"})
37+
return []executorclient.Executor[*processor.ShardProcessor]{exec}, nil
38+
},
39+
wantID: "127.0.0.1:7953",
40+
wantOwns: true,
41+
},
42+
{
43+
name: "fixed executor does not own shard",
44+
namespace: "ns1",
45+
shardKey: "shard-2",
46+
setup: func(ctrl *gomock.Controller) ([]executorclient.Executor[*processor.ShardProcessor], []executorclient.Executor[*processorephemeral.ShardProcessor]) {
47+
exec := executorclient.NewMockExecutor[*processor.ShardProcessor](ctrl)
48+
exec.EXPECT().GetNamespace().Return("ns1").AnyTimes()
49+
exec.EXPECT().GetShardProcess(gomock.Any(), "shard-2").Return(nil, errors.New("not found"))
50+
exec.EXPECT().GetMetadata().Return(map[string]string{"grpc_address": "127.0.0.1:7954"})
51+
return []executorclient.Executor[*processor.ShardProcessor]{exec}, nil
52+
},
53+
wantID: "127.0.0.1:7954",
54+
wantOwns: false,
55+
},
56+
{
57+
name: "ephemeral executor owns shard",
58+
namespace: "ns2",
59+
shardKey: "shard-3",
60+
setup: func(ctrl *gomock.Controller) ([]executorclient.Executor[*processor.ShardProcessor], []executorclient.Executor[*processorephemeral.ShardProcessor]) {
61+
exec := executorclient.NewMockExecutor[*processorephemeral.ShardProcessor](ctrl)
62+
exec.EXPECT().GetNamespace().Return("ns2").AnyTimes()
63+
exec.EXPECT().GetShardProcess(gomock.Any(), "shard-3").Return(&processorephemeral.ShardProcessor{}, nil)
64+
exec.EXPECT().GetMetadata().Return(map[string]string{"grpc_address": "127.0.0.1:7955"})
65+
return nil, []executorclient.Executor[*processorephemeral.ShardProcessor]{exec}
66+
},
67+
wantID: "127.0.0.1:7955",
68+
wantOwns: true,
69+
},
70+
}
71+
72+
for _, tt := range tests {
73+
t.Run(tt.name, func(t *testing.T) {
74+
ctrl := gomock.NewController(t)
75+
defer ctrl.Finish()
76+
77+
fixed, ephemeral := tt.setup(ctrl)
78+
handler := NewPingHandler(Params{
79+
Logger: zap.NewNop(),
80+
ExecutorsFixed: fixed,
81+
ExecutorsEphemeral: ephemeral,
82+
})
83+
84+
resp, err := handler.Ping(context.Background(), &sharddistributorv1.PingRequest{
85+
Namespace: tt.namespace,
86+
ShardKey: tt.shardKey,
87+
})
88+
89+
require.NoError(t, err)
90+
assert.Equal(t, tt.wantID, resp.ExecutorId)
91+
assert.Equal(t, tt.wantOwns, resp.OwnsShard)
92+
assert.Equal(t, tt.shardKey, resp.ShardKey)
93+
})
94+
}
95+
}

0 commit comments

Comments
 (0)