1
1
/*
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.
3
3
*/
4
4
5
5
package kotlinx.coroutines.internal
@@ -19,9 +19,9 @@ import kotlin.test.*
19
19
class LockFreeLinkedListAtomicLFStressTest {
20
20
private val env = LockFreedomTestEnvironment (" LockFreeLinkedListAtomicLFStressTest" )
21
21
22
- data class IntNode (val i : Int ) : LockFreeLinkedListNode()
22
+ private data class Node (val i : Long ) : LockFreeLinkedListNode()
23
23
24
- private val TEST_DURATION_SEC = 5 * stressTestMultiplier
24
+ private val nSeconds = 5 * stressTestMultiplier
25
25
26
26
private val nLists = 4
27
27
private val nAdderThreads = 4
@@ -32,7 +32,8 @@ class LockFreeLinkedListAtomicLFStressTest {
32
32
private val undone = AtomicLong ()
33
33
private val missed = AtomicLong ()
34
34
private val removed = AtomicLong ()
35
- val error = AtomicReference <Throwable >()
35
+ private val error = AtomicReference <Throwable >()
36
+ private val index = AtomicLong ()
36
37
37
38
@Test
38
39
fun testStress () {
@@ -42,23 +43,23 @@ class LockFreeLinkedListAtomicLFStressTest {
42
43
when (rnd.nextInt(4 )) {
43
44
0 -> {
44
45
val list = lists[rnd.nextInt(nLists)]
45
- val node = IntNode (threadId )
46
+ val node = Node (index.incrementAndGet() )
46
47
addLastOp(list, node)
47
48
randomSpinWaitIntermission()
48
49
tryRemoveOp(node)
49
50
}
50
51
1 -> {
51
52
// just to test conditional add
52
53
val list = lists[rnd.nextInt(nLists)]
53
- val node = IntNode (threadId )
54
+ val node = Node (index.incrementAndGet() )
54
55
addLastIfTrueOp(list, node)
55
56
randomSpinWaitIntermission()
56
57
tryRemoveOp(node)
57
58
}
58
59
2 -> {
59
60
// just to test failed conditional add and burn some time
60
61
val list = lists[rnd.nextInt(nLists)]
61
- val node = IntNode (threadId )
62
+ val node = Node (index.incrementAndGet() )
62
63
addLastIfFalseOp(list, node)
63
64
}
64
65
3 -> {
@@ -68,8 +69,8 @@ class LockFreeLinkedListAtomicLFStressTest {
68
69
check(idx1 < idx2) // that is our global order
69
70
val list1 = lists[idx1]
70
71
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() )
73
74
addTwoOp(list1, node1, list2, node2)
74
75
randomSpinWaitIntermission()
75
76
tryRemoveOp(node1)
@@ -91,13 +92,13 @@ class LockFreeLinkedListAtomicLFStressTest {
91
92
removeTwoOp(list1, list2)
92
93
}
93
94
}
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" )
101
102
}
102
103
error.get()?.let { throw it }
103
104
assertEquals(missed.get(), removed.get())
@@ -106,19 +107,19 @@ class LockFreeLinkedListAtomicLFStressTest {
106
107
lists.forEach { it.validate() }
107
108
}
108
109
109
- private fun addLastOp (list : LockFreeLinkedListHead , node : IntNode ) {
110
+ private fun addLastOp (list : LockFreeLinkedListHead , node : Node ) {
110
111
list.addLast(node)
111
112
}
112
113
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 })
115
116
}
116
117
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 })
119
120
}
120
121
121
- private fun addTwoOp (list1 : LockFreeLinkedListHead , node1 : IntNode , list2 : LockFreeLinkedListHead , node2 : IntNode ) {
122
+ private fun addTwoOp (list1 : LockFreeLinkedListHead , node1 : Node , list2 : LockFreeLinkedListHead , node2 : Node ) {
122
123
val add1 = list1.describeAddLast(node1)
123
124
val add2 = list2.describeAddLast(node2)
124
125
val op = object : AtomicOp <Any ?>() {
@@ -138,7 +139,7 @@ class LockFreeLinkedListAtomicLFStressTest {
138
139
assertTrue(op.perform(null ) == null )
139
140
}
140
141
141
- private fun tryRemoveOp (node : IntNode ) {
142
+ private fun tryRemoveOp (node : Node ) {
142
143
if (node.remove())
143
144
undone.incrementAndGet()
144
145
else
@@ -165,5 +166,4 @@ class LockFreeLinkedListAtomicLFStressTest {
165
166
val success = op.perform(null ) == null
166
167
if (success) removed.addAndGet(2 )
167
168
}
168
-
169
169
}
0 commit comments