blockfile_scan_test.go 2.68 KB
Newer Older
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 (
	"os"
	"testing"

23
24
	"github.com/hyperledger/fabric/common/ledger/testutil"
	"github.com/hyperledger/fabric/common/ledger/util"
25

26
	"github.com/hyperledger/fabric/protos/common"
27
28
29
)

func TestBlockFileScanSmallTxOnly(t *testing.T) {
30
	env := newTestEnv(t, NewConf(testPath, 0))
31
	defer env.Cleanup()
manish's avatar
manish committed
32
33
	ledgerid := "testLedger"
	blkfileMgrWrapper := newTestBlockfileWrapper(env, ledgerid)
34
35
36
37
38
	bg := testutil.NewBlockGenerator(t)
	blocks := []*common.Block{}
	blocks = append(blocks, bg.NextTestBlock(0, 0))
	blocks = append(blocks, bg.NextTestBlock(0, 0))
	blocks = append(blocks, bg.NextTestBlock(0, 0))
39
40
41
	blkfileMgrWrapper.addBlocks(blocks)
	blkfileMgrWrapper.close()

manish's avatar
manish committed
42
	filePath := deriveBlockfilePath(env.provider.conf.getLedgerBlockDir(ledgerid), 0)
43
44
45
	_, fileSize, err := util.FileExists(filePath)
	testutil.AssertNoError(t, err, "")

manish's avatar
manish committed
46
	endOffsetLastBlock, numBlocks, err := scanForLastCompleteBlock(env.provider.conf.getLedgerBlockDir(ledgerid), 0, 0)
47
48
49
50
51
52
	testutil.AssertNoError(t, err, "")
	testutil.AssertEquals(t, numBlocks, len(blocks))
	testutil.AssertEquals(t, endOffsetLastBlock, fileSize)
}

func TestBlockFileScanSmallTxLastTxIncomplete(t *testing.T) {
53
	env := newTestEnv(t, NewConf(testPath, 0))
54
	defer env.Cleanup()
manish's avatar
manish committed
55
56
	ledgerid := "testLedger"
	blkfileMgrWrapper := newTestBlockfileWrapper(env, ledgerid)
57
58
59
60
61
	bg := testutil.NewBlockGenerator(t)
	blocks := []*common.Block{}
	blocks = append(blocks, bg.NextTestBlock(0, 0))
	blocks = append(blocks, bg.NextTestBlock(0, 0))
	blocks = append(blocks, bg.NextTestBlock(0, 0))
62
63
64
	blkfileMgrWrapper.addBlocks(blocks)
	blkfileMgrWrapper.close()

manish's avatar
manish committed
65
	filePath := deriveBlockfilePath(env.provider.conf.getLedgerBlockDir(ledgerid), 0)
66
67
68
69
70
71
72
73
74
	_, fileSize, err := util.FileExists(filePath)
	testutil.AssertNoError(t, err, "")

	file, err := os.OpenFile(filePath, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0660)
	defer file.Close()
	testutil.AssertNoError(t, err, "")
	err = file.Truncate(fileSize - 1)
	testutil.AssertNoError(t, err, "")

manish's avatar
manish committed
75
	_, numBlocks, err := scanForLastCompleteBlock(env.provider.conf.getLedgerBlockDir(ledgerid), 0, 0)
76
77
78
	testutil.AssertNoError(t, err, "")
	testutil.AssertEquals(t, numBlocks, len(blocks)-1)
}