Best Mock code snippet using gomock.exhausted
floating_ip_test.go
Source:floating_ip_test.go  
...32		func(_ context.Context, request *ipam.AllocateIPRequest) (address string, subnetUUID string, err error) {33			if request.SubnetUUID == "uuid-1" {34				return "10.0.0.1", "uuid-1", nil35			}36			exhaustedError := addrMgrSubnetExhausted(0)37			if request.SubnetUUID == "uuid-2" {38				return "", "", &exhaustedError39			}40			if request.SubnetUUID == "uuid-3" {41				return "", "", &exhaustedError42			}43			if request.SubnetUUID == "uuid-4" {44				return "", "", fmt.Errorf("generic error")45			}46			return "10.0.0.1", "uuid-1", nil47		}).AnyTimes()48	addressManager.EXPECT().IsIPAllocated(gomock.Not(gomock.Nil()),49		&ipam.IsIPAllocatedRequest{50			VirtualNetwork: &models.VirtualNetwork{},51			IPAddress:      "10.0.0.2",52		}).Return(true, nil).AnyTimes()53}54func floatingIPSetupNextServiceMocks(s *ContrailTypeLogicService) {55	nextService := s.Next().(*servicesmock.MockService) //nolint: errcheck56	// CreateFloatingIP - response57	nextService.EXPECT().CreateFloatingIP(gomock.Not(gomock.Nil()), gomock.Not(gomock.Nil())).DoAndReturn(58		func(59			_ context.Context, request *services.CreateFloatingIPRequest,60		) (response *services.CreateFloatingIPResponse, err error) {61			return &services.CreateFloatingIPResponse{62				FloatingIP: request.FloatingIP,63			}, nil64		}).AnyTimes()65	// DeleteFloatingIP - response66	nextService.EXPECT().DeleteFloatingIP(gomock.Not(gomock.Nil()), gomock.Not(gomock.Nil())).DoAndReturn(67		func(_ context.Context, request *services.DeleteFloatingIPRequest,68		) (response *services.DeleteFloatingIPResponse, err error) {69			return &services.DeleteFloatingIPResponse{70				ID: request.ID,71			}, nil72		}).AnyTimes()73}74func floatingIPPrepareParent(s *ContrailTypeLogicService, floatingIPPool *models.FloatingIPPool) {75	readService := s.ReadService.(*servicesmock.MockReadService) //nolint: errcheck76	if floatingIPPool != nil {77		readService.EXPECT().GetFloatingIPPool(gomock.Not(gomock.Nil()), gomock.Not(gomock.Nil())).Return(78			&services.GetFloatingIPPoolResponse{79				FloatingIPPool: floatingIPPool,80			}, nil).AnyTimes()81	} else {82		readService.EXPECT().GetFloatingIPPool(gomock.Not(gomock.Nil()), gomock.Not(gomock.Nil())).Return(83			nil, fmt.Errorf("error from DB")).AnyTimes()84	}85}86func TestCreateFloatingIP(t *testing.T) {87	tests := []struct {88		name             string89		floatingIPParent *models.FloatingIPPool90		createRequest    services.CreateFloatingIPRequest91		expectedResponse services.CreateFloatingIPResponse92		fails            bool93		errorCode        codes.Code94	}{95		{96			name:             "Create floating ip when parent type is instance-ip",97			createRequest:    services.CreateFloatingIPRequest{FloatingIP: &models.FloatingIP{ParentType: "instance-ip"}},98			floatingIPParent: &models.FloatingIPPool{},99			expectedResponse: services.CreateFloatingIPResponse{FloatingIP: &models.FloatingIP{ParentType: "instance-ip"}},100		},101		{102			name:             "Create floating ip with a free ip address",103			createRequest:    services.CreateFloatingIPRequest{FloatingIP: &models.FloatingIP{ParentType: "floating-ip-pool"}},104			floatingIPParent: &models.FloatingIPPool{},105			expectedResponse: services.CreateFloatingIPResponse{FloatingIP: &models.FloatingIP{106				ParentType:        "floating-ip-pool",107				FloatingIPAddress: "10.0.0.1",108			}},109		},110		{111			name: "Try to create floating ip with IP address which is already allocated",112			createRequest: services.CreateFloatingIPRequest{FloatingIP: &models.FloatingIP{113				ParentType:        "floating-ip-pool",114				FloatingIPAddress: "10.0.0.2",115			}},116			floatingIPParent: &models.FloatingIPPool{},117			fails:            true,118			errorCode:        codes.AlreadyExists,119		},120		{121			name: "Create floating ip without IP address",122			createRequest: services.CreateFloatingIPRequest{FloatingIP: &models.FloatingIP{123				ParentType: "floating-ip-pool",124			}},125			floatingIPParent: &models.FloatingIPPool{},126			expectedResponse: services.CreateFloatingIPResponse{FloatingIP: &models.FloatingIP{127				ParentType:        "floating-ip-pool",128				FloatingIPAddress: "10.0.0.1",129			}},130		},131		{132			name: "Create floating ip with subnets from floating ip pool",133			createRequest: services.CreateFloatingIPRequest{FloatingIP: &models.FloatingIP{134				ParentType: "floating-ip-pool",135			}},136			floatingIPParent: &models.FloatingIPPool{137				FloatingIPPoolSubnets: &models.FloatingIpPoolSubnetType{138					SubnetUUID: []string{"uuid-2", "uuid-1"},139				},140			},141			expectedResponse: services.CreateFloatingIPResponse{FloatingIP: &models.FloatingIP{142				ParentType:        "floating-ip-pool",143				FloatingIPAddress: "10.0.0.1",144			}},145		},146		{147			name: "Try to create floating ip with exhausted subnets from floating ip pool",148			createRequest: services.CreateFloatingIPRequest{FloatingIP: &models.FloatingIP{149				ParentType: "floating-ip-pool",150			}},151			floatingIPParent: &models.FloatingIPPool{152				FloatingIPPoolSubnets: &models.FloatingIpPoolSubnetType{153					SubnetUUID: []string{"uuid-3", "uuid-2"},154				},155			},156			fails:     true,157			errorCode: codes.ResourceExhausted,158		},159		{160			name: "Try to create floating ip with subnets from floating ip pool with generic error",161			createRequest: services.CreateFloatingIPRequest{FloatingIP: &models.FloatingIP{...device42_iterator_test.go
Source:device42_iterator_test.go  
...38			it:               &Device42PageIterator{offset: 1, totalCount: 10, currentPage: PagedResponse{Offset: 1}},39			expectedResponse: PagedResponse{Offset: 1},40		},41		{42			name:             "exhausted",43			it:               &Device42PageIterator{exhausted: true, currentPage: PagedResponse{Offset: 10}},44			expectedResponse: PagedResponse{},45		},46		{47			name:             "iterator error",48			it:               &Device42PageIterator{offset: 2, totalCount: 10, currentPage: PagedResponse{Offset: 2}, err: errors.New("error during last req")},49			expectedResponse: PagedResponse{},50		},51	}52	for _, test := range tc {53		t.Run(test.name, func(tt *testing.T) {54			assert.Equal(tt, test.expectedResponse, test.it.Current())55		})56	}57}58func TestDevice42PageIteratorNext(t *testing.T) {59	iteratorLimit := 160	tc := []struct {61		name                  string62		itOffset              int63		itTotalCount          int64		currentPageTotalCount int65		shouldCallFetchPage   bool66		pageFetchError        error67		expected              bool68		expectedOffset        int69		exhausted             bool70	}{71		{72			name:                  "success",73			itOffset:              0,74			itTotalCount:          10,75			currentPageTotalCount: 10,76			shouldCallFetchPage:   true,77			pageFetchError:        nil,78			expected:              true,79			expectedOffset:        1,80		},81		{82			name:                  "PageFetcher error",83			itOffset:              0,84			itTotalCount:          10,85			currentPageTotalCount: 10,86			shouldCallFetchPage:   true,87			pageFetchError:        errors.New("request error"),88			expected:              false,89			expectedOffset:        0,90		},91		{92			name:                  "offset greater than totalCount",93			itOffset:              11,94			itTotalCount:          10,95			currentPageTotalCount: 10,96			shouldCallFetchPage:   false,97			pageFetchError:        nil,98			expected:              false,99			expectedOffset:        11,100			exhausted:             true,101		},102	}103	for _, test := range tc {104		t.Run(test.name, func(tt *testing.T) {105			ctrl := gomock.NewController(tt)106			defer ctrl.Finish()107			mockPageFetcher := NewMockPageFetcher(ctrl)108			if test.shouldCallFetchPage {109				mockPageFetcher.EXPECT().FetchPage(gomock.Any(), gomock.Any(), gomock.Any()).Return(PagedResponse{}, test.pageFetchError)110			}111			iterator := &Device42PageIterator{112				PageFetcher: mockPageFetcher,113				Limit:       iteratorLimit,114				currentPage: PagedResponse{TotalCount: test.currentPageTotalCount},115				offset:      test.itOffset,116				totalCount:  test.itTotalCount,117			}118			actual := iterator.Next()119			assert.Equal(tt, test.expected, actual)120			assert.Equal(tt, test.expectedOffset, iterator.offset)121			assert.Equal(tt, test.exhausted, iterator.exhausted)122		})123	}124}125func TestDevice42PageIteratorClose(t *testing.T) {126	p := &Device42PageIterator{err: errors.New("error")}127	err := p.Close()128	assert.NotNil(t, err)129}130func TestFetchPage(t *testing.T) {131	tc := []struct {132		name             string133		httpResponse     *http.Response134		httpError        error135		expectedError    bool...checkout_test.go
Source:checkout_test.go  
...132			cacheKey:      "buyer_id11",133			val:           structs.ItemsList{},134		},135		{136			caseName:      "Failed to store Items, Pool exhausted",137			expectedError: redis.ErrPoolExhausted,138			cacheKey:      "buyer_id2",139			val: structs.ItemsList{140				BuyerID:  "2",141				Sku:      "EDC-4848",142				Name:     "any-item",143				Price:    "120000",144				Quantity: 1,145			},146		},147	}148	repo := repositories.NewCheckoutRepositories(mockRepo)149	for in, c := range testCase {150		data, _ := json.Marshal(c.val)...exhausted
Using AI Code Generation
1func (m *MockMock) Exhausted() bool {2	m.ctrl.T.Helper()3	ret := m.ctrl.Call(m, "Exhausted")4	ret0, _ := ret[0].(bool)5}6func (mr *MockMockMockRecorder) Exhausted() *gomock.Call {7	mr.mock.ctrl.T.Helper()8	return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Exhausted", reflect.TypeOf((*MockMock)(nil).Exhausted))9}10func (m *MockMock) Finish() {11	m.ctrl.T.Helper()12	m.ctrl.Call(m, "Finish")13}14func (mr *MockMockMockRecorder) Finish() *gomock.Call {15	mr.mock.ctrl.T.Helper()16	return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Finish", reflect.TypeOf((*MockMock)(nil).Finish))17}18func (m *MockMock) GoString() string {19	m.ctrl.T.Helper()20	ret := m.ctrl.Call(m, "GoString")21	ret0, _ := ret[0].(string)22}23func (mr *MockMockMockRecorder) GoString() *gomock.Call {24	mr.mock.ctrl.T.Helper()25	return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GoString", reflect.TypeOf((*MockMock)(nil).GoString))26}27func (m *MockMock) String() string {28	m.ctrl.T.Helper()29	ret := m.ctrl.Call(m, "String")30	ret0, _ := ret[0].(string)31}32func (mr *MockMockMockRecorder) String() *gomock.Call {33	mr.mock.ctrl.T.Helper()34	return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "String", reflect.TypeOf((*MockMock)(nil).String))35}36func (m *MockMock) Times() gomock.CallOption {37	m.ctrl.T.Helper()38	ret := m.ctrl.Call(m, "Times")39	ret0, _ := ret[0].(gomock.CallOption)40}41func (mr *MockMockMockRecorderexhausted
Using AI Code Generation
1import (2func TestMock(t *testing.T) {3	ctrl := gomock.NewController(t)4	defer ctrl.Finish()5	mock := NewMockInterface(ctrl)6	mock.EXPECT().DoSomething().Return(nil).Times(1)7	mock.DoSomething()8	if mock.Exhausted() {9		fmt.Println("Exhausted")10	}11}12import (13type Interface interface {14	DoSomething() error15}16type mockInterface struct {17}18type mockInterfaceMockRecorder struct {19}20func NewMockInterface(ctrl *gomock.Controller) *mockInterface {21	mock := &mockInterface{ctrl: ctrl}22	mock.recorder = &mockInterfaceMockRecorder{mock}23}24func (m *mockInterface) EXPECT() *mockInterfaceMockRecorder {25}26func (m *mockInterface) DoSomething() error {27	ret := m.ctrl.Call(m, "DoSomething")28	ret0, _ := ret[0].(error)29}30func (mr *mockInterfaceMockRecorder) DoSomething() *gomock.Call {31	return mr.mock.ctrl.RecordCall(mr.mock, "DoSomething")32}exhausted
Using AI Code Generation
1func TestExhausted(t *testing.T) {2	ctrl := gomock.NewController(t)3	defer ctrl.Finish()4	mock := NewMockMyInterface(ctrl)5	mock.EXPECT().DoSomething(gomock.Any()).Return(nil).Times(1)6	mock.EXPECT().DoSomething(gomock.Any()).Return(nil).Times(1)7	mock.EXPECT().DoSomething(gomock.Any()).Return(nil).Times(1)8	mock.DoSomething("1")9	mock.DoSomething("2")10	mock.DoSomething("3")11	if !mock.Exhausted() {12		t.Error("expected exhausted")13	}14}15func TestAny(t *testing.T) {16	ctrl := gomock.NewController(t)17	defer ctrl.Finish()18	mock := NewMockMyInterface(ctrl)19	mock.EXPECT().DoSomething(gomock.Any()).Return(nil).Times(1)20	mock.EXPECT().DoSomething(gomock.Any()).Return(nil).Times(1)21	mock.EXPECT().DoSomething(gomock.Any()).Return(nil).Times(1)22	mock.DoSomething("1")23	mock.DoSomething("2")24	mock.DoSomething("3")25}26func TestAny(t *testing.T) {27	ctrl := gomock.NewController(t)28	defer ctrl.Finish()29	mock := NewMockMyInterface(ctrl)30	mock.EXPECT().DoSomething(gomock.Any()).Return(nil).Times(1)31	mock.EXPECT().DoSomething(gomock.Any()).Return(nil).Times(1)32	mock.EXPECT().DoSomething(gomock.Any()).Return(nil).Times(1)33	mock.DoSomething("1")34	mock.DoSomething("2")35	mock.DoSomething("3")36}37func TestAny(t *testing.T) {38	ctrl := gomock.NewController(t)39	defer ctrl.Finish()40	mock := NewMockMyInterface(ctrl)41	mock.EXPECT().DoSomething(gomock.Any()).Return(nil).Times(1)42	mock.EXPECT().DoSomething(gomock.Any()).Return(nil).Times(1)43	mock.EXPECT().DoSomething(gomock.Any()).Return(nil).Times(1)44	mock.DoSomething("1")45	mock.DoSomething("2")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!!
