Skip to content

Commit

Permalink
add custom threads data structures representation tests
Browse files Browse the repository at this point in the history
Signed-off-by: Evgeniy Moiseenko <[email protected]>
  • Loading branch information
eupp committed Dec 11, 2024
1 parent 7a743c2 commit 469be4f
Show file tree
Hide file tree
Showing 5 changed files with 421 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import kotlin.concurrent.thread
import kotlin.reflect.KClass
import kotlin.reflect.KFunction
import org.junit.Test
import java.util.concurrent.ConcurrentLinkedDeque

class CustomThreadsRepresentationTest {

Expand Down Expand Up @@ -104,6 +105,55 @@ class CustomThreadsRepresentationTest {
outputFileName = if (isJdk8) "custom_threads_livelock_trace_jdk8.txt" else "custom_threads_livelock_trace.txt",
)

fun incorrectConcurrentLinkedDeque() {
val deque = ConcurrentLinkedDeque<Int>()
var r1: Int = -1
var r2: Int = -1
deque.addLast(1)
val t1 = Thread {
r1 = deque.pollFirst()
}
val t2 = Thread {
deque.addFirst(0)
r2 = deque.peekLast()
}
val threads = listOf(t1, t2)
threads.forEach { it.start() }
threads.forEach { it.join() }
check(!(r1 == 1 && r2 == 1))
}

@Test(timeout = TIMEOUT)
fun incorrectConcurrentLinkedDequeTest() = modelCheckerTraceTest(
testClass = this::class,
testOperation = this::incorrectConcurrentLinkedDeque,
invocations = 1_000,
outputFileName = if (isJdk8) "custom_threads_deque_trace_jdk8.txt" else "custom_threads_deque_trace.txt",
)

fun incorrectHashMap() {
val hashMap = HashMap<Int, Int>()
var r1: Int? = null
var r2: Int? = null
val t1 = thread {
r1 = hashMap.put(0, 1)
}
val t2 = thread {
r2 = hashMap.put(0, 1)
}
t1.join()
t2.join()
check(!(r1 == null && r2 == null))
}

@Test(timeout = TIMEOUT)
fun incorrectHashMapTest() = modelCheckerTraceTest(
testClass = this::class,
testOperation = this::incorrectHashMap,
invocations = 1_000,
outputFileName = if (isJdk8) "custom_threads_hashmap_trace_jdk8.txt" else "custom_threads_hashmap_trace.txt",
)

@Suppress("DEPRECATION") // Unsafe
companion object {
val unsafe =
Expand Down
Loading

0 comments on commit 469be4f

Please sign in to comment.