@@ -20,7 +20,6 @@ limitations under the License.
2020package integration
2121
2222import (
23- "fmt"
2423 "testing"
2524 "time"
2625
@@ -31,7 +30,15 @@ import (
3130)
3231
3332func TestEvents (t * testing.T ) {
33+ testSetup := initializeTests (t )
3434
35+ testFailedTx (t , testSetup )
36+ testFailedTxErrorCode (t , testSetup )
37+ testReconnectEventHub (t , testSetup )
38+ testMultipleBlockEventCallbacks (t , testSetup )
39+ }
40+
41+ func initializeTests (t * testing.T ) BaseSetupImpl {
3542 testSetup := BaseSetupImpl {
3643 ConfigFile : "../fixtures/config/config_test.yaml" ,
3744 ChainID : "testchannel" ,
@@ -54,22 +61,7 @@ func TestEvents(t *testing.T) {
5461 t .Fatalf ("instantiateCC return error: %v" , err )
5562 }
5663
57- testFailedTx (t , testSetup )
58-
59- testFailedTxErrorCode (t , testSetup )
60- // Test disconnect event hub
61- testSetup .EventHub .Disconnect ()
62- if testSetup .EventHub .IsConnected () {
63- t .Fatalf ("Failed to disconnect event hub" )
64- }
65-
66- // Reconnect event hub
67- if err := testSetup .EventHub .Connect (); err != nil {
68- t .Fatalf ("Failed to connect event hub" )
69- }
70-
71- testMultipleBlockEventCallbacks (t , testSetup )
72-
64+ return testSetup
7365}
7466
7567func testFailedTx (t * testing.T , testSetup BaseSetupImpl ) {
@@ -121,11 +113,9 @@ func testFailedTx(t *testing.T, testSetup BaseSetupImpl) {
121113 t .Fatalf ("invoke Didn't receive block event for txid1(%s) or txid1(%s)" , tx1 , tx2 )
122114 }
123115 }
124-
125116}
126117
127118func testFailedTxErrorCode (t * testing.T , testSetup BaseSetupImpl ) {
128-
129119 // Arguments for events CC
130120 var args []string
131121 args = append (args , "invoke" )
@@ -190,7 +180,7 @@ func testFailedTxErrorCode(t *testing.T, testSetup BaseSetupImpl) {
190180 t .Fatalf ("Received success for second invoke" )
191181 case <- fail2 :
192182 // success
193- fmt . Println ("Received error validation Code " , errorValidationCode )
183+ t . Logf ("Received error validation code %s " , errorValidationCode . String () )
194184 if errorValidationCode .String () != "MVCC_READ_CONFLICT" {
195185 t .Fatalf ("Expected error code MVCC_READ_CONFLICT" )
196186 }
@@ -202,8 +192,20 @@ func testFailedTxErrorCode(t *testing.T, testSetup BaseSetupImpl) {
202192
203193}
204194
205- func testMultipleBlockEventCallbacks (t * testing.T , testSetup BaseSetupImpl ) {
195+ func testReconnectEventHub (t * testing.T , testSetup BaseSetupImpl ) {
196+ // Test disconnect event hub
197+ testSetup .EventHub .Disconnect ()
198+ if testSetup .EventHub .IsConnected () {
199+ t .Fatalf ("Failed to disconnect event hub" )
200+ }
206201
202+ // Reconnect event hub
203+ if err := testSetup .EventHub .Connect (); err != nil {
204+ t .Fatalf ("Failed to connect event hub" )
205+ }
206+ }
207+
208+ func testMultipleBlockEventCallbacks (t * testing.T , testSetup BaseSetupImpl ) {
207209 // Arguments for events CC
208210 var args []string
209211 args = append (args , "invoke" )
@@ -213,13 +215,13 @@ func testMultipleBlockEventCallbacks(t *testing.T, testSetup BaseSetupImpl) {
213215 // Create and register test callback that will be invoked upon block event
214216 test := make (chan bool )
215217 testSetup .EventHub .RegisterBlockEvent (func (block * common.Block ) {
216- fmt . Println ( "Invoked test callback on block event" )
218+ t . Logf ( "Received test callback on block event" )
217219 test <- true
218220 })
219221
220222 tpResponses , tx , err := fcUtil .CreateAndSendTransactionProposal (testSetup .Chain , testSetup .ChainCodeID , testSetup .ChainID , args , []fabricClient.Peer {testSetup .Chain .GetPrimaryPeer ()}, nil )
221223 if err != nil {
222- t .Fatalf ("CreateAndSendTransactionProposal return error: %v \n " , err )
224+ t .Fatalf ("CreateAndSendTransactionProposal returned error: %v \n " , err )
223225 }
224226
225227 // Register tx for commit/block event(s)
@@ -228,7 +230,7 @@ func testMultipleBlockEventCallbacks(t *testing.T, testSetup BaseSetupImpl) {
228230
229231 _ , err = fcUtil .CreateAndSendTransaction (testSetup .Chain , tpResponses )
230232 if err != nil {
231- t .Fatalf ("First invoke failed err : %v" , err )
233+ t .Fatalf ("CreateAndSendTransaction failed with error : %v" , err )
232234 }
233235
234236 for i := 0 ; i < 2 ; i ++ {
@@ -237,7 +239,7 @@ func testMultipleBlockEventCallbacks(t *testing.T, testSetup BaseSetupImpl) {
237239 case <- fail :
238240 case <- test :
239241 case <- time .After (time .Second * 30 ):
240- t .Fatalf ("invoke Didn't receive test callback event" )
242+ t .Fatalf ("Didn't receive test callback event" )
241243 }
242244 }
243245
0 commit comments