forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
quarkus grpc - multiple server instances
fixes quarkusio#10057
- Loading branch information
1 parent
12bcdca
commit 7bee03e
Showing
9 changed files
with
284 additions
and
112 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
...nsions/grpc/deployment/src/test/java/io/quarkus/grpc/server/scaling/BasicScalingTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package io.quarkus.grpc.server.scaling; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.Future; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.inject.Inject; | ||
|
||
import io.grpc.examples.helloworld.GreeterGrpc; | ||
import io.grpc.examples.helloworld.HelloRequest; | ||
import io.quarkus.grpc.runtime.annotations.GrpcService; | ||
|
||
public class BasicScalingTest { | ||
|
||
@Inject | ||
GreeterConsumer greetingConsumer; | ||
|
||
Set<String> getThreadsUsedForRequests(int requestNo) throws InterruptedException, ExecutionException, TimeoutException { | ||
List<Callable<String>> calls = new ArrayList<>(); | ||
for (int i = 0; i < requestNo; i++) { | ||
calls.add(() -> { | ||
System.out.println("invoking on " + Thread.currentThread()); // mstodo remove | ||
return greetingConsumer.invoke("foo"); | ||
}); | ||
} | ||
List<Future<String>> results = Executors.newFixedThreadPool(requestNo) | ||
.invokeAll(calls); | ||
|
||
Set<String> threads = new HashSet<>(); | ||
for (Future<String> result : results) { | ||
threads.add(result.get(10, TimeUnit.SECONDS)); | ||
} | ||
return threads; | ||
} | ||
|
||
@ApplicationScoped | ||
static class GreeterConsumer { | ||
|
||
@Inject | ||
@GrpcService("hello-service") | ||
GreeterGrpc.GreeterBlockingStub service; | ||
|
||
public String invoke(String s) { | ||
return service.sayHello(HelloRequest.newBuilder().setName(s).build()) | ||
.getMessage(); | ||
} | ||
|
||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...pc/deployment/src/test/java/io/quarkus/grpc/server/scaling/MultipleGrpcVerticlesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package io.quarkus.grpc.server.scaling; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.Set; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.grpc.examples.helloworld.GreeterGrpc; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class MultipleGrpcVerticlesTest extends BasicScalingTest { | ||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest().setArchiveProducer( | ||
() -> ShrinkWrap.create(JavaArchive.class) | ||
.addPackage(GreeterGrpc.class.getPackage()) | ||
.addClass(ThreadReturningGreeterService.class)) | ||
.withConfigurationResource("multiple-instances-config.properties"); | ||
|
||
@Test | ||
public void shouldUseMultipleThreads() throws InterruptedException, TimeoutException, ExecutionException { | ||
Set<String> threads = getThreadsUsedForRequests(20); | ||
|
||
assertThat(threads).hasSizeGreaterThan(1); | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
.../grpc/deployment/src/test/java/io/quarkus/grpc/server/scaling/SingleGrpcVerticleTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package io.quarkus.grpc.server.scaling; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.Set; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.grpc.examples.helloworld.GreeterGrpc; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class SingleGrpcVerticleTest extends BasicScalingTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest().setArchiveProducer( | ||
() -> ShrinkWrap.create(JavaArchive.class) | ||
.addPackage(GreeterGrpc.class.getPackage()) | ||
.addClass(ThreadReturningGreeterService.class)) | ||
.withConfigurationResource("single-instance-config.properties"); | ||
|
||
@Test | ||
public void shouldUseMultipleThreads() throws InterruptedException, TimeoutException, ExecutionException { | ||
Set<String> threads = getThreadsUsedForRequests(20); | ||
|
||
assertThat(threads).hasSize(1); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...eployment/src/test/java/io/quarkus/grpc/server/scaling/ThreadReturningGreeterService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package io.quarkus.grpc.server.scaling; | ||
|
||
import javax.inject.Singleton; | ||
|
||
import io.grpc.examples.helloworld.GreeterGrpc; | ||
import io.grpc.examples.helloworld.HelloReply; | ||
import io.grpc.examples.helloworld.HelloRequest; | ||
import io.grpc.stub.StreamObserver; | ||
|
||
@Singleton | ||
public class ThreadReturningGreeterService extends GreeterGrpc.GreeterImplBase { | ||
|
||
@Override | ||
public void sayHello(HelloRequest request, StreamObserver<HelloReply> responseObserver) { | ||
try { | ||
Thread.sleep(100); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
responseObserver.onNext(HelloReply.newBuilder().setMessage(Thread.currentThread().getName()).build()); | ||
responseObserver.onCompleted(); | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
extensions/grpc/deployment/src/test/resources/multiple-instances-config.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
quarkus.grpc.server.instances=20 | ||
quarkus.grpc.clients.hello-service.host=localhost | ||
quarkus.grpc.clients.hello-service.keep-alive-timeout=1s |
3 changes: 3 additions & 0 deletions
3
extensions/grpc/deployment/src/test/resources/single-instance-config.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
quarkus.grpc.server.instances=1 | ||
quarkus.grpc.clients.hello-service.host=localhost | ||
quarkus.grpc.clients.hello-service.keep-alive-timeout=1s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.