11/*
2- * Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
2+ * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
33 */
44
55package kotlinx.coroutines.internal
@@ -19,9 +19,9 @@ import kotlin.test.*
1919class LockFreeLinkedListAtomicLFStressTest {
2020 private val env = LockFreedomTestEnvironment (" LockFreeLinkedListAtomicLFStressTest" )
2121
22- data class IntNode (val i : Int ) : LockFreeLinkedListNode()
22+ private data class Node (val i : Long ) : LockFreeLinkedListNode()
2323
24- private val TEST_DURATION_SEC = 5 * stressTestMultiplier
24+ private val nSeconds = 5 * stressTestMultiplier
2525
2626 private val nLists = 4
2727 private val nAdderThreads = 4
@@ -32,7 +32,8 @@ class LockFreeLinkedListAtomicLFStressTest {
3232 private val undone = AtomicLong ()
3333 private val missed = AtomicLong ()
3434 private val removed = AtomicLong ()
35- val error = AtomicReference <Throwable >()
35+ private val error = AtomicReference <Throwable >()
36+ private val index = AtomicLong ()
3637
3738 @Test
3839 fun testStress () {
@@ -42,23 +43,23 @@ class LockFreeLinkedListAtomicLFStressTest {
4243 when (rnd.nextInt(4 )) {
4344 0 -> {
4445 val list = lists[rnd.nextInt(nLists)]
45- val node = IntNode (threadId )
46+ val node = Node (index.incrementAndGet() )
4647 addLastOp(list, node)
4748 randomSpinWaitIntermission()
4849 tryRemoveOp(node)
4950 }
5051 1 -> {
5152 // just to test conditional add
5253 val list = lists[rnd.nextInt(nLists)]
53- val node = IntNode (threadId )
54+ val node = Node (index.incrementAndGet() )
5455 addLastIfTrueOp(list, node)
5556 randomSpinWaitIntermission()
5657 tryRemoveOp(node)
5758 }
5859 2 -> {
5960 // just to test failed conditional add and burn some time
6061 val list = lists[rnd.nextInt(nLists)]
61- val node = IntNode (threadId )
62+ val node = Node (index.incrementAndGet() )
6263 addLastIfFalseOp(list, node)
6364 }
6465 3 -> {
@@ -68,8 +69,8 @@ class LockFreeLinkedListAtomicLFStressTest {
6869 check(idx1 < idx2) // that is our global order
6970 val list1 = lists[idx1]
7071 val list2 = lists[idx2]
71- val node1 = IntNode (threadId )
72- val node2 = IntNode ( - threadId - 1 )
72+ val node1 = Node (index.incrementAndGet() )
73+ val node2 = Node (index.incrementAndGet() )
7374 addTwoOp(list1, node1, list2, node2)
7475 randomSpinWaitIntermission()
7576 tryRemoveOp(node1)
@@ -91,13 +92,13 @@ class LockFreeLinkedListAtomicLFStressTest {
9192 removeTwoOp(list1, list2)
9293 }
9394 }
94- env.performTest(TEST_DURATION_SEC ) {
95- val _undone = undone.get()
96- val _missed = missed.get()
97- val _removed = removed.get()
98- println (" Adders undone $_undone node additions" )
99- println (" Adders missed $_missed nodes" )
100- println (" Remover removed $_removed nodes" )
95+ env.performTest(nSeconds ) {
96+ val undone = undone.get()
97+ val missed = missed.get()
98+ val removed = removed.get()
99+ println (" Adders undone $undone node additions" )
100+ println (" Adders missed $missed nodes" )
101+ println (" Remover removed $removed nodes" )
101102 }
102103 error.get()?.let { throw it }
103104 assertEquals(missed.get(), removed.get())
@@ -106,19 +107,19 @@ class LockFreeLinkedListAtomicLFStressTest {
106107 lists.forEach { it.validate() }
107108 }
108109
109- private fun addLastOp (list : LockFreeLinkedListHead , node : IntNode ) {
110+ private fun addLastOp (list : LockFreeLinkedListHead , node : Node ) {
110111 list.addLast(node)
111112 }
112113
113- private fun addLastIfTrueOp (list : LockFreeLinkedListHead , node : IntNode ) {
114- assertTrue(list.addLastIf(node, { true }) )
114+ private fun addLastIfTrueOp (list : LockFreeLinkedListHead , node : Node ) {
115+ assertTrue(list.addLastIf(node) { true })
115116 }
116117
117- private fun addLastIfFalseOp (list : LockFreeLinkedListHead , node : IntNode ) {
118- assertFalse(list.addLastIf(node, { false }) )
118+ private fun addLastIfFalseOp (list : LockFreeLinkedListHead , node : Node ) {
119+ assertFalse(list.addLastIf(node) { false })
119120 }
120121
121- private fun addTwoOp (list1 : LockFreeLinkedListHead , node1 : IntNode , list2 : LockFreeLinkedListHead , node2 : IntNode ) {
122+ private fun addTwoOp (list1 : LockFreeLinkedListHead , node1 : Node , list2 : LockFreeLinkedListHead , node2 : Node ) {
122123 val add1 = list1.describeAddLast(node1)
123124 val add2 = list2.describeAddLast(node2)
124125 val op = object : AtomicOp <Any ?>() {
@@ -138,7 +139,7 @@ class LockFreeLinkedListAtomicLFStressTest {
138139 assertTrue(op.perform(null ) == null )
139140 }
140141
141- private fun tryRemoveOp (node : IntNode ) {
142+ private fun tryRemoveOp (node : Node ) {
142143 if (node.remove())
143144 undone.incrementAndGet()
144145 else
@@ -165,5 +166,4 @@ class LockFreeLinkedListAtomicLFStressTest {
165166 val success = op.perform(null ) == null
166167 if (success) removed.addAndGet(2 )
167168 }
168-
169169}
0 commit comments