Skip to content

Commit 8ba92d3

Browse files
committed
[FAB-4946] Improve UT coverage of orderer/ledger/file
This patchset also cleans up the test style to use assert, as well as moving some factory-specific tests to its own test file from impl test file. Change-Id: I02e6c5e364b3e5ae254c45853595acd32eabcd7b Signed-off-by: Jay Guo <[email protected]>
1 parent 4e219e9 commit 8ba92d3

File tree

3 files changed

+257
-93
lines changed

3 files changed

+257
-93
lines changed

orderer/ledger/file/factory_test.go

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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 fileledger
18+
19+
import (
20+
"fmt"
21+
"io/ioutil"
22+
"testing"
23+
24+
"github.com/hyperledger/fabric/common/configtx/tool/provisional"
25+
"github.com/hyperledger/fabric/common/ledger/blkstorage"
26+
"github.com/hyperledger/fabric/orderer/ledger"
27+
"github.com/stretchr/testify/assert"
28+
)
29+
30+
type mockBlockStoreProvider struct {
31+
blockstore blkstorage.BlockStore
32+
exists bool
33+
list []string
34+
error error
35+
}
36+
37+
func (mbsp *mockBlockStoreProvider) CreateBlockStore(ledgerid string) (blkstorage.BlockStore, error) {
38+
return mbsp.blockstore, mbsp.error
39+
}
40+
41+
func (mbsp *mockBlockStoreProvider) OpenBlockStore(ledgerid string) (blkstorage.BlockStore, error) {
42+
return mbsp.blockstore, mbsp.error
43+
}
44+
45+
func (mbsp *mockBlockStoreProvider) Exists(ledgerid string) (bool, error) {
46+
return mbsp.exists, mbsp.error
47+
}
48+
49+
func (mbsp *mockBlockStoreProvider) List() ([]string, error) {
50+
return mbsp.list, mbsp.error
51+
}
52+
53+
func (mbsp *mockBlockStoreProvider) Close() {
54+
}
55+
56+
func TestBlockstoreProviderError(t *testing.T) {
57+
flf := &fileLedgerFactory{
58+
blkstorageProvider: &mockBlockStoreProvider{error: fmt.Errorf("blockstorage provider error")},
59+
ledgers: make(map[string]ledger.ReadWriter),
60+
}
61+
assert.Panics(
62+
t,
63+
func() { flf.ChainIDs() },
64+
"Expected ChainIDs to panic if storage provider cannot list chain IDs")
65+
66+
_, err := flf.GetOrCreate("foo")
67+
assert.Error(t, err, "Expected GetOrCreate to return error if blockstorage provider cannot open")
68+
assert.Empty(t, flf.ledgers, "Expected no new ledger is created")
69+
}
70+
71+
func TestMultiReinitialization(t *testing.T) {
72+
dir, err := ioutil.TempDir("", "hyperledger_fabric")
73+
assert.NoError(t, err, "Error creating temp dir: %s", err)
74+
75+
flf := New(dir)
76+
_, err = flf.GetOrCreate(provisional.TestChainID)
77+
assert.NoError(t, err, "Error GetOrCreate chain")
78+
assert.Equal(t, 1, len(flf.ChainIDs()), "Expected 1 chain")
79+
flf.Close()
80+
81+
flf = New(dir)
82+
_, err = flf.GetOrCreate("foo")
83+
assert.NoError(t, err, "Error creating chain")
84+
assert.Equal(t, 2, len(flf.ChainIDs()), "Expected chain to be recovered")
85+
flf.Close()
86+
87+
flf = New(dir)
88+
_, err = flf.GetOrCreate("bar")
89+
assert.NoError(t, err, "Error creating chain")
90+
assert.Equal(t, 3, len(flf.ChainIDs()), "Expected chain to be recovered")
91+
flf.Close()
92+
}

orderer/ledger/file/impl.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ func (fl *fileLedger) Iterator(startPosition *ab.SeekPosition) (ledger.Iterator,
8686
return &ledger.NotFoundErrorIterator{}, 0
8787
}
8888
return &fileLedgerIterator{ledger: fl, blockNumber: start.Specified.Number}, start.Specified.Number
89+
default:
90+
return &ledger.NotFoundErrorIterator{}, 0
8991
}
90-
// This line should be unreachable, but the compiler requires it
91-
return &ledger.NotFoundErrorIterator{}, 0
9292
}
9393

9494
// Height returns the number of blocks on the ledger

0 commit comments

Comments
 (0)