asarkar / grpc-test Goto Github PK
View Code? Open in Web Editor NEWIncludes a JUnit 5 Extension that can automatically release gRPC resources at the end of the test
License: Apache License 2.0
Includes a JUnit 5 Extension that can automatically release gRPC resources at the end of the test
License: Apache License 2.0
Get a few warnings when compiling the Java test that depends on this library
com.asarkar.grpc\grpc-test\1.2.1\f551cc51d2cc1a49e03fabbde9333e075e9ad2f\grpc-test-1.2.1.jar(/com/asarkar/grpc/test/GrpcCleanupExtension.class): warning: Cannot find annotation method 'mv()' in type 'Metadata': class file for kotlin.Metadata not found
com.asarkar.grpc\grpc-test\1.2.1\f551cc51d2cc1a49e03fabbde9333e075e9ad2f\grpc-test-1.2.1.jar(/com/asarkar/grpc/test/GrpcCleanupExtension.class): warning: Cannot find annotation method 'k()' in type 'Metadata'
com.asarkar.grpc\grpc-test\1.2.1\f551cc51d2cc1a49e03fabbde9333e075e9ad2f\grpc-test-1.2.1.jar(/com/asarkar/grpc/test/GrpcCleanupExtension.class): warning: Cannot find annotation method 'xi()' in type 'Metadata'
com.asarkar.grpc\grpc-test\1.2.1\f551cc51d2cc1a49e03fabbde9333e075e9ad2f\grpc-test-1.2.1.jar(/com/asarkar/grpc/test/GrpcCleanupExtension.class): warning: Cannot find annotation method 'd1()' in type 'Metadata'
com.asarkar.grpc\grpc-test\1.2.1\f551cc51d2cc1a49e03fabbde9333e075e9ad2f\grpc-test-1.2.1.jar(/com/asarkar/grpc/test/GrpcCleanupExtension.class): warning: Cannot find annotation method 'd2()' in type 'Metadata'
com.asarkar.grpc\grpc-test\1.2.1\f551cc51d2cc1a49e03fabbde9333e075e9ad2f\grpc-test-1.2.1.jar(/com/asarkar/grpc/test/Resources.class): warning: Cannot find annotation method 'mv()' in type 'Metadata'
com.asarkar.grpc\grpc-test\1.2.1\f551cc51d2cc1a49e03fabbde9333e075e9ad2f\grpc-test-1.2.1.jar(/com/asarkar/grpc/test/Resources.class): warning: Cannot find annotation method 'k()' in type 'Metadata'
com.asarkar.grpc\grpc-test\1.2.1\f551cc51d2cc1a49e03fabbde9333e075e9ad2f\grpc-test-1.2.1.jar(/com/asarkar/grpc/test/Resources.class): warning: Cannot find annotation method 'xi()' in type 'Metadata'
com.asarkar.grpc\grpc-test\1.2.1\f551cc51d2cc1a49e03fabbde9333e075e9ad2f\grpc-test-1.2.1.jar(/com/asarkar/grpc/test/Resources.class): warning: Cannot find annotation method 'd1()' in type 'Metadata'
com.asarkar.grpc\grpc-test\1.2.1\f551cc51d2cc1a49e03fabbde9333e075e9ad2f\grpc-test-1.2.1.jar(/com/asarkar/grpc/test/Resources.class): warning: Cannot find annotation method 'd2()' in type 'Metadata'
Since not a kotlin dev, not really sure what this is about.
Hi Again,
I am trying out parameter injection of Resources
.
I put a breakpoint at Resources::clean (https://github.com/asarkar/grpc-test/blob/master/src/main/kotlin/com/asarkar/grpc/test/Resources.kt#L65), and it does not hit when I run the above test in debug mode.
For instance field of Resources the breakpoint does hit. So test https://github.com/saadlu/helloworld-grpc-test/blob/main/src/test/java/net/saad/learning/grpc/hello/HelloWorldServerJ5Test.java does clean up.
I have also trying putting breakpoints in io.grpc.internal.ServerImpl#shutdown
and it does not hit when parameter injection is used but does hit when instance field of Resources is used.
I am not sure if I am doing something wrong.
But I did ran your test
and it fails for me, too.
Wanted but not invoked:
server.shutdown();
-> at com.asarkar.grpc.test.GrpcCleanupExtensionIntegrationTests.testSuccessful(GrpcCleanupExtensionIntegrationTests.kt:45)
Actually, there were zero interactions with this mock.
Wanted but not invoked:
server.shutdown();
-> at com.asarkar.grpc.test.GrpcCleanupExtensionIntegrationTests.testSuccessful(GrpcCleanupExtensionIntegrationTests.kt:45)
Actually, there were zero interactions with this mock.
any advice?
Just wondering.
Hi, thanks for building this library - it's really useful!
I use nested classes to organise my unit tests (nested class per service method) but when I try to use them with this extension I get the exception below.
@ExtendWith(GrpcCleanupExtension::class)
class ServiceTest {
private val service = Service()
private lateinit var resources: Resources
private lateinit var stub: ServiceGrpcKt.ServiceCoroutineStub
@BeforeEach
fun setup() {
val server = InProcessServerBuilder.forName("test").directExecutor().addService(service).build().start()
val channel = InProcessChannelBuilder.forName("test").build()
resources.register(server)
resources.register(channel)
stub = ServiceGrpcKt.ServiceCoroutineStub(channel)
}
@Nested
inner class ExampleNestedClass {
@Test
fun `example test`() = runBlocking {
stub.example(request)
}
}
}
Can not set com.asarkar.grpc.test.Resources field com.example.ServiceTest.resources to com.example.ServiceTest$Get
java.lang.IllegalArgumentException: Can not set com.asarkar.grpc.test.Resources field com.example.ServiceTest.resources to com.example.ServiceTest$Get
at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58)
at java.base/jdk.internal.reflect.UnsafeObjectFieldAccessorImpl.get(UnsafeObjectFieldAccessorImpl.java:36)
at java.base/java.lang.reflect.Field.get(Field.java:416)
at com.asarkar.grpc.test.ExtensionContextUtilsKt.getResourcesInstance(ExtensionContextUtils.kt:46)
at com.asarkar.grpc.test.GrpcCleanupExtension.beforeEach(GrpcCleanupExtension.kt:35)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeBeforeEachCallbacks$1(TestMethodTestDescriptor.java:159)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeBeforeMethodsOrCallbacksUntilExceptionOccurs$5(TestMethodTestDescriptor.java:195)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeBeforeMethodsOrCallbacksUntilExceptionOccurs(TestMethodTestDescriptor.java:195)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeBeforeEachCallbacks(TestMethodTestDescriptor.java:158)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:125)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:65)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:129)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:127)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1510)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:143)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:129)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:127)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1510)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:143)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:129)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:127)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1510)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:143)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:129)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:127)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:108)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:88)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:54)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:67)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:52)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:96)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:75)
at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor$CollectAllTestClassesExecutor.processAllTestClasses(JUnitPlatformTestClassProcessor.java:99)
at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor$CollectAllTestClassesExecutor.access$000(JUnitPlatformTestClassProcessor.java:79)
at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor.stop(JUnitPlatformTestClassProcessor.java:75)
at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.stop(SuiteTestClassProcessor.java:61)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:33)
at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:94)
at com.sun.proxy.$Proxy5.stop(Unknown Source)
at org.gradle.api.internal.tasks.testing.worker.TestWorker.stop(TestWorker.java:133)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:182)
at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:164)
at org.gradle.internal.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:414)
at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:64)
at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:48)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:56)
at java.base/java.lang.Thread.run(Thread.java:832)
Suppressed: java.lang.IllegalArgumentException: Can not set com.asarkar.grpc.test.Resources field dev.r36.mercury.audit.grpc.AuditRecordServiceImplTest.resources to dev.r36.mercury.audit.grpc.AuditRecordServiceImplTest$Get
at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58)
at java.base/jdk.internal.reflect.UnsafeObjectFieldAccessorImpl.get(UnsafeObjectFieldAccessorImpl.java:36)
at java.base/java.lang.reflect.Field.get(Field.java:416)
at com.asarkar.grpc.test.ExtensionContextUtilsKt.getResourcesInstance(ExtensionContextUtils.kt:46)
at com.asarkar.grpc.test.GrpcCleanupExtension.afterEach(GrpcCleanupExtension.kt:52)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeAfterEachCallbacks$11(TestMethodTestDescriptor.java:253)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeAllAfterMethodsOrCallbacks$12(TestMethodTestDescriptor.java:269)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeAllAfterMethodsOrCallbacks$13(TestMethodTestDescriptor.java:269)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1510)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeAllAfterMethodsOrCallbacks(TestMethodTestDescriptor.java:268)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeAfterEachCallbacks(TestMethodTestDescriptor.java:252)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:137)
... 78 more
From JUnit 5 Extension Model:
The engine makes no guarantees when it instantiates extensions and how long it keeps instances around. This has a number of reasons:
It is not clear when and how extensions should be instantiated. (For each test? For each class? For each run?)
Jupiter does not want to bother tracking extension instances.
If extensions were to communicate with one another, a mechanism for exchanging data would be required anyways.
Hence, extensions have to be stateless. Any state they need to maintain has to be written to and loaded from the store that the extension context makes available.
Shouldn't reinitialize Resources
parameter for beforeAll
or for static method.
If a static or instance field is initialized by the user, the extension shouldn't reinitialize it.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.