blocks_itr_test.go 1.98 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"
	"time"

23
	"github.com/hyperledger/fabric/common/ledger/testutil"
24
	"github.com/hyperledger/fabric/protos/common"
manish's avatar
manish committed
25
26
27
)

func TestBlocksItrBlockingNext(t *testing.T) {
28
	env := newTestEnv(t, NewConf(testPath, 0))
manish's avatar
manish committed
29
	defer env.Cleanup()
manish's avatar
manish committed
30
	blkfileMgrWrapper := newTestBlockfileWrapper(env, "testLedger")
manish's avatar
manish committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
	defer blkfileMgrWrapper.close()
	blkfileMgr := blkfileMgrWrapper.blockfileMgr

	blocks := testutil.ConstructTestBlocks(t, 10)
	blkfileMgrWrapper.addBlocks(blocks[:5])

	itr, err := blkfileMgr.retrieveBlocks(2)
	defer itr.Close()
	testutil.AssertNoError(t, err, "")
	doneChan := make(chan bool)
	go testIterateAndVerify(t, itr, blocks[1:], doneChan)
	for {
		if itr.blockNumToRetrieve == 6 {
			break
		}
		time.Sleep(time.Millisecond * 10)
	}
	testAppendBlocks(blkfileMgrWrapper, blocks[5:7])
	blkfileMgr.moveToNextFile()
	time.Sleep(time.Millisecond * 10)
	testAppendBlocks(blkfileMgrWrapper, blocks[7:])
	<-doneChan
}

manish's avatar
manish committed
55
func testIterateAndVerify(t *testing.T, itr *blocksItr, blocks []*common.Block, doneChan chan bool) {
manish's avatar
manish committed
56
57
	blocksIterated := 0
	for {
manish's avatar
manish committed
58
		bh, err := itr.Next()
manish's avatar
manish committed
59
		testutil.AssertNoError(t, err, "")
manish's avatar
manish committed
60
		testutil.AssertEquals(t, bh.(*blockHolder).GetBlock(), blocks[blocksIterated])
manish's avatar
manish committed
61
62
63
64
65
66
67
68
		blocksIterated++
		if blocksIterated == len(blocks) {
			break
		}
	}
	doneChan <- true
}

69
func testAppendBlocks(blkfileMgrWrapper *testBlockfileMgrWrapper, blocks []*common.Block) {
manish's avatar
manish committed
70
71
	blkfileMgrWrapper.addBlocks(blocks)
}