GithubHelp home page GithubHelp logo

asarkar / grpc-test Goto Github PK

View Code? Open in Web Editor NEW
27.0 5.0 1.0 90 KB

Includes a JUnit 5 Extension that can automatically release gRPC resources at the end of the test

License: Apache License 2.0

Kotlin 100.00%
grpc-test grpc grpc-java protobuf protobuf-java testing junit5 junit junit5-extension junit-extension

grpc-test's People

Contributors

asarkar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

saadlu

grpc-test's Issues

Question: Warning when using the library

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.

Parameter injection of Resources does not clean up

Hi Again,

I am trying out parameter injection of Resources.

See https://github.com/saadlu/helloworld-grpc-test/blob/main/src/test/java/net/saad/learning/grpc/hello/HelloWorldServerJ5ParameterInjectionTest.java

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

https://github.com/asarkar/grpc-test/blob/master/src/test/kotlin/com/asarkar/grpc/test/GrpcCleanupExtensionIntegrationTests.kt#L22

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?

IllegalArgumentException when used with @Nested and inner classes

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

Extensions have to be stateless

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.