block_stream_test.go 4.28 KB
Newer Older
manish's avatar
manish committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
Copyright IBM Corp. 2016 All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

		 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package fsblkstorage

import (
	"testing"

	"github.com/golang/protobuf/proto"
23
	"github.com/hyperledger/fabric/common/ledger/testutil"
manish's avatar
manish committed
24
25
26
27
28
29
30
31
32
)

func TestBlockfileStream(t *testing.T) {
	testBlockfileStream(t, 0)
	testBlockfileStream(t, 1)
	testBlockfileStream(t, 10)
}

func testBlockfileStream(t *testing.T, numBlocks int) {
33
	env := newTestEnv(t, NewConf(testPath, 0))
manish's avatar
manish committed
34
	defer env.Cleanup()
manish's avatar
manish committed
35
36
	ledgerid := "testledger"
	w := newTestBlockfileWrapper(env, ledgerid)
manish's avatar
manish committed
37
38
39
40
	blocks := testutil.ConstructTestBlocks(t, numBlocks)
	w.addBlocks(blocks)
	w.close()

manish's avatar
manish committed
41
	s, err := newBlockfileStream(w.blockfileMgr.rootDir, 0, 0)
manish's avatar
manish committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
	defer s.close()
	testutil.AssertNoError(t, err, "Error in constructing blockfile stream")

	blockCount := 0
	for {
		blockBytes, err := s.nextBlockBytes()
		testutil.AssertNoError(t, err, "Error in getting next block")
		if blockBytes == nil {
			break
		}
		blockCount++
	}
	// After the stream has been exhausted, both blockBytes and err should be nil
	blockBytes, err := s.nextBlockBytes()
	testutil.AssertNil(t, blockBytes)
	testutil.AssertNoError(t, err, "Error in getting next block after exhausting the file")
	testutil.AssertEquals(t, blockCount, numBlocks)
}

func TestBlockFileStreamUnexpectedEOF(t *testing.T) {
	partialBlockBytes := []byte{}
	dummyBlockBytes := testutil.ConstructRandomBytes(t, 100)
	lenBytes := proto.EncodeVarint(uint64(len(dummyBlockBytes)))
	partialBlockBytes = append(partialBlockBytes, lenBytes...)
	partialBlockBytes = append(partialBlockBytes, dummyBlockBytes...)
	testBlockFileStreamUnexpectedEOF(t, 10, partialBlockBytes[:1])
	testBlockFileStreamUnexpectedEOF(t, 10, partialBlockBytes[:2])
	testBlockFileStreamUnexpectedEOF(t, 10, partialBlockBytes[:5])
	testBlockFileStreamUnexpectedEOF(t, 10, partialBlockBytes[:20])
}

func testBlockFileStreamUnexpectedEOF(t *testing.T, numBlocks int, partialBlockBytes []byte) {
74
	env := newTestEnv(t, NewConf(testPath, 0))
manish's avatar
manish committed
75
	defer env.Cleanup()
manish's avatar
manish committed
76
	w := newTestBlockfileWrapper(env, "testLedger")
manish's avatar
manish committed
77
78
79
80
81
	blockfileMgr := w.blockfileMgr
	blocks := testutil.ConstructTestBlocks(t, numBlocks)
	w.addBlocks(blocks)
	blockfileMgr.currentFileWriter.append(partialBlockBytes, true)
	w.close()
manish's avatar
manish committed
82
	s, err := newBlockfileStream(blockfileMgr.rootDir, 0, 0)
manish's avatar
manish committed
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
	defer s.close()
	testutil.AssertNoError(t, err, "Error in constructing blockfile stream")

	for i := 0; i < numBlocks; i++ {
		blockBytes, err := s.nextBlockBytes()
		testutil.AssertNotNil(t, blockBytes)
		testutil.AssertNoError(t, err, "Error in getting next block")
	}
	blockBytes, err := s.nextBlockBytes()
	testutil.AssertNil(t, blockBytes)
	testutil.AssertSame(t, err, ErrUnexpectedEndOfBlockfile)
}

func TestBlockStream(t *testing.T) {
	testBlockStream(t, 1)
	testBlockStream(t, 2)
	testBlockStream(t, 10)
}

func testBlockStream(t *testing.T, numFiles int) {
103
	env := newTestEnv(t, NewConf(testPath, 0))
manish's avatar
manish committed
104
	defer env.Cleanup()
manish's avatar
manish committed
105
	w := newTestBlockfileWrapper(env, "testLedger")
manish's avatar
manish committed
106
107
108
109
	defer w.close()
	blockfileMgr := w.blockfileMgr

	numBlocksInEachFile := 10
110
	bg := testutil.NewBlockGenerator(t)
manish's avatar
manish committed
111
	for i := 0; i < numFiles; i++ {
112
		blocks := bg.NextTestBlocks(numBlocksInEachFile)
manish's avatar
manish committed
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
		w.addBlocks(blocks)
		blockfileMgr.moveToNextFile()
	}
	s, err := newBlockStream(blockfileMgr.rootDir, 0, 0, numFiles-1)
	defer s.close()
	testutil.AssertNoError(t, err, "Error in constructing new block stream")
	blockCount := 0
	for {
		blockBytes, err := s.nextBlockBytes()
		testutil.AssertNoError(t, err, "Error in getting next block")
		if blockBytes == nil {
			break
		}
		blockCount++
	}
	// After the stream has been exhausted, both blockBytes and err should be nil
	blockBytes, err := s.nextBlockBytes()
	testutil.AssertNil(t, blockBytes)
	testutil.AssertNoError(t, err, "Error in getting next block after exhausting the file")
	testutil.AssertEquals(t, blockCount, numFiles*numBlocksInEachFile)
}