Skip to content

Commit 8c60eea

Browse files
Brett LoganBrett Logan
authored andcommitted
[FAB-15476] Reduce CouchDB exposure via reuse
Refactored lockbabsedtxmgr to use one instance of Couch. Instead of starting a new Couch container for each test we initialize a new ledger in the same DB. This reduces our exposure to the number of possible timeouts on Couch initializations to a single instance, down from > 30. This reduced the time for testing this package by over 70% Change-Id: If6333812872a77ea5375dcb2d9ef6ae4b49c5634 Signed-off-by: Brett Logan <Brett.T.Logan@ibm.com>
1 parent 15f3941 commit 8c60eea

File tree

5 files changed

+30
-59
lines changed

5 files changed

+30
-59
lines changed

core/ledger/kvledger/txmgmt/txmgr/lockbasedtxmgr/collection_val_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
func TestCollectionValidation(t *testing.T) {
1717
testEnv := testEnvsMap[levelDBtestEnvName]
1818
testEnv.init(t, "testLedger", nil)
19-
defer testEnv.cleanup()
2019
txMgr := testEnv.getTxMgr()
2120
populateCollConfigForTest(t, txMgr.(*LockBasedTxMgr),
2221
[]collConfigkey{
@@ -54,7 +53,6 @@ func TestCollectionValidation(t *testing.T) {
5453
func TestPvtGetNoCollection(t *testing.T) {
5554
testEnv := testEnvs[0]
5655
testEnv.init(t, "test-pvtdata-get-no-collection", nil)
57-
defer testEnv.cleanup()
5856
txMgr := testEnv.getTxMgr().(*LockBasedTxMgr)
5957
queryHelper := newQueryHelper(txMgr, nil, true)
6058
valueHash, metadataBytes, err := queryHelper.getPrivateDataValueHash("cc", "coll", "key")
@@ -66,7 +64,6 @@ func TestPvtGetNoCollection(t *testing.T) {
6664
func TestPvtPutNoCollection(t *testing.T) {
6765
testEnv := testEnvs[0]
6866
testEnv.init(t, "test-pvtdata-put-no-collection", nil)
69-
defer testEnv.cleanup()
7067
txMgr := testEnv.getTxMgr().(*LockBasedTxMgr)
7168
txsim, err := txMgr.NewTxSimulator("txid")
7269
assert.NoError(t, err)
@@ -78,7 +75,6 @@ func TestPvtPutNoCollection(t *testing.T) {
7875
func TestNoCollectionValidationCheck(t *testing.T) {
7976
testEnv := testEnvs[0]
8077
testEnv.init(t, "test-no-collection-validation-check", nil)
81-
defer testEnv.cleanup()
8278
txMgr := testEnv.getTxMgr().(*LockBasedTxMgr)
8379
qe, err := txMgr.NewQueryExecutorNoCollChecks()
8480
assert.NoError(t, err)

core/ledger/kvledger/txmgmt/txmgr/lockbasedtxmgr/helper_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ func TestPvtdataResultsItr(t *testing.T) {
3030
},
3131
)
3232
testEnv.init(t, "test-pvtdata-range-queries", btlPolicy)
33-
defer testEnv.cleanup()
3433

3534
txMgr := testEnv.getTxMgr().(*LockBasedTxMgr)
3635
populateCollConfigForTest(t, txMgr, []collConfigkey{
@@ -88,7 +87,6 @@ func testPrivateDataMetadataRetrievalByHash(t *testing.T, env testEnv) {
8887
},
8988
)
9089
env.init(t, ledgerid, btlPolicy)
91-
defer env.cleanup()
9290

9391
txMgr := env.getTxMgr()
9492
bg, _ := testutil.NewBlockGenerator(t, ledgerid, false)
@@ -132,7 +130,6 @@ func testGetPvtdataHash(t *testing.T, env testEnv) {
132130
},
133131
)
134132
env.init(t, ledgerid, btlPolicy)
135-
defer env.cleanup()
136133
txMgr := env.getTxMgr().(*LockBasedTxMgr)
137134
populateCollConfigForTest(t, txMgr, []collConfigkey{{"ns", "coll"}}, version.NewHeight(1, 1))
138135

core/ledger/kvledger/txmgmt/txmgr/lockbasedtxmgr/pkg_test.go

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ package lockbasedtxmgr
77

88
import (
99
"fmt"
10+
"os"
1011
"testing"
1112

1213
"github.com/golang/protobuf/proto"
14+
"github.com/hyperledger/fabric/common/flogging"
1315
"github.com/hyperledger/fabric/common/ledger/testutil"
1416
"github.com/hyperledger/fabric/core/ledger"
1517
"github.com/hyperledger/fabric/core/ledger/kvledger/bookkeeping"
@@ -26,12 +28,23 @@ import (
2628
"github.com/stretchr/testify/assert"
2729
)
2830

31+
func TestMain(m *testing.M) {
32+
flogging.ActivateSpec(
33+
"lockbasedtxmgr,statevalidator,statebasedval,statecouchdb,valimpl,pvtstatepurgemgmt,valinternal=debug",
34+
)
35+
exitCode := m.Run()
36+
for _, testEnv := range testEnvs {
37+
testEnv.cleanup()
38+
}
39+
os.Exit(exitCode)
40+
}
41+
2942
type testEnv interface {
30-
init(t *testing.T, testLedgerID string, btlPolicy pvtdatapolicy.BTLPolicy)
43+
cleanup()
3144
getName() string
3245
getTxMgr() txmgr.TxMgr
3346
getVDB() privacyenabledstate.DB
34-
cleanup()
47+
init(t *testing.T, testLedgerID string, btlPolicy pvtdatapolicy.BTLPolicy)
3548
}
3649

3750
const (
@@ -54,15 +67,13 @@ var testEnvsMap = map[string]testEnv{
5467
///////////// LevelDB Environment //////////////
5568

5669
type lockBasedEnv struct {
57-
t testing.TB
58-
name string
59-
60-
testDBEnv privacyenabledstate.TestEnv
61-
testDB privacyenabledstate.DB
62-
70+
dbInitialized bool
71+
name string
72+
t testing.TB
6373
testBookkeepingEnv *bookkeeping.TestEnv
64-
65-
txmgr txmgr.TxMgr
74+
testDB privacyenabledstate.DB
75+
testDBEnv privacyenabledstate.TestEnv
76+
txmgr txmgr.TxMgr
6677
}
6778

6879
func (env *lockBasedEnv) getName() string {
@@ -72,7 +83,10 @@ func (env *lockBasedEnv) getName() string {
7283
func (env *lockBasedEnv) init(t *testing.T, testLedgerID string, btlPolicy pvtdatapolicy.BTLPolicy) {
7384
var err error
7485
env.t = t
75-
env.testDBEnv.Init(t)
86+
if env.dbInitialized == false {
87+
env.testDBEnv.Init(t)
88+
env.dbInitialized = true
89+
}
7690
env.testDB = env.testDBEnv.GetDBHandle(testLedgerID)
7791
assert.NoError(t, err)
7892
if btlPolicy == nil {
@@ -98,9 +112,11 @@ func (env *lockBasedEnv) getVDB() privacyenabledstate.DB {
98112
}
99113

100114
func (env *lockBasedEnv) cleanup() {
101-
env.txmgr.Shutdown()
102-
env.testDBEnv.Cleanup()
103-
env.testBookkeepingEnv.Cleanup()
115+
if env.dbInitialized {
116+
env.txmgr.Shutdown()
117+
env.testDBEnv.Cleanup()
118+
env.testBookkeepingEnv.Cleanup()
119+
}
104120
}
105121

106122
//////////// txMgrTestHelper /////////////

core/ledger/kvledger/txmgmt/txmgr/lockbasedtxmgr/state_listener_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ func TestStateListener(t *testing.T) {
3333

3434
testEnv := testEnvsMap[levelDBtestEnvName]
3535
testEnv.init(t, testLedgerid, nil)
36-
defer testEnv.cleanup()
3736
txmgr := testEnv.getTxMgr().(*LockBasedTxMgr)
3837
txmgr.stateListeners = []ledger.StateListener{ml1, ml2, ml3}
3938

@@ -135,7 +134,6 @@ func TestStateListener(t *testing.T) {
135134
func TestStateListenerQueryExecutor(t *testing.T) {
136135
testEnv := testEnvsMap[levelDBtestEnvName]
137136
testEnv.init(t, "testLedger", nil)
138-
defer testEnv.cleanup()
139137
txMgr := testEnv.getTxMgr().(*LockBasedTxMgr)
140138

141139
namespace := "ns"

core/ledger/kvledger/txmgmt/txmgr/lockbasedtxmgr/txmgr_test.go

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@ import (
1010
"encoding/gob"
1111
"encoding/json"
1212
"fmt"
13-
"os"
1413
"strings"
1514
"testing"
1615

17-
"github.com/hyperledger/fabric/common/flogging"
1816
"github.com/hyperledger/fabric/common/ledger/testutil"
1917
"github.com/hyperledger/fabric/core/ledger"
2018
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/privacyenabledstate"
@@ -27,21 +25,13 @@ import (
2725
"github.com/stretchr/testify/assert"
2826
)
2927

30-
func TestMain(m *testing.M) {
31-
flogging.ActivateSpec(
32-
"lockbasedtxmgr,statevalidator,statebasedval,statecouchdb,valimpl,pvtstatepurgemgmt,valinternal=debug",
33-
)
34-
os.Exit(m.Run())
35-
}
36-
3728
func TestTxSimulatorWithNoExistingData(t *testing.T) {
3829
// run the tests for each environment configured in pkg_test.go
3930
for _, testEnv := range testEnvs {
4031
t.Logf("Running test for TestEnv = %s", testEnv.getName())
4132
testLedgerID := "testtxsimulatorwithnoexistingdata"
4233
testEnv.init(t, testLedgerID, nil)
4334
testTxSimulatorWithNoExistingData(t, testEnv)
44-
testEnv.cleanup()
4535
}
4636
}
4737

@@ -68,7 +58,6 @@ func testTxSimulatorWithNoExistingData(t *testing.T, env testEnv) {
6858
func TestTxSimulatorGetResults(t *testing.T) {
6959
testEnv := testEnvsMap[levelDBtestEnvName]
7060
testEnv.init(t, "testLedger", nil)
71-
defer testEnv.cleanup()
7261
txMgr := testEnv.getTxMgr()
7362
populateCollConfigForTest(t, txMgr.(*LockBasedTxMgr),
7463
[]collConfigkey{
@@ -126,7 +115,6 @@ func TestTxSimulatorWithExistingData(t *testing.T) {
126115
testLedgerID := "testtxsimulatorwithexistingdata"
127116
testEnv.init(t, testLedgerID, nil)
128117
testTxSimulatorWithExistingData(t, testEnv)
129-
testEnv.cleanup()
130118
})
131119
}
132120
}
@@ -179,7 +167,6 @@ func TestTxValidation(t *testing.T) {
179167
testLedgerID := "testtxvalidation"
180168
testEnv.init(t, testLedgerID, nil)
181169
testTxValidation(t, testEnv)
182-
testEnv.cleanup()
183170
}
184171
}
185172

@@ -266,7 +253,6 @@ func TestTxPhantomValidation(t *testing.T) {
266253
testLedgerID := "testtxphantomvalidation"
267254
testEnv.init(t, testLedgerID, nil)
268255
testTxPhantomValidation(t, testEnv)
269-
testEnv.cleanup()
270256
}
271257
}
272258

@@ -336,27 +322,22 @@ func TestIterator(t *testing.T) {
336322
testLedgerID := "testiterator.1"
337323
testEnv.init(t, testLedgerID, nil)
338324
testIterator(t, testEnv, 10, 2, 7)
339-
testEnv.cleanup()
340325

341326
testLedgerID = "testiterator.2"
342327
testEnv.init(t, testLedgerID, nil)
343328
testIterator(t, testEnv, 10, 1, 11)
344-
testEnv.cleanup()
345329

346330
testLedgerID = "testiterator.3"
347331
testEnv.init(t, testLedgerID, nil)
348332
testIterator(t, testEnv, 10, 0, 0)
349-
testEnv.cleanup()
350333

351334
testLedgerID = "testiterator.4"
352335
testEnv.init(t, testLedgerID, nil)
353336
testIterator(t, testEnv, 10, 5, 0)
354-
testEnv.cleanup()
355337

356338
testLedgerID = "testiterator.5"
357339
testEnv.init(t, testLedgerID, nil)
358340
testIterator(t, testEnv, 10, 0, 5)
359-
testEnv.cleanup()
360341
}
361342
}
362343

@@ -432,7 +413,6 @@ func TestIteratorPaging(t *testing.T) {
432413
nextStartKey = testIteratorPaging(t, testEnv, 10, nextStartKey, "key_007", int32(2), returnKeys)
433414
returnKeys = []string{"key_006"}
434415
testIteratorPaging(t, testEnv, 10, nextStartKey, "key_007", int32(2), returnKeys)
435-
testEnv.cleanup()
436416
}
437417
}
438418

@@ -497,7 +477,6 @@ func TestIteratorWithDeletes(t *testing.T) {
497477
testLedgerID := "testiteratorwithdeletes"
498478
testEnv.init(t, testLedgerID, nil)
499479
testIteratorWithDeletes(t, testEnv)
500-
testEnv.cleanup()
501480
}
502481
}
503482

@@ -539,7 +518,6 @@ func TestTxValidationWithItr(t *testing.T) {
539518
testLedgerID := "testtxvalidationwithitr"
540519
testEnv.init(t, testLedgerID, nil)
541520
testTxValidationWithItr(t, testEnv)
542-
testEnv.cleanup()
543521
}
544522
}
545523

@@ -604,7 +582,6 @@ func TestGetSetMultipeKeys(t *testing.T) {
604582
testLedgerID := "testgetsetmultipekeys"
605583
testEnv.init(t, testLedgerID, nil)
606584
testGetSetMultipeKeys(t, testEnv)
607-
testEnv.cleanup()
608585
}
609586
}
610587

@@ -667,7 +644,6 @@ func TestExecuteQuery(t *testing.T) {
667644
testLedgerID := "testexecutequery"
668645
testEnv.init(t, testLedgerID, nil)
669646
testExecuteQuery(t, testEnv)
670-
testEnv.cleanup()
671647
}
672648
}
673649
}
@@ -742,7 +718,6 @@ func TestExecutePaginatedQuery(t *testing.T) {
742718
testLedgerID := "testexecutepaginatedquery"
743719
testEnv.init(t, testLedgerID, nil)
744720
testExecutePaginatedQuery(t, testEnv)
745-
testEnv.cleanup()
746721
}
747722
}
748723
}
@@ -843,7 +818,6 @@ func TestValidateKey(t *testing.T) {
843818
if testEnv.getName() == couchDBtestEnvName {
844819
assert.Error(t, err)
845820
}
846-
testEnv.cleanup()
847821
}
848822
}
849823

@@ -852,7 +826,6 @@ func TestValidateKey(t *testing.T) {
852826
func TestTxSimulatorUnsupportedTx(t *testing.T) {
853827
testEnv := testEnvs[0]
854828
testEnv.init(t, "testtxsimulatorunsupportedtx", nil)
855-
defer testEnv.cleanup()
856829
txMgr := testEnv.getTxMgr()
857830
populateCollConfigForTest(t, txMgr.(*LockBasedTxMgr),
858831
[]collConfigkey{
@@ -907,7 +880,6 @@ func TestTxSimulatorQueryUnsupportedTx(t *testing.T) {
907880
testLedgerID := "testtxsimulatorunsupportedtxqueries"
908881
testEnv.init(t, testLedgerID, nil)
909882
testTxSimulatorQueryUnsupportedTx(t, testEnv)
910-
testEnv.cleanup()
911883
}
912884
}
913885
}
@@ -1010,7 +982,6 @@ func TestFindAndRemoveStalePvtData(t *testing.T) {
1010982
ledgerid := "TestFindAndRemoveStalePvtData"
1011983
testEnv := testEnvs[0]
1012984
testEnv.init(t, ledgerid, nil)
1013-
defer testEnv.cleanup()
1014985
db := testEnv.getVDB()
1015986

1016987
batch := privacyenabledstate.NewUpdateBatch()
@@ -1082,7 +1053,6 @@ func TestRemoveStaleAndCommitPvtDataOfOldBlocks(t *testing.T) {
10821053
for _, testEnv := range testEnvs {
10831054
t.Logf("Running test for TestEnv = %s", testEnv.getName())
10841055
testValidationAndCommitOfOldPvtData(t, testEnv)
1085-
testEnv.cleanup()
10861056
}
10871057
}
10881058

@@ -1187,7 +1157,6 @@ func testValidationAndCommitOfOldPvtData(t *testing.T, env testEnv) {
11871157
func TestTxSimulatorMissingPvtdata(t *testing.T) {
11881158
testEnv := testEnvs[0]
11891159
testEnv.init(t, "TestTxSimulatorUnsupportedTxQueries", nil)
1190-
defer testEnv.cleanup()
11911160

11921161
txMgr := testEnv.getTxMgr()
11931162
populateCollConfigForTest(t, txMgr.(*LockBasedTxMgr),
@@ -1233,7 +1202,6 @@ func TestRemoveStaleAndCommitPvtDataOfOldBlocksWithExpiry(t *testing.T) {
12331202
)
12341203
testEnv := testEnvs[0]
12351204
testEnv.init(t, ledgerid, btlPolicy)
1236-
defer testEnv.cleanup()
12371205

12381206
txMgr := testEnv.getTxMgr()
12391207
populateCollConfigForTest(t, txMgr.(*LockBasedTxMgr),
@@ -1341,7 +1309,6 @@ func TestDeleteOnCursor(t *testing.T) {
13411309
cID := "cid"
13421310
env := testEnvs[0]
13431311
env.init(t, "TestDeleteOnCursor", nil)
1344-
defer env.cleanup()
13451312

13461313
txMgr := env.getTxMgr()
13471314
txMgrHelper := newTxMgrTestHelper(t, txMgr)
@@ -1394,7 +1361,6 @@ func TestTxSimulatorMissingPvtdataExpiry(t *testing.T) {
13941361
},
13951362
)
13961363
testEnv.init(t, ledgerid, btlPolicy)
1397-
defer testEnv.cleanup()
13981364

13991365
txMgr := testEnv.getTxMgr()
14001366
populateCollConfigForTest(t, txMgr.(*LockBasedTxMgr), []collConfigkey{{"ns", "coll"}}, version.NewHeight(1, 1))
@@ -1433,7 +1399,6 @@ func TestTxWithPubMetadata(t *testing.T) {
14331399
testLedgerID := "testtxwithpubmetadata"
14341400
testEnv.init(t, testLedgerID, nil)
14351401
testTxWithPubMetadata(t, testEnv)
1436-
testEnv.cleanup()
14371402
}
14381403
}
14391404

@@ -1491,7 +1456,6 @@ func TestTxWithPvtdataMetadata(t *testing.T) {
14911456
t.Logf("Running test for TestEnv = %s", testEnv.getName())
14921457
testEnv.init(t, ledgerid, btlPolicy)
14931458
testTxWithPvtdataMetadata(t, testEnv, ns, coll)
1494-
testEnv.cleanup()
14951459
}
14961460
}
14971461

0 commit comments

Comments
 (0)