-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSubscribeTest.java
More file actions
89 lines (76 loc) · 2.7 KB
/
SubscribeTest.java
File metadata and controls
89 lines (76 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package cn.dmlab.bitxhub;
import cn.dmlab.crypto.ecdsa.ECKeyS256;
import cn.dmlab.utils.SignUtils;
import cn.dmlab.utils.Utils;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import io.grpc.stub.StreamObserver;
import lombok.extern.slf4j.Slf4j;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import pb.BlockOuterClass;
import pb.Broker;
import pb.Transaction;
import java.util.concurrent.CountDownLatch;
@RunWith(JUnit4.class)
@Slf4j
public class SubscribeTest {
private GrpcClient client;
private Config config = Config.defaultConfig();
byte[] from = config.getAddress();
byte[] to = new ECKeyS256().getAddress();
@Before
public void setUp() {
client = new GrpcClientImpl(config);
}
@After
public void tearDown() throws Exception {
client.stop();
}
@Test
public void sendTransaction() {
Transaction.BxhTransaction unsignedTx = Transaction.BxhTransaction.newBuilder()
.setFrom(ByteString.copyFrom(from))
.setTo(ByteString.copyFrom(to))
.setTimestamp(Utils.genTimestamp())
.setPayload(Transaction.TransactionData.newBuilder().setAmount("100000").build().toByteString())
.build();
Transaction.BxhTransaction signedTx = SignUtils.sign(unsignedTx, config.getEcKey());
String txHash = client.sendTransaction(signedTx, null);
Assert.assertNotNull(txHash);
}
@Test
public void subscribe() throws InterruptedException {
CountDownLatch asyncLatch = new CountDownLatch(1);
StreamObserver<Broker.Response> observer = new StreamObserver<Broker.Response>() {
@Override
public void onNext(Broker.Response response) {
ByteString data = response.getData();
BlockOuterClass.Block block = null;
try {
block = BlockOuterClass.Block.parseFrom(data);
} catch (InvalidProtocolBufferException e) {
e.printStackTrace();
}
Assert.assertNotNull(block);
asyncLatch.countDown();
}
@Override
public void onError(Throwable throwable) {
throwable.printStackTrace();
asyncLatch.countDown();
}
@Override
public void onCompleted() {
asyncLatch.countDown();
}
};
client.subscribe(Broker.SubscriptionRequest.Type.BLOCK, observer);
sendTransaction();
asyncLatch.await();
}
}