I would like to surround a test task with a resource monitor. For readers familiar with AspectJ, I am attempting to do something similar to what the AspectJ @Around annotation does below:
@Around("execution(void aspects.TestAspect.myTest())")
public void aroundMyTest(ProceedingJoinPoint joinPoint) throws Throwable {
System.err.println("Running monitor before test" + joinPoint);
monitor();
joinPoint.proceed();
monitor();
System.err.println("Ran monitor after test " + joinPoint);
}
This can be done rather straightforwardly in Gradle with something like:
test.dependsOn monitorTask
test.finalizedBy monitorTask
Unfortunately Gradle considers this to be a circular dependency.
Circular dependency between the following tasks:
:monitorTask
\--- :test
\--- :monitorTask (*)
To get around this, I have duplicated my monitorTask and I now have code that looks like:
test.dependsOn monitorTask
test.finalizedBy monitorTask2
The goal I am trying to achieve is that the monitor code runs immediately before a test and immediately after. This might also be possible with:
test.doFirst { monitorTask.execute() }
test.doLast { monitorTask.execute() }
Execution of of tasks in that manner is however is not allowed.
The monitor code I am trying to run collects statistics about the service I am running the test against for later comparative analysis (did the heap grow, how much CPU was used, …). For this reason, I would like my monitor to run immediately before a test runs, and then immediately after.
Is there a better way to implement this than what I have already done?
task monitorTask(type: JavaExec) {
main = "com.monitor.Monitor"
classpath sourceSets.main.runtimeClasspath
}
task monitorTask2(type: JavaExec) {
main = "com.monitor.Monitor"
classpath sourceSets.main.runtimeClasspath
}
test.dependsOn monitorTask
test.finalizedBy monitorTask2