kv_ledger_provider_test.go 2.98 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 kvledger

import (
	"fmt"
	"testing"

23
	"github.com/hyperledger/fabric/common/ledger/testutil"
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
	"github.com/hyperledger/fabric/core/ledger"
)

func TestLedgerProvider(t *testing.T) {
	env := newTestEnv(t)
	defer env.cleanup()
	numLedgers := 10
	provider, _ := NewProvider()
	existingLedgerIDs, err := provider.List()
	testutil.AssertNoError(t, err, "")
	testutil.AssertEquals(t, len(existingLedgerIDs), 0)
	for i := 0; i < numLedgers; i++ {
		provider.Create(constructTestLedgerID(i))
	}
	existingLedgerIDs, err = provider.List()
	testutil.AssertNoError(t, err, "")
	testutil.AssertEquals(t, len(existingLedgerIDs), numLedgers)

	provider.Close()

	provider, _ = NewProvider()
	defer provider.Close()
	ledgerIds, _ := provider.List()
	testutil.AssertEquals(t, len(ledgerIds), numLedgers)
	t.Logf("ledgerIDs=%#v", ledgerIds)
	for i := 0; i < numLedgers; i++ {
		testutil.AssertEquals(t, ledgerIds[i], constructTestLedgerID(i))
	}
	_, err = provider.Create(constructTestLedgerID(2))
	testutil.AssertEquals(t, err, ErrLedgerIDExists)

	_, err = provider.Open(constructTestLedgerID(numLedgers))
	testutil.AssertEquals(t, err, ErrNonExistingLedgerID)
}

func TestMultipleLedgerBasicRW(t *testing.T) {
	env := newTestEnv(t)
	defer env.cleanup()
	numLedgers := 10
	provider, _ := NewProvider()
64
	ledgers := make([]ledger.PeerLedger, numLedgers)
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
	for i := 0; i < numLedgers; i++ {
		l, err := provider.Create(constructTestLedgerID(i))
		testutil.AssertNoError(t, err, "")
		ledgers[i] = l
	}

	for i, l := range ledgers {
		s, _ := l.NewTxSimulator()
		err := s.SetState("ns", "testKey", []byte(fmt.Sprintf("testValue_%d", i)))
		s.Done()
		testutil.AssertNoError(t, err, "")
		res, err := s.GetTxSimulationResults()
		testutil.AssertNoError(t, err, "")
		b := testutil.ConstructBlock(t, [][]byte{res}, false)
		err = l.Commit(b)
		l.Close()
		testutil.AssertNoError(t, err, "")
	}

	provider.Close()

	provider, _ = NewProvider()
	defer provider.Close()
88
	ledgers = make([]ledger.PeerLedger, numLedgers)
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
	for i := 0; i < numLedgers; i++ {
		l, err := provider.Open(constructTestLedgerID(i))
		testutil.AssertNoError(t, err, "")
		ledgers[i] = l
	}

	for i, l := range ledgers {
		q, _ := l.NewQueryExecutor()
		val, err := q.GetState("ns", "testKey")
		q.Done()
		testutil.AssertNoError(t, err, "")
		testutil.AssertEquals(t, val, []byte(fmt.Sprintf("testValue_%d", i)))
		l.Close()
	}
}

func constructTestLedgerID(i int) string {
	return fmt.Sprintf("ledger_%06d", i)
}