- Problem: Concurrent counter increments leading to lost values
In this example, we have a simple
Counter class with an increment operation. If multiple threads increment the counter concurrently, we may encounter a race condition where an increment is lost.
class Counter {
var value = 0
fun increment() {
value++
}
fun get() = value
}
Lincheck test to expose the issue:
import org.jetbrains.kotlinx.lincheck.check
import org.jetbrains.kotlinx.lincheck.annotations.Operation
import org.jetbrains.kotlinx.lincheck.strategy.stress.StressOptions
import org.junit.jupiter.api.Test
class CounterTest {
private val counter = Counter()
@Operation
fun increment() = counter.increment()
@Operation
fun get() = counter.get()
@Test
fun stressTest() {
StressOptions()
.iterations(100)
.invocationsPerIteration(50)
.threads(4)
.actorsPerThread(10)
.verify()
}
}
- Problem: Concurrent access leading to inconsistent results in a simplistic bank account implementation
class BankAccount {
var balance = 0.0
fun withdraw(amount: Double): Boolean {
if (balance >= amount) {
balance -= amount
return true
}
return false
}
fun deposit(amount: Double) {
balance += amount
}
}
Lincheck test to expose the issue:
import org.jetbrains.kotlinx.lincheck.check
import org.jetbrains.kotlinx.lincheck.annotations.Operation
import org.jetbrains.kotlinx.lincheck.strategy.stress.StressOptions
import org.junit.jupiter.api.Test
class BankAccountTest {
private val account = BankAccount()
@Operation
fun deposit(amount: Double) = account.deposit(amount)
@Operation
fun withdraw(amount: Double) = account.withdraw(amount)
@Test
fun stressTest() {
StressOptions()
.iterations(100)
.invocationsPerIteration(50)
.threads(4)
.actorsPerThread(10)
.verify()
}
}
- Problem: Using a simple resizing ArrayList implementation resulting in missing elements when resized concurrently
class SimpleResizableList {
private var array = arrayOfNulls<Int>(2)
private var size = 0
fun add(value: Int): Boolean {
if (size == array.size) {
array = array.copyOf(size * 2)
}
array[size++] = value
return true
}
fun get(index: Int): Int? {
if (index < size) {
return array[index]
}
return null
}
}
Lincheck test to expose the issue:
import org.jetbrains.kotlinx.lincheck.annotations.Operation
import org.jetbrains.kotlinx.lincheck.strategy.stress.StressOptions
import org.junit.jupiter.api.Test
class SimpleResizableListTest {
private val list = SimpleResizableList()
@Operation
fun add(value: Int) = list.add(value)
@Operation
fun get(index: Int) = list.get(index)
@Test
fun stressTest() {
StressOptions()
.iterations(100)
.invocationsPerIteration(50)
.threads(4)
.actorsPerThread(10)
.verify()
}
}
- Problem: Race condition causing a deadlock in a Dining Philosophers-like problem
class Resource {
private val lock = Object()
fun doWork() {
synchronized(lock) {
Thread.sleep(100)
}
}
}
class ResourceUser(private val first: Resource, private val second: Resource) {
fun work() {
synchronized(first) {
synchronized(second) {
first.doWork()
second.doWork()
}
}
}
}
Lincheck test to expose the issue:
import org.jetbrains.kotlinx.lincheck.check
import org.jetbrains.kotlinx.lincheck.annotations.Operation
import org.jetbrains.kotlinx.lincheck.strategy.stress.StressOptions
import org.junit.jupiter.api.Test
class ResourceUserTest {
private val r1 = Resource()
private val r2 = Resource()
private val user1 = ResourceUser(r1, r2)
private val user2 = ResourceUser(r2, r1)
@Operation
fun user1Work() = user1.work()
@Operation
fun user2Work() = user2.work()
@Test
fun stressTest() {
StressOptions()
.iterations(100)
.invocationsPerIteration(50)
.threads(4)
.actorsPerThread(10)
.verify()
}
}
- Problem: Incorrect implementation of a bounded buffer leading to lost values
class BoundedBuffer(size: Int) {
private val buffer = Array<Int?>(size) {null}
private var tail = 0
private var head = 0
fun put(value: Int): Boolean {
if (buffer[head] == null) {
buffer[head] = value
head = (head + 1) % buffer.size
return true
}
return false
}
fun take(): Int? {
val value = buffer[tail]
if (value != null) {
buffer[tail] = null
tail = (tail + 1) % buffer.size
}
return value
}
}
Lincheck test to expose the issue:
import org.jetbrains.kotlinx.lincheck.check
import org.jetbrains.kotlinx.lincheck.annotations.Operation
import org.jetbrains.kotlinx.lincheck.strategy.stress.StressOptions
import org.junit.jupiter.api.Test
class BoundedBufferTest {
private val buffer = BoundedBuffer(5)
@Operation
fun put(value: Int) = buffer.put(value)
@Operation
fun take(): Int? = buffer.take()
@Test
fun stressTest() {
StressOptions()
.iterations(100)
.invocationsPerIteration(50)
.threads(4)
.actorsPerThread(10)
.verify()
}
}
These Lincheck tests will expose the concurrent problems in the example code. To fix these issues, synchronization mechanisms like locks or atomic operations should be used.