Skip to content

Commit

Permalink
add both enable/disable repo_locking test
Browse files Browse the repository at this point in the history
  • Loading branch information
krrrr38 committed Dec 5, 2022
1 parent 3343ea9 commit 795328f
Showing 1 changed file with 60 additions and 22 deletions.
82 changes: 60 additions & 22 deletions server/events/project_locker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,34 +141,72 @@ func TestDefaultProjectLocker_TryLockUnlocked(t *testing.T) {
}

func TestDefaultProjectLocker_RepoLocking(t *testing.T) {
RegisterMockTestingT(t)
var githubClient *vcs.GithubClient
mockClient := vcs.NewClientProxy(githubClient, nil, nil, nil, nil)
mockLocker := mocks.NewMockLocker()
noOpLocker := mocks.NewMockLocker()
locker := events.DefaultProjectLocker{
Locker: mockLocker,
NoOpLocker: noOpLocker,
VCSClient: mockClient,
}
expProject := models.Project{}
expWorkspace := "default"
expPull := models.PullRequest{Num: 2}
expUser := models.User{}

lockKey := "key"
When(noOpLocker.TryLock(expProject, expWorkspace, expPull, expUser)).ThenReturn(
locking.TryLockResponse{
LockAcquired: true,
CurrLock: models.ProjectLock{},
LockKey: lockKey,
},
nil,
)
res, err := locker.TryLock(logging.NewNoopLogger(t), expPull, expUser, expWorkspace, expProject, false)
Ok(t, err)
Equals(t, true, res.LockAcquired)

// default locker was never called
mockLocker.VerifyWasCalled(Never()).TryLock(expProject, expWorkspace, expPull, expUser)
tests := []struct {
name string
repoLocking bool
setup func(locker *mocks.MockLocker, noOpLocker *mocks.MockLocker)
verify func(locker *mocks.MockLocker, noOpLocker *mocks.MockLocker)
}{
{
"enable repo locking",
true,
func(locker *mocks.MockLocker, noOpLocker *mocks.MockLocker) {
When(locker.TryLock(expProject, expWorkspace, expPull, expUser)).ThenReturn(
locking.TryLockResponse{
LockAcquired: true,
CurrLock: models.ProjectLock{},
LockKey: lockKey,
},
nil,
)
},
func(locker *mocks.MockLocker, noOpLocker *mocks.MockLocker) {
locker.VerifyWasCalledOnce().TryLock(expProject, expWorkspace, expPull, expUser)
noOpLocker.VerifyWasCalled(Never()).TryLock(expProject, expWorkspace, expPull, expUser)
},
},
{
"disable repo locking",
false,
func(locker *mocks.MockLocker, noOpLocker *mocks.MockLocker) {
When(noOpLocker.TryLock(expProject, expWorkspace, expPull, expUser)).ThenReturn(
locking.TryLockResponse{
LockAcquired: true,
CurrLock: models.ProjectLock{},
LockKey: lockKey,
},
nil,
)
},
func(locker *mocks.MockLocker, noOpLocker *mocks.MockLocker) {
locker.VerifyWasCalled(Never()).TryLock(expProject, expWorkspace, expPull, expUser)
noOpLocker.VerifyWasCalledOnce().TryLock(expProject, expWorkspace, expPull, expUser)
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
RegisterMockTestingT(t)
mockLocker := mocks.NewMockLocker()
mockNoOpLocker := mocks.NewMockLocker()
locker := events.DefaultProjectLocker{
Locker: mockLocker,
NoOpLocker: mockNoOpLocker,
VCSClient: mockClient,
}
tt.setup(mockLocker, mockNoOpLocker)
res, err := locker.TryLock(logging.NewNoopLogger(t), expPull, expUser, expWorkspace, expProject, tt.repoLocking)
Ok(t, err)
Equals(t, true, res.LockAcquired)
tt.verify(mockLocker, mockNoOpLocker)
})
}
}

0 comments on commit 795328f

Please sign in to comment.