Is there any way to control how many of a type of test run at the same time? #2185
-
We have so many integration tests that use the database that MySQL is rejecting connections. I've changed Hikari to only have a "pool" of one connection which should help. Each test was creating a pool of 10. But I was wondering if there's way to control spec/feature eligibility to be scheduled? Instead of an |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
You could configure the parallelism overall. In situations like you described I usually recommend exactly that. If you want to limit only specific tests or have several independent pools, make an annotation-driven local Spock extension so that only annotated tests are affected and you could configure a pool identifier and have one semaphore per pool |
Beta Was this translation helpful? Give feedback.
-
Let me see if I'm understanding
Something like: class SemaphoreMethodInterceptor implements IMethodInterceptor {
private static final Semaphore SEMAPHORE = new Semaphore(10)
@Override
void intercept(IMethodInvocation invocation) throws Throwable {
SEMAPHORE.acquire()
try {
invocation.proceed()
} finally {
SEMAPHORE.release()
}
}
}
class SemaphoreGlobalExtension implements IGlobalExtension {
private static final SemaphoreMethodInterceptor INTERCEPTOR = new SemaphoreMethodInterceptor()
@Override
void visitSpec(SpecInfo spec) {
// Skip specs marked as isolated as that is more restrictive
if (spec.isAnnotationPresent(Isolated)) {
return
}
boolean isRepoSpec = BaseRepoIntegrationSpec.isAssignableFrom(spec.reflection)
if (!isRepoSpec) {
return
}
spec.features.each { feature ->
feature.featureMethod.addInterceptor(INTERCEPTOR)
}
}
} |
Beta Was this translation helpful? Give feedback.
You could configure the parallelism overall.
That will be effective for all tests though and still more tests can run in parallel due to work-stealing.
In situations like you described I usually recommend exactly that.
If you want the limit for all tests, have a global Spock extension that creates a
Semaphore
with 10 permits, then acquire it in an according interceptor before callingproceed()
and release it in afinally
block.If you want to limit only specific tests or have several independent pools, make an annotation-driven local Spock extension so that only annotated tests are affected and you could configure a pool identifier and have one semaphore per pool