-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_exports.go
80 lines (71 loc) · 2.13 KB
/
test_exports.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package pvtdatastorage
import (
"io/ioutil"
"os"
"testing"
"time"
"github.com/jxu86/fabric-gm/core/ledger"
"github.com/jxu86/fabric-gm/core/ledger/pvtdatapolicy"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func pvtDataConf() *PrivateDataConfig {
return &PrivateDataConfig{
PrivateDataConfig: &ledger.PrivateDataConfig{
BatchesInterval: 1000,
MaxBatchSize: 5000,
PurgeInterval: 2,
DeprioritizedDataReconcilerInterval: 120 * time.Minute,
},
StorePath: "",
}
}
// StoreEnv provides the store env for testing
type StoreEnv struct {
t testing.TB
TestStoreProvider *Provider
TestStore *Store
ledgerid string
btlPolicy pvtdatapolicy.BTLPolicy
conf *PrivateDataConfig
}
// NewTestStoreEnv construct a StoreEnv for testing
func NewTestStoreEnv(
t *testing.T,
ledgerid string,
btlPolicy pvtdatapolicy.BTLPolicy,
conf *PrivateDataConfig) *StoreEnv {
storeDir, err := ioutil.TempDir("", "pdstore")
if err != nil {
t.Fatalf("Failed to create private data storage directory: %s", err)
}
conf.StorePath = storeDir
testStoreProvider, err := NewProvider(conf)
require.NoError(t, err)
testStore, err := testStoreProvider.OpenStore(ledgerid)
testStore.Init(btlPolicy)
require.NoError(t, err)
return &StoreEnv{t, testStoreProvider, testStore, ledgerid, btlPolicy, conf}
}
// CloseAndReopen closes and opens the store provider
func (env *StoreEnv) CloseAndReopen() {
var err error
env.TestStoreProvider.Close()
env.TestStoreProvider, err = NewProvider(env.conf)
assert.NoError(env.t, err)
env.TestStore, err = env.TestStoreProvider.OpenStore(env.ledgerid)
env.TestStore.Init(env.btlPolicy)
assert.NoError(env.t, err)
}
// Cleanup cleansup the store env after testing
func (env *StoreEnv) Cleanup() {
env.TestStoreProvider.Close()
env.TestStore.db.Close()
if err := os.RemoveAll(env.conf.StorePath); err != nil {
env.t.Errorf("error while removing path %s, %v", env.conf.StorePath, err)
}
}