@@ -26,6 +26,7 @@ import (
2626 "github.com/uber/submitqueue/core/errs"
2727 "github.com/uber/submitqueue/entity"
2828 "github.com/uber/submitqueue/entity/queue"
29+ changemock "github.com/uber/submitqueue/extension/changestore/mock"
2930 queuemock "github.com/uber/submitqueue/extension/queue/mock"
3031 "github.com/uber/submitqueue/extension/storage"
3132 storagemock "github.com/uber/submitqueue/extension/storage/mock"
@@ -34,7 +35,13 @@ import (
3435)
3536
3637// newTestController creates a controller with test dependencies.
37- func newTestController (t * testing.T , ctrl * gomock.Controller , store * storagemock.MockStorage , publishErr error ) * Controller {
38+ func newTestController (
39+ t * testing.T ,
40+ ctrl * gomock.Controller ,
41+ store * storagemock.MockStorage ,
42+ cs * changemock.MockChangeStore ,
43+ publishErr error ,
44+ ) * Controller {
3845 logger := zaptest .NewLogger (t ).Sugar ()
3946 scope := tally .NoopScope
4047
@@ -56,7 +63,7 @@ func newTestController(t *testing.T, ctrl *gomock.Controller, store *storagemock
5663 )
5764 require .NoError (t , err )
5865
59- return NewController (logger , scope , store , registry , consumer .TopicKeyStart , "orchestrator-start" )
66+ return NewController (logger , scope , store , cs , registry , consumer .TopicKeyStart , "orchestrator-start" )
6067}
6168
6269// newMockStorage creates a MockStorage with a MockRequestStore that succeeds on Create.
@@ -69,6 +76,13 @@ func newMockStorage(ctrl *gomock.Controller) *storagemock.MockStorage {
6976 return store
7077}
7178
79+ // newMockChangeStore returns a MockChangeStore that accepts any Create call.
80+ func newMockChangeStore (ctrl * gomock.Controller ) * changemock.MockChangeStore {
81+ cs := changemock .NewMockChangeStore (ctrl )
82+ cs .EXPECT ().Create (gomock .Any (), gomock .Any ()).Return (nil ).AnyTimes ()
83+ return cs
84+ }
85+
7286// makeDelivery builds a MockDelivery wrapping a serialized LandRequest.
7387func makeDelivery (t * testing.T , ctrl * gomock.Controller , lr entity.LandRequest ) * queuemock.MockDelivery {
7488 payload , err := lr .ToBytes ()
@@ -83,7 +97,7 @@ func makeDelivery(t *testing.T, ctrl *gomock.Controller, lr entity.LandRequest)
8397
8498func TestNewController (t * testing.T ) {
8599 ctrl := gomock .NewController (t )
86- controller := newTestController (t , ctrl , newMockStorage (ctrl ), nil )
100+ controller := newTestController (t , ctrl , newMockStorage (ctrl ), newMockChangeStore ( ctrl ), nil )
87101
88102 require .NotNil (t , controller )
89103 assert .Equal (t , consumer .TopicKeyStart , controller .TopicKey ())
@@ -93,7 +107,7 @@ func TestNewController(t *testing.T) {
93107
94108func TestController_Process_Success (t * testing.T ) {
95109 ctrl := gomock .NewController (t )
96- controller := newTestController (t , ctrl , newMockStorage (ctrl ), nil )
110+ controller := newTestController (t , ctrl , newMockStorage (ctrl ), newMockChangeStore ( ctrl ), nil )
97111
98112 delivery := makeDelivery (t , ctrl , entity.LandRequest {
99113 ID : "test-queue/123" ,
@@ -107,7 +121,7 @@ func TestController_Process_Success(t *testing.T) {
107121
108122func TestController_Process_InvalidJSON (t * testing.T ) {
109123 ctrl := gomock .NewController (t )
110- controller := newTestController (t , ctrl , newMockStorage (ctrl ), nil )
124+ controller := newTestController (t , ctrl , newMockStorage (ctrl ), newMockChangeStore ( ctrl ), nil )
111125
112126 invalidPayload := []byte (`{"invalid": json"}` )
113127 msg := queue .NewMessage ("invalid-msg" , invalidPayload , "partition1" , nil )
@@ -134,7 +148,7 @@ func TestController_Process_ConstructsRequestWithStateAndVersion(t *testing.T) {
134148 store := storagemock .NewMockStorage (ctrl )
135149 store .EXPECT ().GetRequestStore ().Return (mockReqStore ).AnyTimes ()
136150
137- controller := newTestController (t , ctrl , store , nil )
151+ controller := newTestController (t , ctrl , store , newMockChangeStore ( ctrl ), nil )
138152
139153 landRequest := entity.LandRequest {
140154 ID : "test-queue/42" ,
@@ -166,7 +180,7 @@ func TestController_Process_AllStrategies(t *testing.T) {
166180 for _ , tt := range tests {
167181 t .Run (tt .name , func (t * testing.T ) {
168182 ctrl := gomock .NewController (t )
169- controller := newTestController (t , ctrl , newMockStorage (ctrl ), nil )
183+ controller := newTestController (t , ctrl , newMockStorage (ctrl ), newMockChangeStore ( ctrl ), nil )
170184
171185 delivery := makeDelivery (t , ctrl , entity.LandRequest {
172186 ID : fmt .Sprintf ("queue/%s" , tt .strategy ),
@@ -182,27 +196,46 @@ func TestController_Process_AllStrategies(t *testing.T) {
182196
183197func TestController_Process_MultipleChanges (t * testing.T ) {
184198 ctrl := gomock .NewController (t )
185- controller := newTestController (t , ctrl , newMockStorage (ctrl ), nil )
186199
187- delivery := makeDelivery (t , ctrl , entity.LandRequest {
188- ID : "queue/999" ,
189- Queue : "test-queue" ,
190- Change : entity.Change {
191- URIs : []string {
192- "github://uber/monorepo/pull/1/aaa111" ,
193- "github://uber/monorepo/pull/2/bbb222" ,
194- "github://uber/monorepo/pull/3/ccc333" ,
195- },
200+ cs := changemock .NewMockChangeStore (ctrl )
201+ var captured []entity.ChangeRecord
202+ cs .EXPECT ().Create (gomock .Any (), gomock .Any ()).DoAndReturn (
203+ func (ctx context.Context , record entity.ChangeRecord ) error {
204+ captured = append (captured , record )
205+ return nil
196206 },
207+ ).Times (3 )
208+
209+ controller := newTestController (t , ctrl , newMockStorage (ctrl ), cs , nil )
210+
211+ uris := []string {
212+ "github://uber/monorepo/pull/1/aaa111" ,
213+ "github://uber/monorepo/pull/2/bbb222" ,
214+ "github://uber/monorepo/pull/3/ccc333" ,
215+ }
216+ delivery := makeDelivery (t , ctrl , entity.LandRequest {
217+ ID : "queue/999" ,
218+ Queue : "test-queue" ,
219+ Change : entity.Change {URIs : uris },
197220 LandStrategy : entity .RequestLandStrategySquashRebase ,
198221 })
199222
200223 require .NoError (t , controller .Process (context .Background (), delivery ))
224+
225+ require .Len (t , captured , len (uris ))
226+ for i , r := range captured {
227+ assert .Equal (t , uris [i ], r .URI )
228+ assert .Equal (t , "queue/999" , r .RequestID )
229+ assert .Equal (t , "test-queue" , r .Queue )
230+ assert .Equal (t , int32 (1 ), r .Version )
231+ assert .Positive (t , r .CreatedAt )
232+ assert .Equal (t , r .CreatedAt , r .UpdatedAt )
233+ }
201234}
202235
203236func TestController_Process_PublishFailure (t * testing.T ) {
204237 ctrl := gomock .NewController (t )
205- controller := newTestController (t , ctrl , newMockStorage (ctrl ), fmt .Errorf ("publish failed" ))
238+ controller := newTestController (t , ctrl , newMockStorage (ctrl ), newMockChangeStore ( ctrl ), fmt .Errorf ("publish failed" ))
206239
207240 delivery := makeDelivery (t , ctrl , entity.LandRequest {
208241 ID : "test-queue/123" ,
@@ -222,7 +255,7 @@ func TestController_Process_StorageFailure(t *testing.T) {
222255 store := storagemock .NewMockStorage (ctrl )
223256 store .EXPECT ().GetRequestStore ().Return (mockReqStore ).AnyTimes ()
224257
225- controller := newTestController (t , ctrl , store , nil )
258+ controller := newTestController (t , ctrl , store , newMockChangeStore ( ctrl ), nil )
226259
227260 delivery := makeDelivery (t , ctrl , entity.LandRequest {
228261 ID : "test-queue/123" ,
@@ -244,7 +277,7 @@ func TestController_Process_AlreadyExistsSucceeds(t *testing.T) {
244277 store := storagemock .NewMockStorage (ctrl )
245278 store .EXPECT ().GetRequestStore ().Return (mockReqStore ).AnyTimes ()
246279
247- controller := newTestController (t , ctrl , store , nil )
280+ controller := newTestController (t , ctrl , store , newMockChangeStore ( ctrl ), nil )
248281
249282 delivery := makeDelivery (t , ctrl , entity.LandRequest {
250283 ID : "test-queue/123" ,
@@ -256,9 +289,27 @@ func TestController_Process_AlreadyExistsSucceeds(t *testing.T) {
256289 require .NoError (t , controller .Process (context .Background (), delivery ))
257290}
258291
292+ func TestController_Process_ChangeStoreFailure (t * testing.T ) {
293+ ctrl := gomock .NewController (t )
294+
295+ cs := changemock .NewMockChangeStore (ctrl )
296+ cs .EXPECT ().Create (gomock .Any (), gomock .Any ()).Return (fmt .Errorf ("change store down" ))
297+
298+ controller := newTestController (t , ctrl , newMockStorage (ctrl ), cs , nil )
299+
300+ delivery := makeDelivery (t , ctrl , entity.LandRequest {
301+ ID : "test-queue/123" ,
302+ Queue : "test-queue" ,
303+ Change : entity.Change {URIs : []string {"github://uber/service/pull/1/xyz789abc" }},
304+ LandStrategy : entity .RequestLandStrategyRebase ,
305+ })
306+
307+ require .Error (t , controller .Process (context .Background (), delivery ))
308+ }
309+
259310func TestController_InterfaceImplementation (t * testing.T ) {
260311 ctrl := gomock .NewController (t )
261- controller := newTestController (t , ctrl , newMockStorage (ctrl ), nil )
312+ controller := newTestController (t , ctrl , newMockStorage (ctrl ), newMockChangeStore ( ctrl ), nil )
262313
263314 var _ consumer.Controller = controller
264315}
0 commit comments