Best Mock code snippet using gomock.WithContext
pipeline_test.go
Source:pipeline_test.go
...50 return s.skip51}52func TestPipeline_SetStages(t *testing.T) {53 t.Run("all set stages are executed", func(t *testing.T) {54 ctrl, ctx := gomock.WithContext(context.Background(), t)55 defer ctrl.Finish()56 payloadFactoryMock := mock.NewMockPayloadFactory(ctrl)57 payloadFactoryMock.EXPECT().GetPayload(gomock.Any()).Return(&payloadMock{}).Times(2)58 p := pipeline.NewDefault(payloadFactoryMock)59 setupTaskMock := mock.NewMockTask(ctrl)60 fetcherTaskMock := mock.NewMockTask(ctrl)61 parserTaskMock := mock.NewMockTask(ctrl)62 validatorTaskMock := mock.NewMockTask(ctrl)63 syncerTaskMock := mock.NewMockTask(ctrl)64 sequencerTaskMock := mock.NewMockTask(ctrl)65 aggregatorTaskMock := mock.NewMockTask(ctrl)66 cleanupTaskMock := mock.NewMockTask(ctrl)67 setupTaskMock.EXPECT().GetName().Return("setupTask").Times(4)68 fetcherTaskMock.EXPECT().GetName().Return("fetcherTask").Times(4)69 parserTaskMock.EXPECT().GetName().Return("parserTask").Times(4)70 validatorTaskMock.EXPECT().GetName().Return("validatorTask").Times(4)71 syncerTaskMock.EXPECT().GetName().Return("syncerTask").Times(4)72 sequencerTaskMock.EXPECT().GetName().Return("sequencerTask").Times(4)73 aggregatorTaskMock.EXPECT().GetName().Return("aggregatorTask").Times(4)74 cleanupTaskMock.EXPECT().GetName().Return("cleanupTask").Times(4)75 p.SetTasks(pipeline.StageSetup, setupTaskMock)76 p.SetTasks(pipeline.StageFetcher, fetcherTaskMock)77 p.SetTasks(pipeline.StageParser, parserTaskMock)78 p.SetTasks(pipeline.StageValidator, validatorTaskMock)79 p.SetTasks(pipeline.StageSyncer, syncerTaskMock)80 p.SetTasks(pipeline.StageSequencer, sequencerTaskMock)81 p.SetTasks(pipeline.StageAggregator, aggregatorTaskMock)82 p.SetTasks(pipeline.StageCleanup, cleanupTaskMock)83 setupTaskMock.EXPECT().Run(gomock.Any(), gomock.Any()).Return(nil).Times(2)84 fetcherTaskMock.EXPECT().Run(gomock.Any(), gomock.Any()).Return(nil).Times(2)85 parserTaskMock.EXPECT().Run(gomock.Any(), gomock.Any()).Return(nil).Times(2)86 validatorTaskMock.EXPECT().Run(gomock.Any(), gomock.Any()).Return(nil).Times(2)87 syncerTaskMock.EXPECT().Run(gomock.Any(), gomock.Any()).Return(nil).Times(2)88 sequencerTaskMock.EXPECT().Run(gomock.Any(), gomock.Any()).Return(nil).Times(2)89 aggregatorTaskMock.EXPECT().Run(gomock.Any(), gomock.Any()).Return(nil).Times(2)90 cleanupTaskMock.EXPECT().Run(gomock.Any(), gomock.Any()).Return(nil).Times(2)91 sinkMock := mock.NewMockSink(ctrl)92 sinkMock.EXPECT().Consume(gomock.Any(), gomock.Any()).Return(nil).Times(2)93 options := &pipeline.Options{}94 if err := p.Start(ctx, &sourceMock{1, 2, 1, false}, sinkMock, options); err != nil {95 t.Errorf("should not return error")96 }97 })98}99func TestPipeline_Start(t *testing.T) {100 stageErr := errors.New("err")101 t.Run("pipeline runs stages in default order when running all stages", func(t *testing.T) {102 ctrl, ctx := gomock.WithContext(context.Background(), t)103 defer ctrl.Finish()104 payloadFactoryMock := mock.NewMockPayloadFactory(ctrl)105 payloadFactoryMock.EXPECT().GetPayload(gomock.Any()).Return(&payloadMock{}).Times(1)106 p := pipeline.NewDefault(payloadFactoryMock)107 setupTaskMock := mock.NewMockTask(ctrl)108 fetcherTaskMock := mock.NewMockTask(ctrl)109 parserTaskMock := mock.NewMockTask(ctrl)110 validatorTaskMock := mock.NewMockTask(ctrl)111 syncerTaskMock := mock.NewMockTask(ctrl)112 sequencerTaskMock := mock.NewMockTask(ctrl)113 aggregatorTaskMock := mock.NewMockTask(ctrl)114 persistorTaskMock := mock.NewMockTask(ctrl)115 cleanupTaskMock := mock.NewMockTask(ctrl)116 setupTaskMock.EXPECT().GetName().Return("setupTask").Times(2)117 fetcherTaskMock.EXPECT().GetName().Return("fetcherTask").Times(2)118 parserTaskMock.EXPECT().GetName().Return("parserTask").Times(2)119 validatorTaskMock.EXPECT().GetName().Return("validatorTask").Times(2)120 syncerTaskMock.EXPECT().GetName().Return("syncerTask").Times(2)121 sequencerTaskMock.EXPECT().GetName().Return("sequencerTask").Times(2)122 aggregatorTaskMock.EXPECT().GetName().Return("aggregatorTask").Times(2)123 persistorTaskMock.EXPECT().GetName().Return("aggregatorTask").Times(2)124 cleanupTaskMock.EXPECT().GetName().Return("cleanupTask").Times(2)125 p.SetTasks(pipeline.StageSetup, setupTaskMock)126 p.SetTasks(pipeline.StageFetcher, fetcherTaskMock)127 p.SetTasks(pipeline.StageParser, parserTaskMock)128 p.SetTasks(pipeline.StageValidator, validatorTaskMock)129 p.SetTasks(pipeline.StageSyncer, syncerTaskMock)130 p.SetTasks(pipeline.StageSequencer, sequencerTaskMock)131 p.SetTasks(pipeline.StageAggregator, aggregatorTaskMock)132 p.SetTasks(pipeline.StagePersistor, persistorTaskMock)133 p.SetTasks(pipeline.StageCleanup, cleanupTaskMock)134 sinkMock := mock.NewMockSink(ctrl)135 runSetup := setupTaskMock.EXPECT().Run(gomock.Any(), gomock.Any()).Return(nil)136 runSyncer := syncerTaskMock.EXPECT().Run(gomock.Any(), gomock.Any()).Return(nil).Times(1)137 runFetcher := fetcherTaskMock.EXPECT().Run(gomock.Any(), gomock.Any()).Return(nil).Times(1)138 runParser := parserTaskMock.EXPECT().Run(gomock.Any(), gomock.Any()).Return(nil).Times(1)139 runValidator := validatorTaskMock.EXPECT().Run(gomock.Any(), gomock.Any()).Return(nil).Times(1)140 runSequencer := sequencerTaskMock.EXPECT().Run(gomock.Any(), gomock.Any()).Return(nil).Times(1)141 runAggregator := aggregatorTaskMock.EXPECT().Run(gomock.Any(), gomock.Any()).Return(nil).Times(1)142 runPersistor := persistorTaskMock.EXPECT().Run(gomock.Any(), gomock.Any()).Return(nil).Times(1)143 runCleanup := cleanupTaskMock.EXPECT().Run(gomock.Any(), gomock.Any()).Return(nil).Times(1)144 runSetup.Times(1)145 runSyncer.After(runSetup)146 runFetcher.After(runSyncer)147 runParser.After(runFetcher)148 runValidator.After(runParser)149 runSequencer.After(runValidator)150 runAggregator.After(runValidator)151 runPersistor.After(runSequencer)152 runPersistor.After(runAggregator)153 runCleanup.After(runPersistor)154 sinkMock.EXPECT().Consume(gomock.Any(), gomock.Any()).Return(nil).Times(1).After(runCleanup)155 options := &pipeline.Options{}156 if err := p.Start(ctx, &sourceMock{1, 1, 1, false}, sinkMock, options); err != nil {157 t.Errorf("did not expect error")158 }159 })160 t.Run("pipeline skip running stages", func(t *testing.T) {161 ctrl, ctx := gomock.WithContext(context.Background(), t)162 defer ctrl.Finish()163 payloadFactoryMock := mock.NewMockPayloadFactory(ctrl)164 payloadFactoryMock.EXPECT().GetPayload(gomock.Any()).Return(&payloadMock{}).Times(1)165 p := pipeline.NewDefault(payloadFactoryMock)166 cleanupTaskMock := mock.NewMockTask(ctrl)167 sinkMock := mock.NewMockSink(ctrl)168 runCleanup := cleanupTaskMock.EXPECT().Run(gomock.Any(), gomock.Any()).Return(nil).Times(0)169 sinkMock.EXPECT().Consume(gomock.Any(), gomock.Any()).Return(nil).Times(1).After(runCleanup)170 options := &pipeline.Options{}171 if err := p.Start(ctx, &sourceMock{1, 1, 1, true}, sinkMock, options); err != nil {172 t.Errorf("did not expect error")173 }174 })175 t.Run("pipeline runs stages in default order when running some stages", func(t *testing.T) {176 ctrl, ctx := gomock.WithContext(context.Background(), t)177 defer ctrl.Finish()178 payloadFactoryMock := mock.NewMockPayloadFactory(ctrl)179 payloadFactoryMock.EXPECT().GetPayload(gomock.Any()).Return(&payloadMock{}).Times(1)180 p := pipeline.NewDefault(payloadFactoryMock)181 setupTaskMock := mock.NewMockTask(ctrl)182 parserTaskMock := mock.NewMockTask(ctrl)183 syncerTaskMock := mock.NewMockTask(ctrl)184 cleanupTaskMock := mock.NewMockTask(ctrl)185 setupTaskMock.EXPECT().GetName().Return("setupTask").Times(2)186 parserTaskMock.EXPECT().GetName().Return("parserTask").Times(2)187 syncerTaskMock.EXPECT().GetName().Return("syncerTask").Times(2)188 cleanupTaskMock.EXPECT().GetName().Return("cleanupTask").Times(2)189 p.SetTasks(pipeline.StageSetup, setupTaskMock)190 p.SetTasks(pipeline.StageParser, parserTaskMock)191 p.SetTasks(pipeline.StageSyncer, syncerTaskMock)192 p.SetTasks(pipeline.StageCleanup, cleanupTaskMock)193 sinkMock := mock.NewMockSink(ctrl)194 gomock.InOrder(195 setupTaskMock.EXPECT().Run(gomock.Any(), gomock.Any()).Return(nil).Times(1),196 syncerTaskMock.EXPECT().Run(gomock.Any(), gomock.Any()).Return(nil).Times(1),197 parserTaskMock.EXPECT().Run(gomock.Any(), gomock.Any()).Return(nil).Times(1),198 cleanupTaskMock.EXPECT().Run(gomock.Any(), gomock.Any()).Return(nil).Times(1),199 sinkMock.EXPECT().Consume(gomock.Any(), gomock.Any()).Return(nil).Times(1),200 )201 options := &pipeline.Options{}202 if err := p.Start(ctx, &sourceMock{1, 1, 1, false}, sinkMock, options); err != nil {203 t.Errorf("did not expect error")204 }205 })206 t.Run("pipeline returns error if syncing stage errors", func(t *testing.T) {207 for _, stageWithErr := range [...]pipeline.StageName{208 pipeline.StageSetup,209 pipeline.StageSyncer,210 pipeline.StageFetcher,211 pipeline.StageParser,212 pipeline.StageValidator,213 pipeline.StagePersistor,214 pipeline.StageCleanup,215 } {216 ctrl, ctx := gomock.WithContext(context.Background(), t)217 defer ctrl.Finish()218 payloadFactoryMock := mock.NewMockPayloadFactory(ctrl)219 payloadFactoryMock.EXPECT().GetPayload(gomock.Any()).Return(&payloadMock{}).Times(1)220 p := pipeline.NewDefault(payloadFactoryMock)221 shouldRun := true222 for _, stage := range allStages {223 var returnVal error224 mockTask := mock.NewMockTask(ctrl)225 if !shouldRun {226 mockTask.EXPECT().GetName().Return("mockTask").Times(0)227 mockTask.EXPECT().Run(gomock.Any(), gomock.Any()).Return(nil).Times(0)228 p.SetTasks(stage, mockTask)229 continue230 }231 if stage == stageWithErr {232 returnVal = stageErr233 shouldRun = false234 }235 mockTask.EXPECT().GetName().Return("mockTask").Times(2)236 mockTask.EXPECT().Run(gomock.Any(), gomock.Any()).Return(returnVal).Times(1)237 p.SetTasks(stage, mockTask)238 }239 sinkMock := mock.NewMockSink(ctrl)240 sinkMock.EXPECT().Consume(gomock.Any(), gomock.Any()).Return(nil).Times(0)241 options := &pipeline.Options{}242 if err := p.Start(ctx, &sourceMock{1, 2, 1, false}, sinkMock, options); err != stageErr {243 t.Errorf("expected error")244 }245 }246 })247 t.Run("pipeline returns error if async stage errors", func(t *testing.T) {248 ctrl, ctx := gomock.WithContext(context.Background(), t)249 defer ctrl.Finish()250 payloadFactoryMock := mock.NewMockPayloadFactory(ctrl)251 payloadFactoryMock.EXPECT().GetPayload(gomock.Any()).Return(&payloadMock{}).Times(1)252 p := pipeline.NewDefault(payloadFactoryMock)253 aggregatorTask := mock.NewMockTask(ctrl)254 aggregatorTask.EXPECT().GetName().Return("aggregatorTask").Times(2)255 aggregatorTask.EXPECT().Run(gomock.Any(), gomock.Any()).Return(stageErr).Times(1)256 p.SetTasks(pipeline.StageAggregator, aggregatorTask)257 sequencerTask := mock.NewMockTask(ctrl)258 sequencerTask.EXPECT().GetName().Return("sequencerTask").Times(2)259 sequencerTask.EXPECT().Run(gomock.Any(), gomock.Any()).Return(nil).Times(1)260 p.SetTasks(pipeline.StageSequencer, sequencerTask)261 cleanupTask := mock.NewMockTask(ctrl)262 cleanupTask.EXPECT().GetName().Return("cleanupTask").Times(0)263 cleanupTask.EXPECT().Run(gomock.Any(), gomock.Any()).Return(nil).Times(0)264 p.SetTasks(pipeline.StageCleanup, cleanupTask)265 sinkMock := mock.NewMockSink(ctrl)266 sinkMock.EXPECT().Consume(gomock.Any(), gomock.Any()).Return(nil).Times(0)267 options := &pipeline.Options{}268 if err := p.Start(ctx, &sourceMock{1, 2, 1, false}, sinkMock, options); err == nil {269 t.Errorf("expected error")270 }271 })272}273func TestPipeline_NewCustom(t *testing.T) {274 t.Run("custom pipeline runs stages in custom order", func(t *testing.T) {275 ctrl, ctx := gomock.WithContext(context.Background(), t)276 defer ctrl.Finish()277 payloadFactoryMock := mock.NewMockPayloadFactory(ctrl)278 payloadFactoryMock.EXPECT().GetPayload(gomock.Any()).Return(&payloadMock{}).Times(1)279 p := pipeline.NewCustom(payloadFactoryMock)280 runCalls := []*gomock.Call{}281 for _, stageName := range []pipeline.StageName{282 pipeline.StageParser, pipeline.StageAggregator, pipeline.StageSetup,283 } {284 mockTask := mock.NewMockTask(ctrl)285 mockTask.EXPECT().GetName().Return("mockTask").Times(2)286 call := mockTask.EXPECT().Run(gomock.Any(), gomock.Any()).Return(nil).Times(1)287 runCalls = append(runCalls, call)288 p.AddStage(pipeline.NewStageWithTasks(stageName, mockTask))289 }290 sinkMock := mock.NewMockSink(ctrl)291 runCalls = append(runCalls,292 sinkMock.EXPECT().Consume(gomock.Any(), gomock.Any()).Return(nil).Times(1))293 gomock.InOrder(runCalls...)294 if err := p.Start(ctx, &sourceMock{1, 1, 1, false}, sinkMock, nil); err != nil {295 t.Errorf("did not expect error")296 }297 })298 t.Run("custom pipeline runs tasks in concurrent stages", func(t *testing.T) {299 ctrl, ctx := gomock.WithContext(context.Background(), t)300 defer ctrl.Finish()301 payloadFactoryMock := mock.NewMockPayloadFactory(ctrl)302 payloadFactoryMock.EXPECT().GetPayload(gomock.Any()).Return(&payloadMock{}).Times(1)303 p := pipeline.NewCustom(payloadFactoryMock)304 mockTasks := make([]pipeline.Task, 3)305 for i := 0; i < 3; i++ {306 mockTask := mock.NewMockTask(ctrl)307 mockTask.EXPECT().GetName().Return("mockTask").Times(2)308 mockTask.EXPECT().Run(gomock.Any(), gomock.Any()).Return(nil).Times(1)309 mockTasks[i] = mockTask310 }311 p.AddConcurrentStages(312 pipeline.NewStageWithTasks(pipeline.StageCleanup, mockTasks[0]),313 pipeline.NewStageWithTasks(pipeline.StageParser, mockTasks[1]),314 pipeline.NewStageWithTasks(pipeline.StageSetup, mockTasks[2]),315 )316 sinkMock := mock.NewMockSink(ctrl)317 sinkMock.EXPECT().Consume(gomock.Any(), gomock.Any()).Return(nil).Times(1)318 if err := p.Start(ctx, &sourceMock{1, 1, 1, false}, sinkMock, nil); err != nil {319 t.Errorf("did not expect error")320 }321 })322}323func TestPipeline_AddStageBefore(t *testing.T) {324 t.Run("new stage is executed before given stage", func(t *testing.T) {325 ctrl, ctx := gomock.WithContext(context.Background(), t)326 defer ctrl.Finish()327 payloadFactoryMock := mock.NewMockPayloadFactory(ctrl)328 payloadFactoryMock.EXPECT().GetPayload(gomock.Any()).Return(&payloadMock{}).Times(1)329 p := pipeline.NewDefault(payloadFactoryMock)330 stages := []struct {331 name pipeline.StageName332 existingName pipeline.StageName333 }{334 {"beforeSetup", pipeline.StageSetup},335 {"beforeFetcher", pipeline.StageFetcher},336 {"beforeParser", pipeline.StageParser},337 {"beforeCleanup", pipeline.StageCleanup},338 }339 for _, stage := range stages {340 existingStageTask := mock.NewMockTask(ctrl)341 existingStageTask.EXPECT().GetName().Return("mockTask").Times(2)342 p.SetTasks(stage.existingName, existingStageTask)343 beforeTask := mock.NewMockTask(ctrl)344 beforeTask.EXPECT().GetName().Return("mockTask").Times(2)345 p.AddStageBefore(stage.existingName, pipeline.NewStageWithTasks(stage.name, beforeTask))346 gomock.InOrder(347 beforeTask.EXPECT().Run(gomock.Any(), gomock.Any()).Return(nil).Times(1),348 existingStageTask.EXPECT().Run(gomock.Any(), gomock.Any()).Return(nil).Times(1),349 )350 }351 options := &pipeline.Options{}352 if _, err := p.Run(ctx, 1, options); err != nil {353 t.Errorf("should not return error")354 }355 })356 t.Run("pipeline returns err", func(t *testing.T) {357 ctrl, ctx := gomock.WithContext(context.Background(), t)358 defer ctrl.Finish()359 stageErr := errors.New("err")360 payloadFactoryMock := mock.NewMockPayloadFactory(ctrl)361 payloadFactoryMock.EXPECT().GetPayload(gomock.Any()).Return(&payloadMock{}).Times(1)362 p := pipeline.NewDefault(payloadFactoryMock)363 beforeTask := mock.NewMockTask(ctrl)364 beforeTask.EXPECT().GetName().Return("mockTask").Times(2)365 beforeTask.EXPECT().Run(gomock.Any(), gomock.Any()).Return(stageErr).Times(1)366 p.AddStageBefore(pipeline.StageFetcher, pipeline.NewStageWithTasks("beforeFetcher", beforeTask))367 existingStageTask := mock.NewMockTask(ctrl)368 existingStageTask.EXPECT().GetName().Return("mockTask").Times(0)369 existingStageTask.EXPECT().Run(gomock.Any(), gomock.Any()).Return(nil).Times(0)370 p.SetTasks(pipeline.StageFetcher, existingStageTask)371 options := &pipeline.Options{}372 if _, err := p.Run(ctx, 1, options); err != stageErr {373 t.Errorf("should return error")374 }375 })376}377func TestPipeline_AddStageAfter(t *testing.T) {378 t.Run("new stage is executed after existing stage", func(t *testing.T) {379 ctrl, ctx := gomock.WithContext(context.Background(), t)380 defer ctrl.Finish()381 payloadFactoryMock := mock.NewMockPayloadFactory(ctrl)382 payloadFactoryMock.EXPECT().GetPayload(gomock.Any()).Return(&payloadMock{}).Times(1)383 p := pipeline.NewDefault(payloadFactoryMock)384 stages := []struct {385 name pipeline.StageName386 existingName pipeline.StageName387 }{388 {"afterSetup", pipeline.StageSetup},389 {"afterFetcher", pipeline.StageFetcher},390 {"afterParser", pipeline.StageParser},391 {"afterCleanup", pipeline.StageCleanup},392 }393 for _, stage := range stages {394 existingStageTask := mock.NewMockTask(ctrl)395 existingStageTask.EXPECT().GetName().Return("mockTask").Times(2)396 p.SetTasks(stage.existingName, existingStageTask)397 afterTask := mock.NewMockTask(ctrl)398 afterTask.EXPECT().GetName().Return("mockTask").Times(2)399 p.AddStageAfter(stage.existingName, pipeline.NewStageWithTasks(stage.name, afterTask))400 gomock.InOrder(401 existingStageTask.EXPECT().Run(gomock.Any(), gomock.Any()).Return(nil).Times(1),402 afterTask.EXPECT().Run(gomock.Any(), gomock.Any()).Return(nil).Times(1),403 )404 }405 options := &pipeline.Options{}406 if _, err := p.Run(ctx, 1, options); err != nil {407 t.Errorf("should not return error")408 }409 })410 t.Run("pipeline returns err", func(t *testing.T) {411 ctrl, ctx := gomock.WithContext(context.Background(), t)412 defer ctrl.Finish()413 stageErr := errors.New("err")414 payloadFactoryMock := mock.NewMockPayloadFactory(ctrl)415 payloadFactoryMock.EXPECT().GetPayload(gomock.Any()).Return(&payloadMock{}).Times(1)416 p := pipeline.NewDefault(payloadFactoryMock)417 afterTask := mock.NewMockTask(ctrl)418 afterTask.EXPECT().GetName().Return("mockTask").Times(2)419 afterTask.EXPECT().Run(gomock.Any(), gomock.Any()).Return(stageErr).Times(1)420 p.AddStageAfter(pipeline.StageFetcher, pipeline.NewStageWithTasks("afterFetcher", afterTask))421 existingStageTask := mock.NewMockTask(ctrl)422 existingStageTask.EXPECT().GetName().Return("mockTask").Times(2)423 existingStageTask.EXPECT().Run(gomock.Any(), gomock.Any()).Return(nil).Times(1)424 p.SetTasks(pipeline.StageFetcher, existingStageTask)425 options := &pipeline.Options{}426 if _, err := p.Run(ctx, 1, options); err != stageErr {427 t.Errorf("should return error")428 }429 })430}431func TestPipeline_StagesBlacklist(t *testing.T) {432 t.Run("blacklisted stage should not run", func(t *testing.T) {433 ctrl, ctx := gomock.WithContext(context.Background(), t)434 defer ctrl.Finish()435 payloadFactoryMock := mock.NewMockPayloadFactory(ctrl)436 payloadFactoryMock.EXPECT().GetPayload(gomock.Any()).Return(&payloadMock{}).Times(1)437 p := pipeline.NewDefault(payloadFactoryMock)438 rand.Seed(time.Now().Unix())439 blacklistedStage := allStages[rand.Intn(len(allStages))]440 options := &pipeline.Options{441 StagesBlacklist: []pipeline.StageName{blacklistedStage},442 }443 for _, stage := range allStages {444 var calls int445 if stage != blacklistedStage {446 calls = 1447 }448 mockTask := mock.NewMockTask(ctrl)449 mockTask.EXPECT().GetName().Return("mockTask").Times(calls * 2)450 mockTask.EXPECT().Run(gomock.Any(), gomock.Any()).Return(nil).Times(calls)451 p.SetTasks(stage, mockTask)452 }453 if _, err := p.Run(ctx, 1, options); err != nil {454 t.Errorf("should not return error")455 }456 })457}458func TestPipeline_RetryStage(t *testing.T) {459 t.Run("tasks return success", func(t *testing.T) {460 ctrl, ctx := gomock.WithContext(context.Background(), t)461 defer ctrl.Finish()462 payloadFactoryMock := mock.NewMockPayloadFactory(ctrl)463 payloadFactoryMock.EXPECT().GetPayload(gomock.Any()).Return(&payloadMock{}).Times(1)464 p := pipeline.NewCustom(payloadFactoryMock)465 task1 := mock.NewMockTask(ctrl)466 task2 := mock.NewMockTask(ctrl)467 task1.EXPECT().GetName().Return("task1").Times(2)468 task2.EXPECT().GetName().Return("task2").Times(2)469 gomock.InOrder(470 task1.EXPECT().Run(gomock.Any(), gomock.Any()).Return(nil).Times(1),471 task2.EXPECT().Run(gomock.Any(), gomock.Any()).Return(nil).Times(1),472 )473 p.AddStage(pipeline.NewStageWithTasks("test_stage", task1, task2))474 p.RetryStage("test_stage", func(err error) bool { return true },475 3)476 if _, err := p.Run(ctx, 1, nil); err != nil {477 t.Errorf("should not return error")478 }479 })480 t.Run("task returns error then success", func(t *testing.T) {481 ctrl, ctx := gomock.WithContext(context.Background(), t)482 defer ctrl.Finish()483 payloadFactoryMock := mock.NewMockPayloadFactory(ctrl)484 payloadFactoryMock.EXPECT().GetPayload(gomock.Any()).Return(&payloadMock{}).Times(1)485 p := pipeline.NewCustom(payloadFactoryMock)486 task1 := mock.NewMockTask(ctrl)487 task1.EXPECT().GetName().Return("task1").Times(4)488 gomock.InOrder(489 task1.EXPECT().Run(gomock.Any(), gomock.Any()).Return(errors.New("test err")).Times(1),490 task1.EXPECT().Run(gomock.Any(), gomock.Any()).Return(nil).Times(1),491 )492 p.AddStage(pipeline.NewStageWithTasks("test_stage", task1))493 p.RetryStage("test_stage", func(err error) bool { return true },494 3)495 if _, err := p.Run(ctx, 1, nil); err != nil {496 t.Errorf("should not return error")497 }498 })499 t.Run("task returns error every try", func(t *testing.T) {500 ctrl, ctx := gomock.WithContext(context.Background(), t)501 defer ctrl.Finish()502 payloadFactoryMock := mock.NewMockPayloadFactory(ctrl)503 payloadFactoryMock.EXPECT().GetPayload(gomock.Any()).Return(&payloadMock{}).Times(1)504 p := pipeline.NewCustom(payloadFactoryMock)505 task1 := mock.NewMockTask(ctrl)506 task1.EXPECT().GetName().Return("task1").Times(6)507 task1.EXPECT().Run(gomock.Any(), gomock.Any()).Return(errors.New("test error")).Times(3)508 p.AddStage(pipeline.NewStageWithTasks("test_stage", task1))509 p.RetryStage("test_stage", func(err error) bool { return true },510 3)511 if _, err := p.Run(ctx, 1, nil); err == nil {512 t.Errorf("should return error")513 }514 })...
interfaces_mock.go
Source:interfaces_mock.go
...278func (mr *MockQueryMockRecorder) ScanCAS(arg0 ...interface{}) *gomock.Call {279 mr.mock.ctrl.T.Helper()280 return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ScanCAS", reflect.TypeOf((*MockQuery)(nil).ScanCAS), arg0...)281}282// WithContext mocks base method.283func (m *MockQuery) WithContext(arg0 context.Context) Query {284 m.ctrl.T.Helper()285 ret := m.ctrl.Call(m, "WithContext", arg0)286 ret0, _ := ret[0].(Query)287 return ret0288}289// WithContext indicates an expected call of WithContext.290func (mr *MockQueryMockRecorder) WithContext(arg0 interface{}) *gomock.Call {291 mr.mock.ctrl.T.Helper()292 return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "WithContext", reflect.TypeOf((*MockQuery)(nil).WithContext), arg0)293}294// WithTimestamp mocks base method.295func (m *MockQuery) WithTimestamp(arg0 int64) Query {296 m.ctrl.T.Helper()297 ret := m.ctrl.Call(m, "WithTimestamp", arg0)298 ret0, _ := ret[0].(Query)299 return ret0300}301// WithTimestamp indicates an expected call of WithTimestamp.302func (mr *MockQueryMockRecorder) WithTimestamp(arg0 interface{}) *gomock.Call {303 mr.mock.ctrl.T.Helper()304 return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "WithTimestamp", reflect.TypeOf((*MockQuery)(nil).WithTimestamp), arg0)305}306// MockBatch is a mock of Batch interface.307type MockBatch struct {308 ctrl *gomock.Controller309 recorder *MockBatchMockRecorder310}311// MockBatchMockRecorder is the mock recorder for MockBatch.312type MockBatchMockRecorder struct {313 mock *MockBatch314}315// NewMockBatch creates a new mock instance.316func NewMockBatch(ctrl *gomock.Controller) *MockBatch {317 mock := &MockBatch{ctrl: ctrl}318 mock.recorder = &MockBatchMockRecorder{mock}319 return mock320}321// EXPECT returns an object that allows the caller to indicate expected use.322func (m *MockBatch) EXPECT() *MockBatchMockRecorder {323 return m.recorder324}325// Query mocks base method.326func (m *MockBatch) Query(arg0 string, arg1 ...interface{}) {327 m.ctrl.T.Helper()328 varargs := []interface{}{arg0}329 for _, a := range arg1 {330 varargs = append(varargs, a)331 }332 m.ctrl.Call(m, "Query", varargs...)333}334// Query indicates an expected call of Query.335func (mr *MockBatchMockRecorder) Query(arg0 interface{}, arg1 ...interface{}) *gomock.Call {336 mr.mock.ctrl.T.Helper()337 varargs := append([]interface{}{arg0}, arg1...)338 return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Query", reflect.TypeOf((*MockBatch)(nil).Query), varargs...)339}340// WithContext mocks base method.341func (m *MockBatch) WithContext(arg0 context.Context) Batch {342 m.ctrl.T.Helper()343 ret := m.ctrl.Call(m, "WithContext", arg0)344 ret0, _ := ret[0].(Batch)345 return ret0346}347// WithContext indicates an expected call of WithContext.348func (mr *MockBatchMockRecorder) WithContext(arg0 interface{}) *gomock.Call {349 mr.mock.ctrl.T.Helper()350 return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "WithContext", reflect.TypeOf((*MockBatch)(nil).WithContext), arg0)351}352// WithTimestamp mocks base method.353func (m *MockBatch) WithTimestamp(arg0 int64) Batch {354 m.ctrl.T.Helper()355 ret := m.ctrl.Call(m, "WithTimestamp", arg0)356 ret0, _ := ret[0].(Batch)357 return ret0358}359// WithTimestamp indicates an expected call of WithTimestamp.360func (mr *MockBatchMockRecorder) WithTimestamp(arg0 interface{}) *gomock.Call {361 mr.mock.ctrl.T.Helper()362 return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "WithTimestamp", reflect.TypeOf((*MockBatch)(nil).WithTimestamp), arg0)363}364// MockIter is a mock of Iter interface....
concurrent_test.go
Source:concurrent_test.go
...18 return 0, ctx.Err()19 }20}21// testConcurrentFails is expected to fail (and is disabled). It22// demonstrates how to use gomock.WithContext to interrupt the test23// from a different goroutine.24func testConcurrentFails(t *testing.T) {25 ctrl, ctx := gomock.WithContext(context.Background(), t)26 defer ctrl.Finish()27 m := mock.NewMockMath(ctrl)28 if _, err := call(ctx, m); err != nil {29 t.Error("call failed:", err)30 }31}32func TestConcurrentWorks(t *testing.T) {33 ctrl, ctx := gomock.WithContext(context.Background(), t)34 defer ctrl.Finish()35 m := mock.NewMockMath(ctrl)36 m.EXPECT().Sum(1, 2).Return(3)37 if _, err := call(ctx, m); err != nil {38 t.Error("call failed:", err)39 }40}
WithContext
Using AI Code Generation
1import (2func TestWithContext(t *testing.T) {3 ctrl := gomock.NewController(t)4 defer ctrl.Finish()5 mockObj := mock.NewMockInterface(ctrl)6 mockObj.EXPECT().WithContext(gomock.Any()).Return(1).Times(1)7 fmt.Println(mockObj.WithContext(context.Background()))8}9import (10func TestWithContext(t *testing.T) {11 ctrl := gomock.NewController(t)12 defer ctrl.Finish()13 mockObj := mock.NewMockInterface(ctrl)14 mockObj.EXPECT().WithContext(gomock.Any()).Return(1).Times(1)15 fmt.Println(mockObj.WithContext(context.Background()))16}17import (18func TestWithContext(t *testing.T) {19 ctrl := gomock.NewController(t)20 defer ctrl.Finish()21 mockObj := mock.NewMockInterface(ctrl)22 mockObj.EXPECT().WithContext(gomock.Any()).Return(1).Times(1)23 fmt.Println(mockObj.WithContext(context.Background()))24}25import (26func TestWithContext(t *testing.T) {27 ctrl := gomock.NewController(t)28 defer ctrl.Finish()29 mockObj := mock.NewMockInterface(ctrl)30 mockObj.EXPECT().WithContext(gomock.Any()).Return(1).Times(1)31 fmt.Println(mockObj.WithContext(context.Background()))32}33import (34func TestWithContext(t *testing.T) {35 ctrl := gomock.NewController(t)36 defer ctrl.Finish()
WithContext
Using AI Code Generation
1mockCtrl := gomock.NewController(t)2defer mockCtrl.Finish()3mockService := NewMockService(mockCtrl)4mockService.EXPECT().WithContext(gomock.Any()).Return(mockService).AnyTimes()5mockService.EXPECT().DoSomething(gomock.Any()).Return(nil).AnyTimes()6mockCtrl := gomock.NewController(t)7defer mockCtrl.Finish()8mockService := NewMockService(mockCtrl)9mockService.EXPECT().WithContext(gomock.AssignableToTypeOf(context.Background())).Return(mockService).AnyTimes()10mockService.EXPECT().DoSomething(gomock.AssignableToTypeOf(context.Background())).Return(nil).AnyTimes()11mockCtrl := gomock.NewController(t)12defer mockCtrl.Finish()13mockService := NewMockService(mockCtrl)14mockService.EXPECT().WithContext(gomock.Any()).Return(mockService).AnyTimes()15mockService.EXPECT().DoSomething(gomock.Any()).Return(nil).AnyTimes()16mockCtrl := gomock.NewController(t)17defer mockCtrl.Finish()18mockService := NewMockService(mockCtrl)19mockService.EXPECT().WithContext(gomock.AssignableToTypeOf(context.Background())).Return(mockService).AnyTimes()20mockService.EXPECT().DoSomething(gomock.AssignableToTypeOf(context.Background())).Return(nil).AnyTimes()21mockCtrl := gomock.NewController(t)22defer mockCtrl.Finish()23mockService := NewMockService(mockCtrl)24mockService.EXPECT().WithContext(gomock.Any()).Return(mockService).AnyTimes()25mockService.EXPECT().DoSomething(gomock.Any()).Return(nil).AnyTimes()26mockCtrl := gomock.NewController(t)27defer mockCtrl.Finish()28mockService := NewMockService(mockCtrl)29mockService.EXPECT().WithContext(gomock.AssignableToTypeOf(context.Background())).Return(mockService).Any
WithContext
Using AI Code Generation
1import (2func TestMockWithContext(t *testing.T) {3 ctrl := gomock.NewController(t)4 defer ctrl.Finish()5 mockObj := mock.NewMockMyInterface(ctrl)6 ctx := context.Background()7 mockObj.EXPECT().WithContext(ctx).Return(ctx).Times(1)8 fmt.Println(mockObj.WithContext(ctx))9}10import (11func TestMockWithContext(t *testing.T) {12 ctrl := gomock.NewController(t)13 defer ctrl.Finish()14 mockObj := mock.NewMockMyInterface(ctrl)15 ctx := context.Background()16 mockObj.EXPECT().WithContext(ctx).Return(ctx).Times(1)17 fmt.Println(mockObj.WithContext(ctx))18}19import (20func TestMockWithContext(t *testing.T) {21 ctrl := gomock.NewController(t)22 defer ctrl.Finish()23 mockObj := mock.NewMockMyInterface(ctrl)24 ctx := context.Background()25 mockObj.EXPECT().WithContext(ctx).Return(ctx).Times(1)26 fmt.Println(mockObj.WithContext(ctx))27}28import (29func TestMockWithContext(t *testing.T) {30 ctrl := gomock.NewController(t)31 defer ctrl.Finish()32 mockObj := mock.NewMockMyInterface(ctrl)33 ctx := context.Background()34 mockObj.EXPECT().WithContext(ctx).Return(ctx).Times(1)35 fmt.Println(mockObj.WithContext(ctx))36}37import (38func TestMockWithContext(t *testing.T) {
WithContext
Using AI Code Generation
1func TestWithContext(t *testing.T) {2 ctrl := gomock.NewController(t)3 defer ctrl.Finish()4 mockObj := NewMockInterface(ctrl)5 mockObj.EXPECT().WithContext(gomock.Any()).Return(mockObj)6 mockObj.WithContext(context.Background())7}8func TestWithContext(t *testing.T) {9 ctrl := gomock.NewController(t)10 defer ctrl.Finish()11 mockObj := NewMockInterface(ctrl)12 mockObj.EXPECT().WithContext(gomock.Any()).Return(mockObj)13 mockObj.WithContext(context.Background())14}15func TestWithContext(t *testing.T) {16 ctrl := gomock.NewController(t)17 defer ctrl.Finish()18 mockObj := NewMockInterface(ctrl)19 mockObj.EXPECT().WithContext(gomock.Any()).Return(mockObj)20 mockObj.WithContext(context.Background())21}22func TestWithContext(t *testing.T) {23 ctrl := gomock.NewController(t)24 defer ctrl.Finish()25 mockObj := NewMockInterface(ctrl)26 mockObj.EXPECT().WithContext(gomock.Any()).Return(mockObj)27 mockObj.WithContext(context.Background())28}29func TestWithContext(t *testing.T) {30 ctrl := gomock.NewController(t)31 defer ctrl.Finish()32 mockObj := NewMockInterface(ctrl)33 mockObj.EXPECT().WithContext(gomock.Any()).Return(mockObj)34 mockObj.WithContext(context.Background())35}36func TestWithContext(t *testing.T) {37 ctrl := gomock.NewController(t)38 defer ctrl.Finish()39 mockObj := NewMockInterface(ctrl)40 mockObj.EXPECT().WithContext(gomock.Any()).Return(mockObj)41 mockObj.WithContext(context.Background())42}43func TestWithContext(t *testing.T) {44 ctrl := gomock.NewController(t)45 defer ctrl.Finish()46 mockObj := NewMockInterface(ctrl)47 mockObj.EXPECT().WithContext(gomock.Any()).Return(mockObj)48 mockObj.WithContext(context.Background())49}
WithContext
Using AI Code Generation
1func WithContext(ctx context.Context, req *pb.Request) (*pb.Response, error) {2}3func WithContext(ctx context.Context, req *pb.Request) (*pb.Response, error) {4}5func WithContext(ctx context.Context, req *pb.Request) (*pb.Response, error) {6}7func WithContext(ctx context.Context, req *pb.Request) (*pb.Response, error) {8}9func WithContext(ctx context.Context, req *pb.Request) (*pb.Response, error) {10}11func WithContext(ctx context.Context, req *pb.Request) (*pb.Response, error) {12}13func WithContext(ctx context.Context, req *pb.Request) (*pb.Response, error) {14}15func WithContext(ctx context.Context, req *pb.Request) (*pb.Response, error) {16}17func WithContext(ctx context.Context, req *pb.Request) (*pb.Response, error) {18}19func WithContext(ctx context.Context, req *pb.Request) (*pb.Response, error) {20}21func WithContext(ctx context.Context, req *pb.Request) (*pb.Response, error) {22}
WithContext
Using AI Code Generation
1import (2type MockFoo struct {3}4type MockFooMockRecorder struct {5}6func NewMockFoo(ctrl *gomock.Controller) *MockFoo {7 mock := &MockFoo{ctrl: ctrl}8 mock.recorder = &MockFooMockRecorder{mock}9}10func (m *MockFoo) EXPECT() *MockFooMockRecorder {11}12func (m *MockFoo) DoSomething(ctx context.Context) {13 m.ctrl.Call(m, "DoSomething", ctx)14}15func (mr *MockFooMockRecorder) DoSomething(arg0 interface{}) *gomock.Call {16 return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DoSomething", reflect.TypeOf((*MockFoo)(nil).DoSomething), arg0)17}18func main() {19 ctrl := gomock.NewController(nil)20 mockFoo := NewMockFoo(ctrl)21 mockFoo.EXPECT().DoSomething(gomock.Any()).AnyTimes()22 mockFoo.DoSomething(context.Background())23 fmt.Println("Hello, playground")24}25import (26type MockFoo struct {27}28type MockFooMockRecorder struct {29}30func NewMockFoo(ctrl *gomock.Controller) *MockFoo {31 mock := &MockFoo{ctrl: ctrl}32 mock.recorder = &MockFooMockRecorder{mock}33}34func (m *MockFoo) EXPECT() *MockFooMockRecorder {35}36func (m *MockFoo) DoSomething(ctx context.Context) {37 m.ctrl.Call(m, "DoSomething", ctx)38}39func (mr *MockFooMockRecorder) DoSomething(arg0 interface{}) *gomock.Call {40 return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DoSomething", reflect.TypeOf((*MockFoo)(nil).DoSomething), arg0)41}42func main() {
WithContext
Using AI Code Generation
1func TestWithContext(t *testing.T) {2 mock := NewMockMyInterface(mockCtrl)3 ctx := context.Background()4 mock.EXPECT().WithContext(ctx).Return(nil)5 TestFunc(ctx, mock)6}7func TestWithContext(t *testing.T) {8 mock := NewMockMyInterface(mockCtrl)9 ctx := context.Background()10 mock.EXPECT().WithContext(ctx).Return(nil)11 TestFunc(ctx, mock)12}13func TestWithContext(t *testing.T) {14 mock := NewMockMyInterface(mockCtrl)15 ctx := context.Background()16 mock.EXPECT().WithContext(ctx).Return(nil)17 TestFunc(ctx, mock)18}19func TestWithContext(t *testing.T) {20 mock := NewMockMyInterface(mockCtrl)21 ctx := context.Background()22 mock.EXPECT().WithContext(ctx).Return(nil)23 TestFunc(ctx, mock)24}25func TestWithContext(t *testing.T) {26 mock := NewMockMyInterface(mockCtrl)27 ctx := context.Background()28 mock.EXPECT().WithContext(ctx).Return(nil)29 TestFunc(ctx, mock)30}31func TestWithContext(t *testing.T) {32 mock := NewMockMyInterface(mockCtrl)33 ctx := context.Background()
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!