-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsubscribe_test.go
73 lines (58 loc) · 1.37 KB
/
subscribe_test.go
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
package rpcx
import (
"context"
"testing"
"time"
"github.com/meshplus/bitxhub-kit/crypto"
"github.com/meshplus/bitxhub-kit/crypto/asym"
"github.com/meshplus/bitxhub-model/pb"
"github.com/stretchr/testify/require"
)
func TestChainClient_Subscribe(t *testing.T) {
privKey, err := asym.GenerateKeyPair(crypto.Secp256k1)
require.Nil(t, err)
privKey1, err := asym.GenerateKeyPair(crypto.Secp256k1)
require.Nil(t, err)
from, err := privKey.PublicKey().Address()
require.Nil(t, err)
to, err := privKey1.PublicKey().Address()
require.Nil(t, err)
cli, err := New(
WithNodesInfo(cfg.nodesInfo...),
WithLogger(cfg.logger),
WithPrivateKey(privKey),
)
require.Nil(t, err)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c, err := cli.Subscribe(ctx, pb.SubscriptionRequest_BLOCK, nil)
require.Nil(t, err)
td := &pb.TransactionData{
Amount: "10",
}
data, err := td.Marshal()
require.Nil(t, err)
go func() {
tx := &pb.BxhTransaction{
From: from,
To: to,
Payload: data,
Timestamp: time.Now().UnixNano(),
}
err = tx.Sign(privKey)
require.Nil(t, err)
hash, err := cli.SendTransaction(tx, nil)
require.Nil(t, err)
require.EqualValues(t, 66, len(hash))
}()
for {
select {
case block, ok := <-c:
require.Equal(t, true, ok)
require.NotNil(t, block)
return
case <-ctx.Done():
return
}
}
}