Skip to content
Permalink
Browse files
add register mode test
  • Loading branch information
EarthChen committed Sep 9, 2021
1 parent 9a674ae commit 390aa668c498efbb9fc1d7448eb5171fe885221c
Showing 21 changed files with 662 additions and 109 deletions.
@@ -25,11 +25,11 @@ Code under `test` folder is a general interoperation test suite for both Dubbo/D
##### GRPC --> Dubbo

1. Run `org.apache.dubbo.sample.tri.TriProvider`
2. Run `org.apache.dubbo.sample.tri.GrpcConsumerTest`
2. Run `org.apache.dubbo.sample.tri.grpc.GrpcConsumerTest`

##### Dubbo --> GRPC

1. Run `org.apache.dubbo.sample.tri.GrpcProvider`
1. Run `org.apache.dubbo.sample.tri.grpc.GrpcProvider`
2. Run `org.apache.dubbo.sample.tri.TriPbConsumerTest`


@@ -18,15 +18,26 @@
package org.apache.dubbo.sample.tri;


import static org.apache.dubbo.common.constants.RegistryConstants.DEFAULT_REGISTER_MODE_ALL;
import static org.apache.dubbo.common.constants.RegistryConstants.DEFAULT_REGISTER_MODE_INSTANCE;
import static org.apache.dubbo.common.constants.RegistryConstants.DEFAULT_REGISTER_MODE_INTERFACE;
import static org.apache.dubbo.common.constants.RegistryConstants.REGISTER_MODE_KEY;

public class TriSampleConstants {

// macos 11 later the 50051 is occupied by system (pid=1!!!)
public static final int SERVER_PORT = 50052;

public static final int DEFAULT_DUBBO_PORT=20880;
public static final int DEFAULT_DUBBO_PORT = 20880;

public static final String ZK_ADDRESS = "zookeeper://127.0.0.1:2181";

public static final String ZK_ADDRESS_MODE_INSTANCE = "zookeeper://127.0.0.1:2181?" + REGISTER_MODE_KEY + "=" + DEFAULT_REGISTER_MODE_INSTANCE;

public static final String ZK_ADDRESS_MODE_INTERFACE = "zookeeper://127.0.0.1:2181?" + REGISTER_MODE_KEY + "=" + DEFAULT_REGISTER_MODE_INTERFACE;

public static final String ZK_ADDRESS_MODE_ALL = "zookeeper://127.0.0.1:2181?" + REGISTER_MODE_KEY + "=" + DEFAULT_REGISTER_MODE_ALL;

public static final String HOST = "127.0.0.1";

public static final String DEFAULT_ADDRESS = "tri://" + HOST + ":" + SERVER_PORT;
@@ -0,0 +1,134 @@
package org.apache.dubbo.sample.tri;

import org.apache.dubbo.common.stream.StreamObserver;
import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.RpcException;
import org.apache.dubbo.sample.tri.service.PbGreeterManual;

import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
* @author earthchen
* @date 2021/9/9
**/
public abstract class BasePbConsumerTest {

protected static PbGreeter delegate;

protected static PbGreeterManual delegateManual;
//
// @BeforeClass
// public static void init() {
// ReferenceConfig<PbGreeter> ref = new ReferenceConfig<>();
// ref.setInterface(PbGreeter.class);
// ref.setCheck(false);
// ref.setUrl(TriSampleConstants.DEFAULT_ADDRESS);
// ref.setProtocol(CommonConstants.TRIPLE);
// ref.setLazy(true);
// ref.setTimeout(10000);
//
// ReferenceConfig<PbGreeterManual> ref2 = new ReferenceConfig<>();
// ref2.setInterface(PbGreeterManual.class);
// ref2.setCheck(false);
// ref2.setUrl(TriSampleConstants.DEFAULT_ADDRESS);
// ref2.setProtocol(CommonConstants.TRIPLE);
// ref2.setLazy(true);
// ref2.setTimeout(10000);
//
// DubboBootstrap bootstrap = DubboBootstrap.getInstance();
// bootstrap.application(new ApplicationConfig("demo-consumer"))
//// .registry(new RegistryConfig("zookeeper://127.0.0.1:2181"))
// .reference(ref)
// .reference(ref2)
// .start();
//
// delegate = ref.get();
// delegateManual = ref2.get();
// }

@Test
public void serverStream() throws InterruptedException {
int n = 10;
CountDownLatch latch = new CountDownLatch(n);
final GreeterRequest request = GreeterRequest.newBuilder()
.setName("request")
.build();
delegate.greetServerStream(request, new StdoutStreamObserver<GreeterReply>("sayGreeterServerStream") {
@Override
public void onNext(GreeterReply data) {
super.onNext(data);
latch.countDown();
}
});
Assert.assertTrue(latch.await(3, TimeUnit.SECONDS));
}

@Test
public void stream() throws InterruptedException {
int n = 10;
CountDownLatch latch = new CountDownLatch(n);
final GreeterRequest request = GreeterRequest.newBuilder()
.setName("stream request")
.build();
final StreamObserver<GreeterRequest> requestObserver = delegate.greetStream(new StdoutStreamObserver<GreeterReply>("sayGreeterStream") {
@Override
public void onNext(GreeterReply data) {
super.onNext(data);
latch.countDown();
}
});
for (int i = 0; i < n; i++) {
requestObserver.onNext(request);
}
requestObserver.onCompleted();
Assert.assertTrue(latch.await(3, TimeUnit.SECONDS));
}

@Test
public void unaryGreeter() {
final GreeterReply reply = delegate.greet(GreeterRequest.newBuilder()
.setName("name")
.build());
Assert.assertNotNull(reply);
}


@Test(expected = RpcException.class)
@Ignore
public void clientSendLargeSizeHeader() {
StringBuilder sb = new StringBuilder("a");
for (int j = 0; j < 15; j++) {
sb.append(sb);
}
sb.setLength(8191);
RpcContext.getClientAttachment().setObjectAttachment("large-size-meta", sb.toString());
delegate.greet(GreeterRequest.newBuilder().setName("meta").build());
RpcContext.getClientAttachment().clearAttachments();
}

@Test
public void attachmentTest() {
final String key = "user-attachment";
final String value = "attachment-value";
RpcContext.removeClientAttachment();
RpcContext.getClientAttachment().setAttachment(key, value);
delegate.greetWithAttachment(GreeterRequest.newBuilder().setName("meta").build());
final String returned = (String) RpcContext.getServiceContext().getObjectAttachment(key);
Assert.assertEquals(value, returned);
}

@Test
public void methodNotFound() {
try {
delegateManual.methodNonExist(GreeterRequest.newBuilder().setName("meta").build());
TimeUnit.SECONDS.sleep(1);
} catch (RpcException | InterruptedException e) {
Assert.assertTrue(e.getMessage().contains("not found"));
}
}
}
@@ -0,0 +1,138 @@
package org.apache.dubbo.sample.tri;

import org.apache.dubbo.common.stream.StreamObserver;
import org.apache.dubbo.rpc.RpcException;
import org.apache.dubbo.sample.tri.service.WrapGreeter;

import org.junit.Assert;
import org.junit.Test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class BaseTriWrapConsumerTest {

protected static WrapGreeter delegate;

// @BeforeClass
// public static void initStub() {
// ReferenceConfig<WrapGreeter> ref = new ReferenceConfig<>();
// ref.setInterface(WrapGreeter.class);
// ref.setCheck(false);
// ref.setTimeout(3000);
// ref.setProtocol(CommonConstants.TRIPLE);
// ref.setLazy(true);
//
// DubboBootstrap bootstrap = DubboBootstrap.getInstance();
// bootstrap.application(new ApplicationConfig("demo-consumer"))
// .registry(new RegistryConfig(TriSampleConstants.ZK_ADDRESS))
// .reference(ref)
// .start();
// delegate = ref.get();
// }

@Test
public void sayHelloUnaryRequestVoid() {
Assert.assertEquals("hello!void", delegate.sayHelloRequestVoid());
}

@Test
public void sayHelloUnaryResponseVoid() {
delegate.sayHelloResponseVoid("void");
}

@Test
public void sayHelloUnary() {
Assert.assertEquals("hello,unary", delegate.sayHello("unary"));
}

@Test(expected = RpcException.class)
public void sayHelloException() {
delegate.sayHelloException("exception");
}

@Test
public void sayHelloServerStream() throws InterruptedException {
int n = 10;
CountDownLatch latch = new CountDownLatch(n);
delegate.sayHelloServerStream("server stream", new StdoutStreamObserver<String>("sayHelloServerStream") {
@Override
public void onNext(String data) {
super.onNext(data);
latch.countDown();
}
});
Assert.assertTrue(latch.await(3, TimeUnit.SECONDS));


delegate.sayHelloServerStream("server stream", new StreamObserver<String>() {
@Override
public void onNext(String data) {
System.out.println(data);
}

@Override
public void onError(Throwable throwable) {
throwable.printStackTrace();
}

@Override
public void onCompleted() {
System.out.println("onCompleted");
}
});


StreamObserver<String> request = delegate.sayHelloStream(new StreamObserver<String>() {
@Override
public void onNext(String data) {
System.out.println(data);
}

@Override
public void onError(Throwable throwable) {
throwable.printStackTrace();
}

@Override
public void onCompleted() {
System.out.println("onCompleted");
}
});
for (int i = 0; i < n; i++) {
request.onNext("stream request" + i);
}
request.onCompleted();


}

@Test
public void sayHelloStream() throws InterruptedException {
int n = 10;
CountDownLatch latch = new CountDownLatch(n);
final StreamObserver<String> request = delegate.sayHelloStream(new StdoutStreamObserver<String>("sayHelloStream") {
@Override
public void onNext(String data) {
super.onNext(data);
latch.countDown();
}
});
for (int i = 0; i < n; i++) {
request.onNext("stream request");
}
request.onCompleted();
Assert.assertTrue(latch.await(3, TimeUnit.SECONDS));
}

@Test
public void sayHelloLong() {
int power = 25;
for (int i = 0; i < power; i++) {
final int len = (1 << i);
final String response = delegate.sayHelloLong(len);
System.out.println("Response len:" + response.length());
Assert.assertEquals(len, response.length());
}
}
}

0 comments on commit 390aa66

Please sign in to comment.