|
| 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