-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added tests for ThreadSafeBooleanChangeLock
- Loading branch information
1 parent
1bd04b2
commit c1ea862
Showing
1 changed file
with
67 additions
and
0 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
shared/src/test/java/org/odk/collect/shared/locks/ThreadSafeBooleanChangeLockTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package org.odk.collect.shared.locks | ||
|
||
import org.hamcrest.CoreMatchers.equalTo | ||
import org.hamcrest.MatcherAssert.assertThat | ||
import org.junit.Test | ||
|
||
class ThreadSafeBooleanChangeLockTest { | ||
private val changeLock = ThreadSafeBooleanChangeLock() | ||
|
||
@Test | ||
fun `tryLock acquires the lock if it is no acquired`() { | ||
val acquired = changeLock.tryLock() | ||
|
||
assertThat(acquired, equalTo(true)) | ||
} | ||
|
||
@Test | ||
fun `tryLock does not acquire the lock if it is already acquired`() { | ||
changeLock.tryLock() | ||
val acquired = changeLock.tryLock() | ||
|
||
assertThat(acquired, equalTo(false)) | ||
} | ||
|
||
@Test | ||
fun `unlock releases the lock`() { | ||
changeLock.tryLock() | ||
changeLock.unlock() | ||
val acquired = changeLock.tryLock() | ||
|
||
assertThat(acquired, equalTo(true)) | ||
} | ||
|
||
@Test | ||
fun `withLock acquires the lock if it is no acquired`() { | ||
changeLock.withLock { acquired -> | ||
assertThat(acquired, equalTo(true)) | ||
} | ||
} | ||
|
||
@Test | ||
fun `withLock does not acquire the lock if it is already acquired`() { | ||
changeLock.tryLock() | ||
changeLock.withLock { acquired -> | ||
assertThat(acquired, equalTo(false)) | ||
} | ||
} | ||
|
||
@Test | ||
fun `withLock releases the lock after performing the job`() { | ||
changeLock.withLock {} | ||
|
||
val acquired = changeLock.tryLock() | ||
|
||
assertThat(acquired, equalTo(true)) | ||
} | ||
|
||
@Test | ||
fun `withLock does not release the lock it it was not able to acquire it`() { | ||
changeLock.tryLock() | ||
changeLock.withLock {} | ||
|
||
val acquired = changeLock.tryLock() | ||
|
||
assertThat(acquired, equalTo(false)) | ||
} | ||
} |