Skip to content

Commit

Permalink
Merge pull request #33 from ScaCap/testing/boltdb
Browse files Browse the repository at this point in the history
Added tests for boltdb.go functionalities
  • Loading branch information
ghaiszaher authored Mar 7, 2022
2 parents 4100e7c + 8ca1803 commit 521adf5
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 4 deletions.
5 changes: 1 addition & 4 deletions server/core/db/boltdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ func NewWithDB(db *bolt.DB, bucket string, globalBucket string) (*BoltDB, error)
}

// TryLock attempts to create a new lock. If the lock is
// acquired, it will return true, the lock returned which will be newLock,
// and the enqueue status will be null.
// acquired, it will return true, the lock returned which will be newLock.
// If the lock is not acquired, it will return false, the current
// lock that is preventing this lock from being acquired, and the enqueue status.
func (b *BoltDB) TryLock(newLock models.ProjectLock) (bool, models.ProjectLock, models.EnqueueStatus, error) {
Expand Down Expand Up @@ -176,7 +175,6 @@ func (b *BoltDB) TryLock(newLock models.ProjectLock) (bool, models.ProjectLock,
return lockAcquired, currLock, enqueueStatus, nil
}

// TODO(ghais) refactor to a better place
func findPullRequest(locks []models.ProjectLock, pullRequestNumber int) int {
for i := range locks {
if locks[i].Pull.Num == pullRequestNumber {
Expand All @@ -192,7 +190,6 @@ func findPullRequest(locks []models.ProjectLock, pullRequestNumber int) int {
// If there is a lock, then it will delete it, and then return a pointer
// to the deleted lock.
func (b *BoltDB) Unlock(p models.Project, workspace string) (*models.ProjectLock, *models.ProjectLock, error) {
// TODO monikma extend the tests
var lock models.ProjectLock
var dequeuedLock *models.ProjectLock
foundLock := false
Expand Down
131 changes: 131 additions & 0 deletions server/core/db/boltdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,61 @@ func TestGetQueueByLock(t *testing.T) {
Equals(t, 2, len(queue))
}

func TestSingleQueue(t *testing.T) {
t.Log("locking should return correct EnqueueStatus for a single queue")
db, b := newTestDB()
defer cleanupDB(db)

lockAcquired, _, _, err := b.TryLock(lock)
Ok(t, err)
Equals(t, true, lockAcquired)

secondLock := lock
secondLock.Pull.Num = pullNum + 1
lockAcquired, _, enqueueStatus, err := b.TryLock(secondLock)
Ok(t, err)
Equals(t, false, lockAcquired)
Equals(t, models.Enqueued, enqueueStatus.Status)
Equals(t, 1, enqueueStatus.ProjectLocksInFront)

lockAcquired, _, enqueueStatus, err = b.TryLock(secondLock)
Ok(t, err)
Equals(t, false, lockAcquired)
Equals(t, models.AlreadyInTheQueue, enqueueStatus.Status)
Equals(t, 1, enqueueStatus.ProjectLocksInFront)

thirdLock := lock
thirdLock.Pull.Num = pullNum + 2
lockAcquired, _, enqueueStatus, err = b.TryLock(thirdLock)
Ok(t, err)
Equals(t, false, lockAcquired)
Equals(t, models.Enqueued, enqueueStatus.Status)
Equals(t, 2, enqueueStatus.ProjectLocksInFront)
}

func TestMultipleQueues(t *testing.T) {
t.Log("locking should return correct EnqueueStatus for multiple queues")
db, b := newTestDB()
defer cleanupDB(db)

lockAcquired, _, _, err := b.TryLock(lock)
Ok(t, err)
Equals(t, true, lockAcquired)

lockInDifferentWorkspace := lock
lockInDifferentWorkspace.Workspace = "different-workspace"
lockAcquired, _, _, err = b.TryLock(lockInDifferentWorkspace)
Ok(t, err)
Equals(t, true, lockAcquired)

secondLock := lock
secondLock.Pull.Num = pullNum + 1
lockAcquired, _, enqueueStatus, err := b.TryLock(secondLock)
Ok(t, err)
Equals(t, false, lockAcquired)
Equals(t, 1, enqueueStatus.ProjectLocksInFront)
}

func TestLockCommandNotSet(t *testing.T) {
t.Log("retrieving apply lock when there are none should return empty LockCommand")
db, b := newTestDB()
Expand Down Expand Up @@ -427,6 +482,82 @@ func TestUnlockByPullMatching(t *testing.T) {
Equals(t, 0, len(ls))
}

func TestDequeueAfterUnlock(t *testing.T) {
t.Log("unlocking should dequeue and grant lock to the next ProjectLock")
db, b := newTestDB()
defer cleanupDB(db)

// first lock acquired
_, _, _, err := b.TryLock(lock)
Ok(t, err)

// second lock enqueued
new := lock
new.Pull.Num = pullNum + 1
_, _, _, err = b.TryLock(new)
Ok(t, err)

// third lock enqueued
new2 := lock
new2.Pull.Num = pullNum + 2
_, _, _, err = b.TryLock(new2)
Ok(t, err)
queue, err := b.GetQueueByLock(lock.Project, lock.Workspace)
Ok(t, err)
Equals(t, 2, len(queue))
Equals(t, new.Pull, queue[0].Pull)
Equals(t, new2.Pull, queue[1].Pull)

// first lock unlocked -> second lock dequeued and lock acquired
_, dequeuedLock, err := b.Unlock(lock.Project, lock.Workspace)
Ok(t, err)
queue, err = b.GetQueueByLock(lock.Project, lock.Workspace)
Ok(t, err)
Equals(t, new, *dequeuedLock)
Equals(t, 1, len(queue))
Equals(t, new2.Pull, queue[0].Pull)

// second lock unlocked -> third lock dequeued and lock acquired
_, dequeuedLock, err = b.Unlock(new.Project, new.Workspace)
Ok(t, err)
Equals(t, new2, *dequeuedLock)

// third lock unlocked -> no more locks in the queue
_, dequeuedLock, err = b.Unlock(new2.Project, new2.Workspace)
Ok(t, err)
Equals(t, (*models.ProjectLock)(nil), dequeuedLock)
}

func TestDequeueAfterUnlockByPull(t *testing.T) {
t.Log("unlocking by pull should dequeue and grant lock to all dequeued ProjectLocks")
db, b := newTestDB()
defer cleanupDB(db)

_, _, _, err := b.TryLock(lock)
Ok(t, err)

lock2 := lock
lock2.Workspace = "different-workspace"
_, _, _, err = b.TryLock(lock2)
Ok(t, err)

lock3 := lock
lock3.Pull.Num = pullNum + 1
_, _, _, err = b.TryLock(lock3)
Ok(t, err)

lock4 := lock
lock4.Workspace = "different-workspace"
lock4.Pull.Num = pullNum + 1
_, _, _, err = b.TryLock(lock4)
Ok(t, err)

_, dequeueStatus, err := b.UnlockByPull(project.RepoFullName, pullNum)
Ok(t, err)

Equals(t, 2, len(dequeueStatus.ProjectLocks))
}

func TestGetLockNotThere(t *testing.T) {
t.Log("getting a lock that doesn't exist should return a nil pointer")
db, b := newTestDB()
Expand Down

0 comments on commit 521adf5

Please sign in to comment.