Skip to content

Commit

Permalink
FAB-1758 Fix CouchDB tests when run in parallel
Browse files Browse the repository at this point in the history
When various packages that utilize CouchDB were tested
all together, we received intermitent test failures
due to collisions in test envionment.  Fixed by scoping
each test package to different CouchDB databases (chains).

Change-Id: I93b9043f7a5a60b1052d74e9f5f877f9e19a29d9
Signed-off-by: denyeart <enyeart@us.ibm.com>
  • Loading branch information
denyeart committed Jan 20, 2017
1 parent add2776 commit 963042a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ func TestBasicRW(t *testing.T) {
if ledgerconfig.IsCouchDBEnabled() == true {

env := NewTestVDBEnv(t)
defer env.Cleanup()
env.Cleanup("TestDB")
defer env.Cleanup("TestDB")
commontests.TestBasicRW(t, env.DBProvider)

}
Expand All @@ -51,7 +52,10 @@ func TestMultiDBBasicRW(t *testing.T) {
if ledgerconfig.IsCouchDBEnabled() == true {

env := NewTestVDBEnv(t)
defer env.Cleanup()
env.Cleanup("TestDB1")
env.Cleanup("TestDB2")
defer env.Cleanup("TestDB1")
defer env.Cleanup("TestDB2")
commontests.TestMultiDBBasicRW(t, env.DBProvider)

}
Expand All @@ -60,7 +64,8 @@ func TestMultiDBBasicRW(t *testing.T) {
/* TODO add delete support in couchdb and then convert key value of nil to a couch delete. This will resolve TestDeletes
func TestDeletes(t *testing.T) {
env := NewTestVDBEnv(t)
defer env.Cleanup()
env.Cleanup("TestDB")
defer env.Cleanup("TestDB")
commontests.TestDeletes(t, env.DBProvider)
}
*/
Expand All @@ -69,7 +74,8 @@ func TestIterator(t *testing.T) {
if ledgerconfig.IsCouchDBEnabled() == true {

env := NewTestVDBEnv(t)
defer env.Cleanup()
env.Cleanup("TestDB")
defer env.Cleanup("TestDB")
commontests.TestIterator(t, env.DBProvider)

}
Expand Down Expand Up @@ -113,7 +119,8 @@ func TestQuery(t *testing.T) {
if ledgerconfig.IsCouchDBEnabled() == true {

env := NewTestVDBEnv(t)
defer env.Cleanup()
env.Cleanup("TestDB")
defer env.Cleanup("TestDB")
commontests.TestQuery(t, env.DBProvider)

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package statecouchdb

import (
"strings"
"testing"

"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb"
Expand All @@ -29,28 +30,26 @@ var badConnectURL = "localhost:5990"
var username = ""
var password = ""

// TestVDBEnv provides a level db backed versioned db for testing
// TestVDBEnv provides a couch db backed versioned db for testing
type TestVDBEnv struct {
t testing.TB
DBProvider statedb.VersionedDBProvider
}

// NewTestVDBEnv instantiates and new level db backed TestVDB
// NewTestVDBEnv instantiates and new couch db backed TestVDB
func NewTestVDBEnv(t testing.TB) *TestVDBEnv {
t.Logf("Creating new TestVDBEnv")

dbProvider, _ := NewVersionedDBProvider()
testVDBEnv := &TestVDBEnv{t, dbProvider}
testVDBEnv.Cleanup()
// No cleanup for new test environment. Need to cleanup per test for each DB used in the test.
return testVDBEnv
}

// Cleanup drops the test couch databases and closes the db provider
func (env *TestVDBEnv) Cleanup() {
func (env *TestVDBEnv) Cleanup(dbName string) {
env.t.Logf("Cleaningup TestVDBEnv")
cleanupDB("testdb")
cleanupDB("testdb1")
cleanupDB("testdb2")
cleanupDB(strings.ToLower(dbName))
env.DBProvider.Close()

}
Expand Down
12 changes: 10 additions & 2 deletions core/ledger/kvledger/txmgmt/txmgr/commontests/pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ func init() {

}

///////////// LevelDB Environment //////////////

type levelDBLockBasedEnv struct {
testDBEnv *stateleveldb.TestVDBEnv
testDB statedb.VersionedDB
Expand Down Expand Up @@ -90,6 +92,10 @@ func (env *levelDBLockBasedEnv) cleanup() {
defer env.testDBEnv.Cleanup()
}

///////////// CouchDB Environment //////////////

var couchTestChainID = "TxmgrTestDB"

type couchDBLockBasedEnv struct {
testDBEnv *statecouchdb.TestVDBEnv
testDB statedb.VersionedDB
Expand All @@ -104,7 +110,7 @@ func (env *couchDBLockBasedEnv) init(t *testing.T) {
viper.Set("peer.fileSystemPath", "/tmp/fabric/ledgertests")
viper.Set("ledger.state.couchDBConfig.couchDBAddress", "127.0.0.1:5984")
testDBEnv := statecouchdb.NewTestVDBEnv(t)
testDB, err := testDBEnv.DBProvider.GetDBHandle("TestDB")
testDB, err := testDBEnv.DBProvider.GetDBHandle(couchTestChainID)
testutil.AssertNoError(t, err, "")

txMgr := lockbasedtxmgr.NewLockBasedTxMgr(testDB)
Expand All @@ -123,9 +129,11 @@ func (env *couchDBLockBasedEnv) getVDB() statedb.VersionedDB {

func (env *couchDBLockBasedEnv) cleanup() {
defer env.txmgr.Shutdown()
defer env.testDBEnv.Cleanup()
defer env.testDBEnv.Cleanup(couchTestChainID)
}

//////////// txMgrTestHelper /////////////

type txMgrTestHelper struct {
t *testing.T
txMgr txmgr.TxMgr
Expand Down
2 changes: 1 addition & 1 deletion core/ledger/util/couchdb/couchdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
//Basic setup to test couch
var connectURL = "localhost:5984"
var badConnectURL = "localhost:5990"
var database = "testdb1"
var database = "couch_util_testdb"
var username = ""
var password = ""

Expand Down

0 comments on commit 963042a

Please sign in to comment.