Skip to content

Commit 77bca87

Browse files
author
Luis Sanchez
committed
[FAB-3901] Improve h/f/orderer/ram coverage
improve hyperledger/fabric/orderer/ram coverage to 98.7% Change-Id: I214b79e01c2bb4741bd2d7990616ab2a7f1bb49a Signed-off-by: Luis Sanchez <sanchezl@us.ibm.com>
1 parent 2599f1b commit 77bca87

File tree

2 files changed

+157
-0
lines changed

2 files changed

+157
-0
lines changed

orderer/ledger/ram/factory_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
Copyright IBM Corp. 2016 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package ramledger
18+
19+
import (
20+
"testing"
21+
22+
"github.com/hyperledger/fabric/common/configtx/tool/provisional"
23+
24+
logging "github.com/op/go-logging"
25+
)
26+
27+
func init() {
28+
logging.SetLevel(logging.DEBUG, "")
29+
}
30+
31+
func TestGetOrCreate(t *testing.T) {
32+
rlf := New(3)
33+
channel, err := rlf.GetOrCreate(provisional.TestChainID)
34+
if err != nil {
35+
panic(err)
36+
}
37+
channel2, err := rlf.GetOrCreate(provisional.TestChainID)
38+
if err != nil {
39+
panic(err)
40+
}
41+
if channel != channel2 {
42+
t.Fatalf("Expecting already created channel.")
43+
}
44+
}
45+
46+
func TestChainIDs(t *testing.T) {
47+
rlf := New(3)
48+
rlf.GetOrCreate("channel1")
49+
rlf.GetOrCreate("channel2")
50+
rlf.GetOrCreate("channel3")
51+
if len(rlf.ChainIDs()) != 3 {
52+
t.Fatalf("Expecting three channels,")
53+
}
54+
rlf.Close()
55+
}

orderer/ledger/ram/impl_test.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ import (
2020
"testing"
2121

2222
"github.com/hyperledger/fabric/common/configtx/tool/provisional"
23+
"github.com/hyperledger/fabric/orderer/ledger"
2324
cb "github.com/hyperledger/fabric/protos/common"
25+
ab "github.com/hyperledger/fabric/protos/orderer"
2426

2527
logging "github.com/op/go-logging"
2628
)
@@ -106,3 +108,103 @@ func TestTruncationSafety(t *testing.T) {
106108
t.Fatalf("The iterator should have found %d new blocks but found %d", newBlocks, count)
107109
}
108110
}
111+
112+
func TestRetrieval(t *testing.T) {
113+
rl := newTestChain(3)
114+
rl.Append(ledger.CreateNextBlock(rl, []*cb.Envelope{&cb.Envelope{Payload: []byte("My Data")}}))
115+
it, num := rl.Iterator(&ab.SeekPosition{Type: &ab.SeekPosition_Oldest{}})
116+
if num != 0 {
117+
t.Fatalf("Expected genesis block iterator, but got %d", num)
118+
}
119+
signal := it.ReadyChan()
120+
select {
121+
case <-signal:
122+
default:
123+
t.Fatalf("Should be ready for block read")
124+
}
125+
block, status := it.Next()
126+
if status != cb.Status_SUCCESS {
127+
t.Fatalf("Expected to successfully read the genesis block")
128+
}
129+
if block.Header.Number != 0 {
130+
t.Fatalf("Expected to successfully retrieve the genesis block")
131+
}
132+
signal = it.ReadyChan()
133+
select {
134+
case <-signal:
135+
default:
136+
t.Fatalf("Should still be ready for block read")
137+
}
138+
block, status = it.Next()
139+
if status != cb.Status_SUCCESS {
140+
t.Fatalf("Expected to successfully read the second block")
141+
}
142+
if block.Header.Number != 1 {
143+
t.Fatalf("Expected to successfully retrieve the second block but got block number %d", block.Header.Number)
144+
}
145+
}
146+
147+
func TestBlockedRetrieval(t *testing.T) {
148+
rl := newTestChain(3)
149+
it, num := rl.Iterator(&ab.SeekPosition{Type: &ab.SeekPosition_Specified{Specified: &ab.SeekSpecified{Number: 1}}})
150+
if num != 1 {
151+
t.Fatalf("Expected block iterator at 1, but got %d", num)
152+
}
153+
signal := it.ReadyChan()
154+
select {
155+
case <-signal:
156+
t.Fatalf("Should not be ready for block read")
157+
default:
158+
}
159+
rl.Append(ledger.CreateNextBlock(rl, []*cb.Envelope{&cb.Envelope{Payload: []byte("My Data")}}))
160+
select {
161+
case <-signal:
162+
default:
163+
t.Fatalf("Should now be ready for block read")
164+
}
165+
block, status := it.Next()
166+
if status != cb.Status_SUCCESS {
167+
t.Fatalf("Expected to successfully read the second block")
168+
}
169+
if block.Header.Number != 1 {
170+
t.Fatalf("Expected to successfully retrieve the second block")
171+
}
172+
}
173+
174+
func TestIteratorPastEnd(t *testing.T) {
175+
rl := newTestChain(3)
176+
it, _ := rl.Iterator(&ab.SeekPosition{Type: &ab.SeekPosition_Specified{Specified: &ab.SeekSpecified{Number: 2}}})
177+
if _, status := it.Next(); status != cb.Status_NOT_FOUND {
178+
t.Fatalf("Expected block with status NOT_FOUND, but got %d", status)
179+
}
180+
}
181+
182+
func TestIteratorOldest(t *testing.T) {
183+
rl := newTestChain(3)
184+
// add enough block to roll off the genesis block
185+
rl.Append(ledger.CreateNextBlock(rl, []*cb.Envelope{&cb.Envelope{Payload: []byte("My Data")}}))
186+
rl.Append(ledger.CreateNextBlock(rl, []*cb.Envelope{&cb.Envelope{Payload: []byte("My Data")}}))
187+
rl.Append(ledger.CreateNextBlock(rl, []*cb.Envelope{&cb.Envelope{Payload: []byte("My Data")}}))
188+
_, num := rl.Iterator(&ab.SeekPosition{Type: &ab.SeekPosition_Specified{Specified: &ab.SeekSpecified{Number: 1}}})
189+
if num != 1 {
190+
t.Fatalf("Expected block iterator at 1, but got %d", num)
191+
}
192+
}
193+
194+
func TestAppendBadBLock(t *testing.T) {
195+
rl := newTestChain(3)
196+
t.Run("BadBlockNumber", func(t *testing.T) {
197+
nextBlock := ledger.CreateNextBlock(rl, []*cb.Envelope{&cb.Envelope{Payload: []byte("My Data")}})
198+
nextBlock.Header.Number = nextBlock.Header.Number + 1
199+
if err := rl.Append(nextBlock); err == nil {
200+
t.Fatalf("Expected Append to fail.")
201+
}
202+
})
203+
t.Run("BadPreviousHash", func(t *testing.T) {
204+
nextBlock := ledger.CreateNextBlock(rl, []*cb.Envelope{&cb.Envelope{Payload: []byte("My Data")}})
205+
nextBlock.Header.PreviousHash = []byte("bad hash")
206+
if err := rl.Append(nextBlock); err == nil {
207+
t.Fatalf("Expected Append to fail.")
208+
}
209+
})
210+
}

0 commit comments

Comments
 (0)