-
Notifications
You must be signed in to change notification settings - Fork 331
Expand file tree
/
Copy pathThreadUtilsTest.groovy
More file actions
96 lines (82 loc) · 2.46 KB
/
ThreadUtilsTest.groovy
File metadata and controls
96 lines (82 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package utils
import datadog.trace.agent.test.InstrumentationSpecification
import datadog.trace.config.inversion.ConfigHelper
import datadog.trace.test.util.ThreadUtils
import org.spockframework.runtime.ConditionNotSatisfiedError
import spock.lang.FailsWith
import spock.lang.Shared
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.atomic.AtomicInteger
class ThreadUtilsTest extends InstrumentationSpecification {
@Override
protected void configurePreAgent() {
super.configurePreAgent()
// Opt out of strict config validation - test module loads test instrumentations with fake names
ConfigHelper.get().setConfigInversionStrict(ConfigHelper.StrictnessPolicy.TEST)
}
@Shared
def counter = new AtomicInteger()
@Shared
def byThread = new ConcurrentHashMap<String, Integer>()
def cleanup() {
counter.set(0)
}
def "run a closure concurrently"() {
expect:
ThreadUtils.runConcurrently(10, 200, {
def total = counter.incrementAndGet()
def name = Thread.currentThread().getName()
def mine = byThread.get(name, 0) + 1
byThread.put(name, mine)
assert total >= mine
})
assert counter.get() == 200
assert byThread.size() == 10
byThread.each {
assert it.value == 20
}
}
@FailsWith(value = ConditionNotSatisfiedError)
def "assert should fail test"() {
expect:
ThreadUtils.runConcurrently(10, 200, {
def total = counter.incrementAndGet()
assert total <= 17
})
// we should never get here
assert "we should" == "never get here"
}
@FailsWith(value = ConditionNotSatisfiedError)
def "assert as last operation should fail test"() {
expect:
ThreadUtils.runConcurrently(10, 200, {
def total = counter.incrementAndGet()
if (total == 200) {
Thread.sleep(500)
}
assert total < 200
})
// we should never get here
assert "we should" == "never get here"
}
def "concurrency 1 should run on this thread"() {
when:
def thread = Thread.currentThread()
then:
ThreadUtils.runConcurrently(1, 200, {
counter.incrementAndGet()
assert Thread.currentThread() == thread
})
assert counter.get() == 200
}
def "totalIterations 1 should run on this thread"() {
when:
def thread = Thread.currentThread()
then:
ThreadUtils.runConcurrently(10, 1, {
counter.incrementAndGet()
assert Thread.currentThread() == thread
})
assert counter.get() == 1
}
}