1717package events
1818
1919import (
20- "encoding/json"
2120 "fmt"
22- "net/http"
23- "net/http/httptest"
2421 "strings"
2522 "sync"
2623 "testing"
2724 "time"
2825
29- "github.com/hyperledger/fabric-protos-go/common"
30- "github.com/hyperledger/fabric-protos-go/peer"
31- "github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
32- eventmocks "github.com/hyperledger/fabric-sdk-go/pkg/fab/events/service/mocks"
3326 "github.com/hyperledger/firefly-fabconnect/internal/conf"
3427 "github.com/hyperledger/firefly-fabconnect/internal/errors"
3528 eventsapi "github.com/hyperledger/firefly-fabconnect/internal/events/api"
@@ -40,119 +33,6 @@ import (
4033 "github.com/stretchr/testify/mock"
4134)
4235
43- func newTestStreamForBatching (spec * StreamInfo , db kvstore.KVStore , status ... int ) (* subscriptionMGR , * eventStream , * httptest.Server , chan []* eventsapi.EventEntry ) {
44- mux := http .NewServeMux ()
45- eventStream := make (chan []* eventsapi.EventEntry )
46- count := 0
47- mux .HandleFunc ("/" , func (res http.ResponseWriter , req * http.Request ) {
48- var events []* eventsapi.EventEntry
49- _ = json .NewDecoder (req .Body ).Decode (& events )
50- eventStream <- events
51- idx := count
52- if idx >= len (status ) {
53- idx = len (status ) - 1
54- }
55- res .WriteHeader (status [idx ])
56- count ++
57- })
58- svr := httptest .NewServer (mux )
59- if spec .Type == "" {
60- spec .Type = "webhook"
61- spec .Webhook .URL = svr .URL
62- spec .Webhook .Headers = map [string ]string {"x-my-header" : "my-value" }
63- }
64- sm := newTestSubscriptionManager ()
65- sm .config .WebhooksAllowPrivateIPs = true
66- sm .config .PollingIntervalSec = 0
67- if db != nil {
68- sm .db = db
69- }
70- mockstore , ok := sm .db .(* mockkvstore.KVStore )
71- if ok {
72- mockstore .On ("Get" , mock .Anything ).Return ([]byte ("" ), nil )
73- mockstore .On ("Put" , mock .Anything , mock .Anything ).Return (nil )
74- }
75-
76- _ = sm .addStream (spec )
77- return sm , sm .streams [spec .ID ], svr , eventStream
78- }
79-
80- func newTestStreamForWebSocket (spec * StreamInfo , db kvstore.KVStore , status ... int ) (* subscriptionMGR , * eventStream , * mockWebSocket ) {
81- sm := newTestSubscriptionManager ()
82- sm .config .PollingIntervalSec = 0
83- if db != nil {
84- sm .db = db
85- }
86- _ = sm .addStream (spec )
87- return sm , sm .streams [spec .ID ], sm .wsChannels .(* mockWebSocket )
88- }
89-
90- func testEvent (subID string ) * eventData {
91- entry := & eventsapi.EventEntry {
92- SubID : subID ,
93- }
94- return & eventData {
95- event : entry ,
96- batchComplete : func (* eventsapi.EventEntry ) {},
97- }
98- }
99-
100- func mockRPCClient (fromBlock string , withReset ... bool ) * mockfabric.RPCClient {
101- rpc := & mockfabric.RPCClient {}
102- blockEventChan := make (chan * fab.BlockEvent )
103- ccEventChan := make (chan * fab.CCEvent )
104- var roBlockEventChan <- chan * fab.BlockEvent = blockEventChan
105- var roCCEventChan <- chan * fab.CCEvent = ccEventChan
106- res := & fab.BlockchainInfoResponse {
107- BCI : & common.BlockchainInfo {
108- Height : 10 ,
109- },
110- }
111- rpc .On ("SubscribeEvent" , mock .Anything , mock .Anything ).Return (nil , roBlockEventChan , roCCEventChan , nil )
112- rpc .On ("QueryChainInfo" , mock .Anything , mock .Anything ).Return (res , nil )
113- rpc .On ("Unregister" , mock .Anything ).Return ()
114-
115- go func () {
116- if fromBlock == "0" {
117- blockEventChan <- & fab.BlockEvent {
118- Block : constructBlock (1 ),
119- }
120- }
121- blockEventChan <- & fab.BlockEvent {
122- Block : constructBlock (11 ),
123- }
124- if len (withReset ) > 0 {
125- blockEventChan <- & fab.BlockEvent {
126- Block : constructBlock (11 ),
127- }
128- }
129- }()
130-
131- return rpc
132- }
133-
134- func setupTestSubscription (sm * subscriptionMGR , stream * eventStream , subscriptionName , fromBlock string , withReset ... bool ) * eventsapi.SubscriptionInfo {
135- rpc := mockRPCClient (fromBlock , withReset ... )
136- sm .rpc = rpc
137- spec := & eventsapi.SubscriptionInfo {
138- Name : subscriptionName ,
139- Stream : stream .spec .ID ,
140- }
141- if fromBlock != "" {
142- spec .FromBlock = fromBlock
143- }
144- _ = sm .addSubscription (spec )
145-
146- return spec
147- }
148-
149- func constructBlock (number uint64 ) * common.Block {
150- mockTx := eventmocks .NewTransactionWithCCEvent ("testTxID" , peer .TxValidationCode_VALID , "testChaincodeID" , "testCCEventName" , []byte ("testPayload" ))
151- mockBlock := eventmocks .NewBlock ("testChannelID" , mockTx )
152- mockBlock .Header .Number = number
153- return mockBlock
154- }
155-
15636func TestConstructorNoSpec (t * testing.T ) {
15737 assert := assert .New (t )
15838 _ , err := newEventStream (newTestSubscriptionManager (), nil , nil )
@@ -500,7 +380,7 @@ func TestProcessEventsEnd2EndWebhook(t *testing.T) {
500380 & StreamInfo {
501381 BatchSize : 1 ,
502382 Webhook : & webhookActionInfo {},
503- Timestamps : false ,
383+ Timestamps : true ,
504384 }, db , 200 )
505385 defer svr .Close ()
506386
@@ -512,9 +392,15 @@ func TestProcessEventsEnd2EndWebhook(t *testing.T) {
512392 wg := & sync.WaitGroup {}
513393 wg .Add (1 )
514394 go func () {
395+ // the block event
515396 e1s := <- eventStream
516397 assert .Equal (1 , len (e1s ))
517398 assert .Equal (uint64 (11 ), e1s [0 ].BlockNumber )
399+ // the chaincode event
400+ e2s := <- eventStream
401+ assert .Equal (1 , len (e2s ))
402+ assert .Equal (uint64 (10 ), e2s [0 ].BlockNumber )
403+ assert .Equal (int64 (1000000 ), e2s [0 ].Timestamp )
518404 wg .Done ()
519405 }()
520406 wg .Wait ()
@@ -537,7 +423,7 @@ func TestProcessEventsEnd2EndCatchupWebhook(t *testing.T) {
537423 _ = db .Init ()
538424 sm , stream , svr , eventStream := newTestStreamForBatching (
539425 & StreamInfo {
540- BatchSize : 1 ,
426+ BatchSize : 2 ,
541427 Webhook : & webhookActionInfo {},
542428 Timestamps : false ,
543429 }, db , 200 )
@@ -552,11 +438,9 @@ func TestProcessEventsEnd2EndCatchupWebhook(t *testing.T) {
552438 wg .Add (1 )
553439 go func () {
554440 e1s := <- eventStream
555- assert .Equal (1 , len (e1s ))
441+ assert .Equal (2 , len (e1s ))
556442 assert .Equal (uint64 (1 ), e1s [0 ].BlockNumber )
557- e2s := <- eventStream
558- assert .Equal (1 , len (e2s ))
559- assert .Equal (uint64 (11 ), e2s [0 ].BlockNumber )
443+ assert .Equal (uint64 (11 ), e1s [1 ].BlockNumber )
560444 wg .Done ()
561445 }()
562446 wg .Wait ()
@@ -636,6 +520,10 @@ func TestProcessEventsEnd2EndWithReset(t *testing.T) {
636520 e1s := <- eventStream
637521 assert .Equal (1 , len (e1s ))
638522 assert .Equal (uint64 (11 ), e1s [0 ].BlockNumber )
523+ // the chaincode event
524+ e2s := <- eventStream
525+ assert .Equal (1 , len (e2s ))
526+ assert .Equal (uint64 (10 ), e2s [0 ].BlockNumber )
639527 wg .Done ()
640528 }()
641529 wg .Wait ()
@@ -745,7 +633,7 @@ func TestPauseResumeAfterCheckpoint(t *testing.T) {
745633 wg := & sync.WaitGroup {}
746634 wg .Add (1 )
747635 go func () {
748- for i := 0 ; i < 1 ; i ++ {
636+ for i := 0 ; i < 2 ; i ++ {
749637 <- eventStream
750638 }
751639 wg .Done ()
@@ -811,7 +699,7 @@ func TestPauseResumeBeforeCheckpoint(t *testing.T) {
811699 wg := & sync.WaitGroup {}
812700 wg .Add (1 )
813701 go func () {
814- for i := 0 ; i < 1 ; i ++ {
702+ for i := 0 ; i < 2 ; i ++ {
815703 <- eventStream
816704 }
817705 wg .Done ()
@@ -851,7 +739,7 @@ func TestMarkStaleOnError(t *testing.T) {
851739 wg := & sync.WaitGroup {}
852740 wg .Add (1 )
853741 go func () {
854- for i := 0 ; i < 1 ; i ++ {
742+ for i := 0 ; i < 2 ; i ++ {
855743 <- eventStream
856744 }
857745 wg .Done ()
@@ -929,7 +817,7 @@ func TestStoreCheckpointStoreError(t *testing.T) {
929817 wg := & sync.WaitGroup {}
930818 wg .Add (1 )
931819 go func () {
932- for i := 0 ; i < 1 ; i ++ {
820+ for i := 0 ; i < 2 ; i ++ {
933821 <- eventStream
934822 }
935823 wg .Done ()
@@ -1199,6 +1087,7 @@ func TestUpdateStreamMissingWebhookURL(t *testing.T) {
11991087 wg := & sync.WaitGroup {}
12001088 wg .Add (1 )
12011089 go func () {
1090+ <- eventStream
12021091 <- eventStream
12031092 wg .Done ()
12041093 }()
@@ -1242,6 +1131,7 @@ func TestUpdateStreamInvalidWebhookURL(t *testing.T) {
12421131 wg := & sync.WaitGroup {}
12431132 wg .Add (1 )
12441133 go func () {
1134+ <- eventStream
12451135 <- eventStream
12461136 wg .Done ()
12471137 }()
0 commit comments