@@ -26,6 +26,7 @@ import (
26
26
cb "github.com/hyperledger/fabric/protos/common"
27
27
28
28
logging "github.com/op/go-logging"
29
+ "github.com/stretchr/testify/assert"
29
30
)
30
31
31
32
func init () {
@@ -54,7 +55,8 @@ func goWithWait(target func()) *waitableGo {
54
55
return wg
55
56
}
56
57
57
- func TestEmptyBatch (t * testing.T ) {
58
+ // This test checks that if consenter is halted before a timer fires, nothing is actually written.
59
+ func TestHaltBeforeTimeout (t * testing.T ) {
58
60
batchTimeout , _ := time .ParseDuration ("1ms" )
59
61
support := & mockmultichain.ConsenterSupport {
60
62
Blocks : make (chan * cb.Block ),
@@ -75,6 +77,45 @@ func TestEmptyBatch(t *testing.T) {
75
77
}
76
78
}
77
79
80
+ func TestStart (t * testing.T ) {
81
+ batchTimeout , _ := time .ParseDuration ("1ms" )
82
+ support := & mockmultichain.ConsenterSupport {
83
+ Blocks : make (chan * cb.Block ),
84
+ BlockCutterVal : mockblockcutter .NewReceiver (),
85
+ SharedConfigVal : & mockconfig.Orderer {BatchTimeoutVal : batchTimeout },
86
+ }
87
+ close (support .BlockCutterVal .Block )
88
+ bs , _ := New ().HandleChain (support , nil )
89
+ bs .Start ()
90
+ defer bs .Halt ()
91
+
92
+ support .BlockCutterVal .CutNext = true
93
+ bs .Enqueue (testMessage )
94
+ select {
95
+ case <- support .Blocks :
96
+ case <- bs .Errored ():
97
+ t .Fatalf ("Expected not to exit" )
98
+ }
99
+ }
100
+
101
+ func TestEnqueueAfterHalt (t * testing.T ) {
102
+ batchTimeout , _ := time .ParseDuration ("1ms" )
103
+ support := & mockmultichain.ConsenterSupport {
104
+ Blocks : make (chan * cb.Block ),
105
+ BlockCutterVal : mockblockcutter .NewReceiver (),
106
+ SharedConfigVal : & mockconfig.Orderer {BatchTimeoutVal : batchTimeout },
107
+ }
108
+ defer close (support .BlockCutterVal .Block )
109
+ bs := newChain (support )
110
+ bs .Halt ()
111
+ assert .False (t , bs .Enqueue (testMessage ), "Enqueue should not be accepted after halt" )
112
+ select {
113
+ case <- bs .Errored ():
114
+ default :
115
+ t .Fatalf ("Expected Errored to be closed by halt" )
116
+ }
117
+ }
118
+
78
119
func TestBatchTimer (t * testing.T ) {
79
120
batchTimeout , _ := time .ParseDuration ("1ms" )
80
121
support := & mockmultichain.ConsenterSupport {
@@ -188,3 +229,35 @@ func TestConfigStyleMultiBatch(t *testing.T) {
188
229
case <- wg .done :
189
230
}
190
231
}
232
+
233
+ // This test checks that solo consenter could recover from an erroneous situation
234
+ // where empty batch is cut
235
+ func TestRecoverFromError (t * testing.T ) {
236
+ batchTimeout , _ := time .ParseDuration ("1ms" )
237
+ support := & mockmultichain.ConsenterSupport {
238
+ Blocks : make (chan * cb.Block ),
239
+ BlockCutterVal : mockblockcutter .NewReceiver (),
240
+ SharedConfigVal : & mockconfig.Orderer {BatchTimeoutVal : batchTimeout },
241
+ }
242
+ defer close (support .BlockCutterVal .Block )
243
+ bs := newChain (support )
244
+ _ = goWithWait (bs .main )
245
+ defer bs .Halt ()
246
+
247
+ syncQueueMessage (testMessage , bs , support .BlockCutterVal )
248
+ support .BlockCutterVal .CurBatch = nil
249
+
250
+ select {
251
+ case <- support .Blocks :
252
+ t .Fatalf ("Expected no invocations of Append" )
253
+ case <- time .After (2 * time .Millisecond ):
254
+ }
255
+
256
+ support .BlockCutterVal .CutNext = true
257
+ syncQueueMessage (testMessage , bs , support .BlockCutterVal )
258
+ select {
259
+ case <- support .Blocks :
260
+ case <- time .After (time .Second ):
261
+ t .Fatalf ("Expected block to be cut" )
262
+ }
263
+ }
0 commit comments