Skip to content

Commit

Permalink
add mock file and gomock. start writing test for bucket
Browse files Browse the repository at this point in the history
  • Loading branch information
changsongl committed Jun 9, 2021
1 parent a41eac8 commit 14717fb
Show file tree
Hide file tree
Showing 5 changed files with 365 additions and 0 deletions.
32 changes: 32 additions & 0 deletions bucket/bucket_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package bucket

import (
"errors"
lockmock "github.com/changsongl/delay-queue/test/mock/pkg/lock"
storemock "github.com/changsongl/delay-queue/test/mock/store"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"
"testing"
)

func TestBucketCreateJob(t *testing.T){
ctrl := gomock.NewController(t)
defer ctrl.Finish()

sm := storemock.NewMockStore(ctrl)
lockMk := lockmock.NewMockLocker(ctrl)

b := New(sm, 10, "test_bucket")

// case1: no error
sm.EXPECT().GetLock(gomock.All()).Return(lockMk).AnyTimes()
sm.EXPECT().CreateJobInBucket(gomock.Eq("test_bucket_1"), gomock.All(), gomock.All()).Return(nil)
err := b.CreateJob(nil, true)
require.NoError(t, err, "first create should no error")

// case2: has error
expectErr := errors.New("expect error")
sm.EXPECT().CreateJobInBucket(gomock.Eq("test_bucket_2"), gomock.All(), gomock.All()).Return(expectErr)
err = b.CreateJob(nil, true)
require.Equal(t, expectErr, err, "second create should be expect error")
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/go-playground/validator/v10 v10.4.1 // indirect
github.com/go-redis/redis/v8 v8.4.4
github.com/go-redsync/redsync/v4 v4.0.4
github.com/golang/mock v1.5.0
github.com/gomodule/redigo v2.0.0+incompatible // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/prometheus/client_golang v1.9.0
Expand Down
104 changes: 104 additions & 0 deletions test/mock/bucket/bucket.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 63 additions & 0 deletions test/mock/pkg/lock/lock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

165 changes: 165 additions & 0 deletions test/mock/store/store.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 14717fb

Please sign in to comment.