Skip to content

Commit 248fc59

Browse files
committed
[FAB-4349] Improve UT coverage of orderer/solo
Change-Id: I45ae124305f720e552a47403c518f46623bd194e Signed-off-by: Jay Guo <[email protected]>
1 parent 4eb5815 commit 248fc59

File tree

1 file changed

+74
-1
lines changed

1 file changed

+74
-1
lines changed

orderer/solo/consensus_test.go

+74-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
cb "github.com/hyperledger/fabric/protos/common"
2727

2828
logging "github.com/op/go-logging"
29+
"github.com/stretchr/testify/assert"
2930
)
3031

3132
func init() {
@@ -54,7 +55,8 @@ func goWithWait(target func()) *waitableGo {
5455
return wg
5556
}
5657

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) {
5860
batchTimeout, _ := time.ParseDuration("1ms")
5961
support := &mockmultichain.ConsenterSupport{
6062
Blocks: make(chan *cb.Block),
@@ -75,6 +77,45 @@ func TestEmptyBatch(t *testing.T) {
7577
}
7678
}
7779

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+
78119
func TestBatchTimer(t *testing.T) {
79120
batchTimeout, _ := time.ParseDuration("1ms")
80121
support := &mockmultichain.ConsenterSupport{
@@ -188,3 +229,35 @@ func TestConfigStyleMultiBatch(t *testing.T) {
188229
case <-wg.done:
189230
}
190231
}
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

Comments
 (0)