-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgateway_test.go
80 lines (70 loc) · 2 KB
/
gateway_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
74
75
76
77
78
79
80
/*
Copyright Zhigui.com. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package fabric_connector
import (
"context"
"sync"
"testing"
"time"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
"github.com/stretchr/testify/assert"
)
var gw Gateway
func init() {
var err error
gw, err = NewGatewayService("./conf/connection-org1.yaml", "User1",
"./artifacts/crypto-config/peerOrganizations/org1.example.com/users/{username}@org1.example.com/msp", "Org1MSP")
if err != nil {
panic(err)
}
}
func TestGatewayService_CallContract(t *testing.T) {
payload, err := gw.SubmitTransaction(testChannelId, testCCId, "move", []string{"a", "b", "10", testEventID})
assert.NoError(t, err)
t.Log(string(payload))
result, err := gw.EvaluateTransaction(testChannelId, testCCId, "query", []string{"a"})
assert.NoError(t, err)
t.Log(string(result))
}
func TestGatewayService_RegisterContractEvent(t *testing.T) {
wg := sync.WaitGroup{}
wg.Add(2)
ctx, cancel := context.WithCancel(context.Background())
go func() {
err := gw.RegisterChaincodeEvent(ctx, testChannelId, testCCId, testEventID, func(e *fab.CCEvent) {
t.Logf("ChaincodeEvent receive data: %+v, payload:%s \n", e, string(e.Payload))
})
assert.NoError(t, err)
wg.Done()
}()
go func() {
_, err := gw.SubmitTransaction(testChannelId, testCCId, "move", []string{"a", "b", "10", testEventID})
assert.NoError(t, err)
wg.Done()
time.Sleep(time.Second)
cancel()
}()
wg.Wait()
}
func TestGatewayService_RegisterBlockEvent(t *testing.T) {
wg := sync.WaitGroup{}
wg.Add(2)
ctx, cancel := context.WithCancel(context.Background())
go func() {
err := gw.RegisterBlockEvent(ctx, testChannelId, func(data *TransactionInfo) {
t.Logf("EventHandler receive data: %+v \n", data)
})
assert.NoError(t, err)
wg.Done()
}()
go func() {
_, err := gw.SubmitTransaction(testChannelId, testCCId, "move", []string{"a", "b", "10", testEventID})
assert.NoError(t, err)
wg.Done()
time.Sleep(time.Second)
cancel()
}()
wg.Wait()
}