Skip to content

Commit

Permalink
Added tests for ThreadSafeBooleanChangeLock
Browse files Browse the repository at this point in the history
  • Loading branch information
grzesiek2010 committed Oct 26, 2024
1 parent 1bd04b2 commit c1ea862
Showing 1 changed file with 67 additions and 0 deletions.
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))
}
}

0 comments on commit c1ea862

Please sign in to comment.