Skip to content

Commit

Permalink
finish bucket testing
Browse files Browse the repository at this point in the history
  • Loading branch information
changsongl committed Jun 9, 2021
1 parent 14717fb commit ed0b1d2
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
2 changes: 1 addition & 1 deletion bucket/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

const (
DefaultMaxFetchNum = 20
DefaultMaxFetchNum uint64 = 20
)

// Bucket interface to save jobs and repeat is searched
Expand Down
69 changes: 68 additions & 1 deletion bucket/bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bucket

import (
"errors"
"github.com/changsongl/delay-queue/job"
lockmock "github.com/changsongl/delay-queue/test/mock/pkg/lock"
storemock "github.com/changsongl/delay-queue/test/mock/store"
"github.com/golang/mock/gomock"
Expand All @@ -16,10 +17,10 @@ func TestBucketCreateJob(t *testing.T){
sm := storemock.NewMockStore(ctrl)
lockMk := lockmock.NewMockLocker(ctrl)

sm.EXPECT().GetLock(gomock.All()).Return(lockMk).AnyTimes()
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")
Expand All @@ -30,3 +31,69 @@ func TestBucketCreateJob(t *testing.T){
err = b.CreateJob(nil, true)
require.Equal(t, expectErr, err, "second create should be expect error")
}

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

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

sm.EXPECT().GetLock(gomock.All()).Return(lockMk).AnyTimes()
b := New(sm, 2, "test_bucket")
bucketNames := b.GetBuckets()

expectNames := []uint64{
0, 1,
}

for i, bucketName := range bucketNames{
if i > len(expectNames) {
t.Error("it is greater than expecting length")
t.FailNow()
}

require.Equal(t, expectNames[i], bucketName, "bucket names are not equal")
}
}

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

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

sm.EXPECT().GetLock(gomock.All()).Return(lockMk).AnyTimes()
b := New(sm, 2, "test_bucket")

expectErr := errors.New("error GetReadyJobsInBucket")
sm.EXPECT().GetReadyJobsInBucket(gomock.Eq("test_bucket_0"), gomock.All()).Return(nil, expectErr)
versions, err := b.GetBucketJobs(0)
require.Equal(t, expectErr, err, "it should be expecting")
require.Nil(t, versions, "version names should be nil")

expectNvs := []job.NameVersion{
"nv1", "nv2",
}
sm.EXPECT().GetReadyJobsInBucket(gomock.Eq("test_bucket_1"), gomock.All()).Return(expectNvs, nil)
versions, err = b.GetBucketJobs(1)
require.NoError(t, err, "it should have no error")
require.Equal(t, expectNvs, versions, "version names should be equal")
}

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

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

sm.EXPECT().GetLock(gomock.All()).Return(lockMk).AnyTimes()
b := New(sm, 2, "test_bucket")
require.Equal(t, DefaultMaxFetchNum, b.GetMaxFetchNum(), "fetch number should be default")

var newNum uint64 = 30
b.SetMaxFetchNum(newNum)
require.Equal(t, newNum, b.GetMaxFetchNum(), "fetch number should be new number")
}

0 comments on commit ed0b1d2

Please sign in to comment.