@@ -24,9 +24,13 @@ package matching
24
24
25
25
import (
26
26
"context"
27
+ "fmt"
27
28
"testing"
28
29
29
30
"github.com/stretchr/testify/assert"
31
+ "github.com/stretchr/testify/require"
32
+ "github.com/uber/cadence/common/log/testlogger"
33
+ "github.com/uber/cadence/service/sharddistributor/constants"
30
34
"go.uber.org/mock/gomock"
31
35
"go.uber.org/yarpc"
32
36
@@ -59,6 +63,114 @@ func TestNewClient(t *testing.T) {
59
63
assert .NotNil (t , c )
60
64
}
61
65
66
+ func TestGetShardOwner (t * testing.T ) {
67
+ ctrl := gomock .NewController (t )
68
+
69
+ mockPeerResolver := NewMockPeerResolver (ctrl )
70
+ mockShardDistributorClient := sharddistributor .NewMockClient (ctrl )
71
+ mockLogger , observedLogs := testlogger .NewObserved (t )
72
+
73
+ tests := []struct {
74
+ name string
75
+ shardDistributionMode string
76
+ shardDistributorClient sharddistributor.Client
77
+ setupMocks func ()
78
+ expectedOwner string
79
+ expectedError error
80
+ expectedLog string
81
+ }{
82
+ {
83
+ name : "ShardDistributorMode_Success" ,
84
+ shardDistributionMode : common .ShardModeShardDistributor ,
85
+ shardDistributorClient : mockShardDistributorClient ,
86
+ setupMocks : func () {
87
+ mockShardDistributorClient .EXPECT ().GetShardOwner (gomock .Any (), & types.GetShardOwnerRequest {
88
+ ShardKey : "test-tasklist" ,
89
+ Namespace : constants .MatchingNamespace ,
90
+ }).Return (& types.GetShardOwnerResponse {Owner : "shardDistributorOwner" }, nil )
91
+ },
92
+ expectedOwner : "shardDistributorOwner" ,
93
+ },
94
+ {
95
+ name : "ShardDistributorMode_Error" ,
96
+ shardDistributionMode : common .ShardModeShardDistributor ,
97
+ shardDistributorClient : mockShardDistributorClient ,
98
+ setupMocks : func () {
99
+ mockShardDistributorClient .EXPECT ().GetShardOwner (gomock .Any (), & types.GetShardOwnerRequest {
100
+ ShardKey : "test-tasklist" ,
101
+ Namespace : constants .MatchingNamespace ,
102
+ }).Return (nil , assert .AnError )
103
+ },
104
+ expectedError : fmt .Errorf ("find shard in shard distributor: %w" , assert .AnError ),
105
+ },
106
+ {
107
+ name : "ShardDistributorMode_FallbackToHashRing" ,
108
+ shardDistributionMode : common .ShardModeShardDistributor ,
109
+ shardDistributorClient : nil ,
110
+ setupMocks : func () {
111
+ mockPeerResolver .EXPECT ().FromTaskList ("test-tasklist" ).Return ("hashRingOwner" , nil )
112
+ },
113
+ expectedOwner : "hashRingOwner" ,
114
+ expectedLog : "ShardDistributor mode enabled, but shard distributor is not available, falling back to hash-ring" ,
115
+ },
116
+ {
117
+ name : "HashRingMode_Success" ,
118
+ shardDistributionMode : common .ShardModeHashRing ,
119
+ setupMocks : func () {
120
+ mockPeerResolver .EXPECT ().FromTaskList ("test-tasklist" ).Return ("hashRingOwner" , nil )
121
+ },
122
+ expectedOwner : "hashRingOwner" ,
123
+ },
124
+ {
125
+ name : "HashRingMode_Error" ,
126
+ shardDistributionMode : common .ShardModeHashRing ,
127
+ setupMocks : func () {
128
+ mockPeerResolver .EXPECT ().FromTaskList ("test-tasklist" ).Return ("" , assert .AnError )
129
+ },
130
+ expectedError : fmt .Errorf ("find shard in hash ring: %w" , assert .AnError ),
131
+ },
132
+ {
133
+ name : "UnknownMode_FallbackToHashRing" ,
134
+ shardDistributionMode : "some-bad-shard-distribution-mode" ,
135
+ setupMocks : func () {
136
+ mockPeerResolver .EXPECT ().FromTaskList ("test-tasklist" ).Return ("hashRingOwner" , nil )
137
+ },
138
+ expectedOwner : "hashRingOwner" ,
139
+ expectedLog : "Unknown hash distribution mode, falling back to hash-ring" ,
140
+ },
141
+ }
142
+
143
+ for _ , tt := range tests {
144
+ t .Run (tt .name , func (t * testing.T ) {
145
+ mockShardDistributionModeFn := func (opts ... dynamicconfig.FilterOption ) string {
146
+ return tt .shardDistributionMode
147
+ }
148
+
149
+ client := & clientImpl {
150
+ peerResolver : mockPeerResolver ,
151
+ shardDistributorClient : tt .shardDistributorClient ,
152
+ shardDistributionMode : mockShardDistributionModeFn ,
153
+ logger : mockLogger ,
154
+ }
155
+
156
+ tt .setupMocks ()
157
+ owner , err := client .getShardOwner (context .Background (), "test-tasklist" )
158
+
159
+ if tt .expectedError != nil {
160
+ require .Error (t , err )
161
+ require .Equal (t , tt .expectedError , err )
162
+ } else {
163
+ require .NoError (t , err )
164
+ require .Equal (t , tt .expectedOwner , owner )
165
+ }
166
+
167
+ if tt .expectedLog != "" {
168
+ assert .Equal (t , 1 , observedLogs .FilterMessage (tt .expectedLog ).Len ())
169
+ }
170
+ })
171
+ }
172
+ }
173
+
62
174
func TestClient_withoutResponse (t * testing.T ) {
63
175
tests := []struct {
64
176
name string
0 commit comments