This is an automated email from the ASF dual-hosted git repository.
albumenj pushed a commit to branch 3.2
in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/3.2 by this push:
new 5d979c18f2 Refactor the test in the package org. Apache. Dubbo.
Reactive. (#13175)
5d979c18f2 is described below
commit 5d979c18f2cfaa1e0d8fb64a2cb06f4797a8e18c
Author: gzhao9 <[email protected]>
AuthorDate: Tue Oct 10 03:52:47 2023 -0400
Refactor the test in the package org. Apache. Dubbo. Reactive. (#13175)
---
...HandlerTest.java => CreateObserverAdapter.java} | 57 +++++++++++----------
.../reactive/ManyToManyMethodHandlerTest.java | 28 +++--------
.../dubbo/reactive/ManyToOneMethodHandlerTest.java | 58 ++++++++--------------
.../dubbo/reactive/OneToManyMethodHandlerTest.java | 47 ++++++------------
4 files changed, 72 insertions(+), 118 deletions(-)
diff --git
a/dubbo-plugin/dubbo-reactive/src/test/java/org/apache/dubbo/reactive/ManyToManyMethodHandlerTest.java
b/dubbo-plugin/dubbo-reactive/src/test/java/org/apache/dubbo/reactive/CreateObserverAdapter.java
similarity index 51%
copy from
dubbo-plugin/dubbo-reactive/src/test/java/org/apache/dubbo/reactive/ManyToManyMethodHandlerTest.java
copy to
dubbo-plugin/dubbo-reactive/src/test/java/org/apache/dubbo/reactive/CreateObserverAdapter.java
index b1c642967b..1f979efc78 100644
---
a/dubbo-plugin/dubbo-reactive/src/test/java/org/apache/dubbo/reactive/ManyToManyMethodHandlerTest.java
+++
b/dubbo-plugin/dubbo-reactive/src/test/java/org/apache/dubbo/reactive/CreateObserverAdapter.java
@@ -17,49 +17,52 @@
package org.apache.dubbo.reactive;
-import org.apache.dubbo.common.stream.StreamObserver;
-import org.apache.dubbo.reactive.handler.ManyToManyMethodHandler;
import org.apache.dubbo.rpc.protocol.tri.observer.ServerCallToObserverAdapter;
-import org.junit.jupiter.api.Assertions;
-import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
-import java.util.concurrent.CompletableFuture;
-import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicInteger;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doAnswer;
-/**
- * Unit test for ManyToManyMethodHandler
- */
-public final class ManyToManyMethodHandlerTest {
+public class CreateObserverAdapter {
+
+ private ServerCallToObserverAdapter<String> responseObserver;
+ private AtomicInteger nextCounter;
+ private AtomicInteger completeCounter;
+ private AtomicInteger errorCounter;
+
+ CreateObserverAdapter() {
+
+ nextCounter = new AtomicInteger();
+ completeCounter = new AtomicInteger();
+ errorCounter = new AtomicInteger();
- @Test
- void testInvoke() throws ExecutionException, InterruptedException {
- AtomicInteger nextCounter = new AtomicInteger();
- AtomicInteger completeCounter = new AtomicInteger();
- AtomicInteger errorCounter = new AtomicInteger();
- ServerCallToObserverAdapter<String> responseObserver =
Mockito.mock(ServerCallToObserverAdapter.class);
+ responseObserver = Mockito.mock(ServerCallToObserverAdapter.class);
doAnswer(o -> nextCounter.incrementAndGet())
.when(responseObserver).onNext(anyString());
doAnswer(o -> completeCounter.incrementAndGet())
.when(responseObserver).onCompleted();
doAnswer(o -> errorCounter.incrementAndGet())
.when(responseObserver).onError(any(Throwable.class));
- ManyToManyMethodHandler<String, String> handler = new
ManyToManyMethodHandler<>(requestFlux ->
- requestFlux.map(r -> r + "0"));
- CompletableFuture<StreamObserver<String>> future = handler.invoke(new
Object[]{responseObserver});
- StreamObserver<String> requestObserver = future.get();
- for (int i = 0; i < 10; i++) {
- requestObserver.onNext(String.valueOf(i));
- }
- requestObserver.onCompleted();
- Assertions.assertEquals(10, nextCounter.get());
- Assertions.assertEquals(0, errorCounter.get());
- Assertions.assertEquals(1, completeCounter.get());
+
+ }
+
+ public AtomicInteger getCompleteCounter() {
+ return completeCounter;
+ }
+
+ public AtomicInteger getNextCounter() {
+ return nextCounter;
+ }
+
+ public AtomicInteger getErrorCounter() {
+ return errorCounter;
+ }
+
+ public ServerCallToObserverAdapter<String> getResponseObserver() {
+ return this.responseObserver;
}
}
diff --git
a/dubbo-plugin/dubbo-reactive/src/test/java/org/apache/dubbo/reactive/ManyToManyMethodHandlerTest.java
b/dubbo-plugin/dubbo-reactive/src/test/java/org/apache/dubbo/reactive/ManyToManyMethodHandlerTest.java
index b1c642967b..42276cdd8b 100644
---
a/dubbo-plugin/dubbo-reactive/src/test/java/org/apache/dubbo/reactive/ManyToManyMethodHandlerTest.java
+++
b/dubbo-plugin/dubbo-reactive/src/test/java/org/apache/dubbo/reactive/ManyToManyMethodHandlerTest.java
@@ -19,47 +19,31 @@ package org.apache.dubbo.reactive;
import org.apache.dubbo.common.stream.StreamObserver;
import org.apache.dubbo.reactive.handler.ManyToManyMethodHandler;
-import org.apache.dubbo.rpc.protocol.tri.observer.ServerCallToObserverAdapter;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
-import org.mockito.Mockito;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
-import java.util.concurrent.atomic.AtomicInteger;
-
-import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.ArgumentMatchers.anyString;
-import static org.mockito.Mockito.doAnswer;
/**
* Unit test for ManyToManyMethodHandler
*/
public final class ManyToManyMethodHandlerTest {
-
@Test
void testInvoke() throws ExecutionException, InterruptedException {
- AtomicInteger nextCounter = new AtomicInteger();
- AtomicInteger completeCounter = new AtomicInteger();
- AtomicInteger errorCounter = new AtomicInteger();
- ServerCallToObserverAdapter<String> responseObserver =
Mockito.mock(ServerCallToObserverAdapter.class);
- doAnswer(o -> nextCounter.incrementAndGet())
- .when(responseObserver).onNext(anyString());
- doAnswer(o -> completeCounter.incrementAndGet())
- .when(responseObserver).onCompleted();
- doAnswer(o -> errorCounter.incrementAndGet())
- .when(responseObserver).onError(any(Throwable.class));
+ CreateObserverAdapter creator = new CreateObserverAdapter();
+
ManyToManyMethodHandler<String, String> handler = new
ManyToManyMethodHandler<>(requestFlux ->
requestFlux.map(r -> r + "0"));
- CompletableFuture<StreamObserver<String>> future = handler.invoke(new
Object[]{responseObserver});
+ CompletableFuture<StreamObserver<String>> future = handler.invoke(new
Object[]{creator.getResponseObserver()});
StreamObserver<String> requestObserver = future.get();
for (int i = 0; i < 10; i++) {
requestObserver.onNext(String.valueOf(i));
}
requestObserver.onCompleted();
- Assertions.assertEquals(10, nextCounter.get());
- Assertions.assertEquals(0, errorCounter.get());
- Assertions.assertEquals(1, completeCounter.get());
+ Assertions.assertEquals(10, creator.getNextCounter().get());
+ Assertions.assertEquals(0, creator.getErrorCounter().get());
+ Assertions.assertEquals(1, creator.getCompleteCounter().get());
}
}
diff --git
a/dubbo-plugin/dubbo-reactive/src/test/java/org/apache/dubbo/reactive/ManyToOneMethodHandlerTest.java
b/dubbo-plugin/dubbo-reactive/src/test/java/org/apache/dubbo/reactive/ManyToOneMethodHandlerTest.java
index 1a5953fad1..e1c8fd3122 100644
---
a/dubbo-plugin/dubbo-reactive/src/test/java/org/apache/dubbo/reactive/ManyToOneMethodHandlerTest.java
+++
b/dubbo-plugin/dubbo-reactive/src/test/java/org/apache/dubbo/reactive/ManyToOneMethodHandlerTest.java
@@ -22,63 +22,45 @@ import
org.apache.dubbo.reactive.handler.ManyToOneMethodHandler;
import org.apache.dubbo.rpc.protocol.tri.observer.ServerCallToObserverAdapter;
import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
-import org.mockito.Mockito;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicInteger;
import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.ArgumentMatchers.anyString;
-import static org.mockito.Mockito.doAnswer;
/**
* Unit test for ManyToOneMethodHandler
*/
public final class ManyToOneMethodHandlerTest {
- @Test
- void testInvoker() throws ExecutionException, InterruptedException {
- AtomicInteger nextCounter = new AtomicInteger();
- AtomicInteger completeCounter = new AtomicInteger();
- AtomicInteger errorCounter = new AtomicInteger();
- ServerCallToObserverAdapter<String> responseObserver =
Mockito.mock(ServerCallToObserverAdapter.class);
- doAnswer(o -> nextCounter.incrementAndGet())
- .when(responseObserver).onNext(anyString());
- doAnswer(o -> completeCounter.incrementAndGet())
- .when(responseObserver).onCompleted();
- doAnswer(o -> errorCounter.incrementAndGet())
- .when(responseObserver).onError(any(Throwable.class));
+ private StreamObserver<String> requestObserver;
+ private CreateObserverAdapter creator;
+
+ @BeforeEach
+ void init() throws ExecutionException, InterruptedException {
+ creator = new CreateObserverAdapter();
ManyToOneMethodHandler<String, String> handler = new
ManyToOneMethodHandler<>(requestFlux ->
requestFlux.map(Integer::valueOf).reduce(Integer::sum).map(String::valueOf));
- CompletableFuture<StreamObserver<String>> future = handler.invoke(new
Object[]{responseObserver});
- StreamObserver<String> requestObserver = future.get();
+ CompletableFuture<StreamObserver<String>> future = handler.invoke(new
Object[]{creator.getResponseObserver()});
+ requestObserver = future.get();
+ }
+
+ @Test
+ void testInvoker() {
for (int i = 0; i < 10; i++) {
requestObserver.onNext(String.valueOf(i));
}
requestObserver.onCompleted();
- Assertions.assertEquals(1, nextCounter.get());
- Assertions.assertEquals(0, errorCounter.get());
- Assertions.assertEquals(1, completeCounter.get());
+ Assertions.assertEquals(1, creator.getNextCounter().get());
+ Assertions.assertEquals(0, creator.getErrorCounter().get());
+ Assertions.assertEquals(1, creator.getCompleteCounter().get());
}
@Test
- void testError() throws ExecutionException, InterruptedException {
- AtomicInteger nextCounter = new AtomicInteger();
- AtomicInteger completeCounter = new AtomicInteger();
- AtomicInteger errorCounter = new AtomicInteger();
- ServerCallToObserverAdapter<String> responseObserver =
Mockito.mock(ServerCallToObserverAdapter.class);
- doAnswer(o -> nextCounter.incrementAndGet())
- .when(responseObserver).onNext(anyString());
- doAnswer(o -> completeCounter.incrementAndGet())
- .when(responseObserver).onCompleted();
- doAnswer(o -> errorCounter.incrementAndGet())
- .when(responseObserver).onError(any(Throwable.class));
- ManyToOneMethodHandler<String, String> handler = new
ManyToOneMethodHandler<>(requestFlux ->
-
requestFlux.map(Integer::valueOf).reduce(Integer::sum).map(String::valueOf));
- CompletableFuture<StreamObserver<String>> future = handler.invoke(new
Object[]{responseObserver});
- StreamObserver<String> requestObserver = future.get();
+ void testError() {
for (int i = 0; i < 10; i++) {
if (i == 6) {
requestObserver.onError(new Throwable());
@@ -86,8 +68,8 @@ public final class ManyToOneMethodHandlerTest {
requestObserver.onNext(String.valueOf(i));
}
requestObserver.onCompleted();
- Assertions.assertEquals(0, nextCounter.get());
- Assertions.assertEquals(1, errorCounter.get());
- Assertions.assertEquals(0, completeCounter.get());
+ Assertions.assertEquals(0, creator.getNextCounter().get());
+ Assertions.assertEquals(1, creator.getErrorCounter().get());
+ Assertions.assertEquals(0, creator.getCompleteCounter().get());
}
}
diff --git
a/dubbo-plugin/dubbo-reactive/src/test/java/org/apache/dubbo/reactive/OneToManyMethodHandlerTest.java
b/dubbo-plugin/dubbo-reactive/src/test/java/org/apache/dubbo/reactive/OneToManyMethodHandlerTest.java
index f1cdeb6f65..2fbba77633 100644
---
a/dubbo-plugin/dubbo-reactive/src/test/java/org/apache/dubbo/reactive/OneToManyMethodHandlerTest.java
+++
b/dubbo-plugin/dubbo-reactive/src/test/java/org/apache/dubbo/reactive/OneToManyMethodHandlerTest.java
@@ -21,57 +21,42 @@ import
org.apache.dubbo.reactive.handler.OneToManyMethodHandler;
import org.apache.dubbo.rpc.protocol.tri.observer.ServerCallToObserverAdapter;
import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
-import org.mockito.Mockito;
import reactor.core.publisher.Flux;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicInteger;
import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.ArgumentMatchers.anyString;
-import static org.mockito.Mockito.doAnswer;
/**
* Unit test for OneToManyMethodHandler
*/
public final class OneToManyMethodHandlerTest {
+ private CreateObserverAdapter creator;
+
+ @BeforeEach
+ void init() {
+ creator = new CreateObserverAdapter();
+ }
+
@Test
void testInvoke() {
String request = "1,2,3,4,5,6,7";
- AtomicInteger nextCounter = new AtomicInteger();
- AtomicInteger completeCounter = new AtomicInteger();
- AtomicInteger errorCounter = new AtomicInteger();
- ServerCallToObserverAdapter<String> responseObserver =
Mockito.mock(ServerCallToObserverAdapter.class);
- doAnswer(o -> nextCounter.incrementAndGet())
- .when(responseObserver).onNext(anyString());
- doAnswer(o -> completeCounter.incrementAndGet())
- .when(responseObserver).onCompleted();
- doAnswer(o -> errorCounter.incrementAndGet())
- .when(responseObserver).onError(any(Throwable.class));
OneToManyMethodHandler<String, String> handler = new
OneToManyMethodHandler<>(requestMono ->
requestMono.flatMapMany(r -> Flux.fromArray(r.split(","))));
- CompletableFuture<?> future = handler.invoke(new Object[]{request,
responseObserver});
+ CompletableFuture<?> future = handler.invoke(new Object[]{request,
creator.getResponseObserver()});
Assertions.assertTrue(future.isDone());
- Assertions.assertEquals(7, nextCounter.get());
- Assertions.assertEquals(0, errorCounter.get());
- Assertions.assertEquals(1, completeCounter.get());
+ Assertions.assertEquals(7, creator.getNextCounter().get());
+ Assertions.assertEquals(0, creator.getErrorCounter().get());
+ Assertions.assertEquals(1, creator.getCompleteCounter().get());
}
@Test
void testError() {
String request = "1,2,3,4,5,6,7";
- AtomicInteger nextCounter = new AtomicInteger();
- AtomicInteger completeCounter = new AtomicInteger();
- AtomicInteger errorCounter = new AtomicInteger();
- ServerCallToObserverAdapter<String> responseObserver =
Mockito.mock(ServerCallToObserverAdapter.class);
- doAnswer(o -> nextCounter.incrementAndGet())
- .when(responseObserver).onNext(anyString());
- doAnswer(o -> completeCounter.incrementAndGet())
- .when(responseObserver).onCompleted();
- doAnswer(o -> errorCounter.incrementAndGet())
- .when(responseObserver).onError(any(Throwable.class));
OneToManyMethodHandler<String, String> handler = new
OneToManyMethodHandler<>(requestMono ->
Flux.create(emitter -> {
for (int i = 0; i < 10; i++) {
@@ -82,10 +67,10 @@ public final class OneToManyMethodHandlerTest {
}
}
}));
- CompletableFuture<?> future = handler.invoke(new Object[]{request,
responseObserver});
+ CompletableFuture<?> future = handler.invoke(new Object[]{request,
creator.getResponseObserver()});
Assertions.assertTrue(future.isDone());
- Assertions.assertEquals(6, nextCounter.get());
- Assertions.assertEquals(1, errorCounter.get());
- Assertions.assertEquals(0, completeCounter.get());
+ Assertions.assertEquals(6, creator.getNextCounter().get());
+ Assertions.assertEquals(1, creator.getErrorCounter().get());
+ Assertions.assertEquals(0, creator.getCompleteCounter().get());
}
}