Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
21 changed files
with
662 additions
and
109 deletions.
There are no files selected for viewing
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
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
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,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")); | ||
} | ||
} | ||
} |
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,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()); | ||
} | ||
} | ||
} |
Oops, something went wrong.