Skip to content

Commit

Permalink
change the format of data structure tests to avoid the problem with i…
Browse files Browse the repository at this point in the history
…ncorrect local object tracking

Signed-off-by: Evgeniy Moiseenko <[email protected]>
  • Loading branch information
eupp committed Nov 26, 2024
1 parent 02c04f1 commit 250236c
Showing 1 changed file with 27 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ import org.junit.Test

class DataStructuresTests {

private val deque = ConcurrentLinkedDeque<Int>()

fun incorrectConcurrentLinkedDeque() {
val deque = ConcurrentLinkedDeque<Int>()
// val deque = ConcurrentLinkedDeque<Int>()
var r1: Int = -1
var r2: Int = -1
deque.addLast(1)
Expand All @@ -45,32 +47,34 @@ class DataStructuresTests {
invocations = 1_000,
)

/* This test does not pass currently because the local object tracker
* does not handle properly the situation when an object escapes its thread
* when passed to a new thread via lambda capturing.
*
* TODO: fix local object tracker to handle this new case correctly
*/
private val hashMap = HashMap<Int, Int>()

fun incorrectHashMap() {
val hashMap = HashMap<Int, Int>() // <- this object is incorrectly classified as a local object
/* If we replace the `hashMap` field with method-local variable, the test would fail.
* This is because the local object tracker currently does not handle properly
* the situation when an object escapes its thread
* by being passed to a new thread via lambda capturing.
*
* TODO: fix local object tracker to handle this new case correctly
*/
// val hashMap = HashMap<Int, Int>() // <- this object is incorrectly classified as a local object
var r1: Int? = null
var r2: Int? = null
val t1 = thread {
// the local object tracker does not detect here
// that the `hashMap` object escapes into another thread
// the local object tracker does not detect here that the `hashMap` object,
// stored in the local variable, escapes into another thread
r1 = hashMap.put(0, 1)
}
val t2 = thread {
// the local object tracker does not detect here
// that the `hashMap` object escapes into another thread
// the local object tracker does not detect here that the `hashMap` object,
// stored in the local variable, escapes into another thread
r2 = hashMap.put(0, 1)
}
t1.join()
t2.join()
check(!(r1 == null && r2 == null))
}

@Ignore
@Test(timeout = TIMEOUT)
fun incorrectHashMapTest() = modelCheckerTest(
testClass = this::class,
Expand All @@ -79,15 +83,17 @@ class DataStructuresTests {
invocations = 1_000,
)

private val concurrentHashMap = ConcurrentHashMap<Int, Int>()

fun correctConcurrentHashMap() {
val hashMap = ConcurrentHashMap<Int, Int>()
// val concurrentHashMap = ConcurrentHashMap<Int, Int>()
var r1: Int? = -1
var r2: Int? = -1
val t1 = thread {
r1 = hashMap.put(0, 1)
r1 = concurrentHashMap.put(0, 1)
}
val t2 = thread {
r2 = hashMap.put(0, 1)
r2 = concurrentHashMap.put(0, 1)
}
t1.join()
t2.join()
Expand All @@ -102,15 +108,17 @@ class DataStructuresTests {
invocations = 1_000,
)

private val concurrentSkipListMap = ConcurrentSkipListMap<Int, Int>()

fun correctConcurrentSkipListMap() {
val hashMap = ConcurrentSkipListMap<Int, Int>()
// val concurrentSkipListMap = ConcurrentSkipListMap<Int, Int>()
var r1: Int? = -1
var r2: Int? = -1
val t1 = thread {
r1 = hashMap.put(0, 1)
r1 = concurrentSkipListMap.put(0, 1)
}
val t2 = thread {
r2 = hashMap.put(0, 1)
r2 = concurrentSkipListMap.put(0, 1)
}
t1.join()
t2.join()
Expand Down

0 comments on commit 250236c

Please sign in to comment.