forked from spockframework/spock
/
ByteBuddyMockFactoryConcurrentSpec.groovy
146 lines (130 loc) · 4.85 KB
/
ByteBuddyMockFactoryConcurrentSpec.groovy
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* Copyright 2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.spockframework.mock.runtime
import groovy.transform.Canonical
import spock.lang.Isolated
import spock.lang.Shared
import spock.lang.Specification
import spock.lang.Timeout
import spock.lang.Unroll
import java.util.concurrent.CompletableFuture
import java.util.concurrent.Phaser
import java.util.concurrent.TimeUnit
import java.util.function.Function
// We need to run in isolation, because the ByteBuddyMockFactory is static and we interfere with the cache
// In the future we should change the ByteBuddyMockFactory to be an instance instead of static API.
@Isolated
class ByteBuddyMockFactoryConcurrentSpec extends Specification {
private static final String IfA = "IfA"
private static final String IfB = "IfB"
private static final String IfC = "IfC"
@Shared
final IProxyBasedMockInterceptor interceptor = Stub()
// Just to be save to abort, normally the tests run in 2 secs.
@Timeout(40)
@Unroll("#test")
def "cacheLockingStressTest"(String test, MockSpec mockSpecA, MockSpec mockSpecB) {
setup:
int iterations = 5_000
def tempClassLoader = new ByteBuddyTestClassLoader()
MockFeatures featA = toMockFeatures(mockSpecA, tempClassLoader)
MockFeatures featB = toMockFeatures(mockSpecB, tempClassLoader)
Phaser phaser = new Phaser(4)
Function<Runnable, CompletableFuture<Void>> runCode = { Runnable code ->
CompletableFuture.runAsync({
phaser.arriveAndAwaitAdvance()
try {
for (int i = 0; i < iterations; i++) {
code.run()
}
} finally {
phaser.arrive()
}
})
}
when:
def mockFeatAFuture = runCode.apply {
Class<?> mockClass = mockClass(tempClassLoader, featA)
assertValidMockClass(featA, mockClass, tempClassLoader)
}
def mockFeatBFuture = runCode.apply {
Class<?> mockClass = mockClass(tempClassLoader, featB)
assertValidMockClass(featB, mockClass, tempClassLoader)
}
def cacheFuture = runCode.apply { ByteBuddyMockFactory.clearAllCaches() }
phaser.arriveAndAwaitAdvance()
// Wait for test to end
int phase = phaser.arrive()
try {
phaser.awaitAdvanceInterruptibly(phase, 30, TimeUnit.SECONDS)
} finally {
// Collect exceptions from the futures, to make issues visible.
mockFeatAFuture.getNow(null)
mockFeatBFuture.getNow(null)
cacheFuture.getNow(null)
}
then:
noExceptionThrown()
where:
test | mockSpecA | mockSpecB
"same hashcode different mockType" | mockSpec(IfA, IfB) | mockSpec(IfB, IfA)
"same hashcode same mockType" | mockSpec(IfA) | mockSpec(IfA)
"different hashcode different interfaces" | mockSpec(IfA, IfB) | mockSpec(IfB, IfC)
"unrelated classes" | mockSpec(IfA) | mockSpec(IfB)
}
private Class<?> mockClass(ClassLoader cl, MockFeatures feature) {
return ByteBuddyMockFactory.createMock(
feature.mockType,
feature.interfaces,
null,
interceptor,
cl,
false)
.getClass()
}
private static MockSpec mockSpec(String mockedType, String... interfaces) {
return new MockSpec(mockedType, Arrays.asList(interfaces))
}
private void assertValidMockClass(MockFeatures mockFeature, Class<?> mockClass, ClassLoader classLoader) {
assert mockClass.classLoader == classLoader
assert mockFeature.mockType.isAssignableFrom(mockClass)
for (Class<?> anInterface : mockFeature.interfaces) {
assert anInterface.isAssignableFrom(mockClass)
}
}
MockFeatures toMockFeatures(MockSpec mockFeaturesString, ByteBuddyTestClassLoader classLoader) {
def mockType = classLoader.defineInterface(mockFeaturesString.mockType)
def interfaces = mockFeaturesString.interfaces.collect { classLoader.defineInterface(it) }
return new MockFeatures(mockType, interfaces)
}
/**
* Class holding the loaded classes to mock.
*/
@Canonical
private static class MockFeatures {
final Class<?> mockType
final List<Class<?>> interfaces
}
/**
* Class holding the class names to mock.
* Which will be converted into a {@link MockFeatures} during test.
*/
@Canonical
private static class MockSpec {
final String mockType
final List<String> interfaces
}
}