Skip to content

Commit

Permalink
[FAB-2388] Fix intermittent CouchDB failures
Browse files Browse the repository at this point in the history
https://jira.hyperledger.org/browse/FAB-2388

Instead of creating/deleting same database multiple times, the
unit-tests now use different databases for each test. This should avoid
hitting the database deletion async issue in Couch 2.x.

Activating statecouchdb unit-tests.

Change-Id: Iba6eff9e259098971cf9063f467350a478ff2dab
Signed-off-by: Adnan Choudhury <adnan.choudhury@itpeoplecorp.com>
  • Loading branch information
adnan-c authored and denyeart committed Feb 27, 2017
1 parent 9a09ac0 commit bdd972f
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 47 deletions.
12 changes: 6 additions & 6 deletions core/ledger/kvledger/txmgmt/statedb/commontests/test_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (

// TestBasicRW tests basic read-write
func TestBasicRW(t *testing.T, dbProvider statedb.VersionedDBProvider) {
db, err := dbProvider.GetDBHandle("TestDB")
db, err := dbProvider.GetDBHandle("testbasicrw")
testutil.AssertNoError(t, err, "")

// Test retrieval of non-existent key - returns nil rather than error
Expand Down Expand Up @@ -61,10 +61,10 @@ func TestBasicRW(t *testing.T, dbProvider statedb.VersionedDBProvider) {

// TestMultiDBBasicRW tests basic read-write on multiple dbs
func TestMultiDBBasicRW(t *testing.T, dbProvider statedb.VersionedDBProvider) {
db1, err := dbProvider.GetDBHandle("TestDB1")
db1, err := dbProvider.GetDBHandle("testmultidbbasicrw")
testutil.AssertNoError(t, err, "")

db2, err := dbProvider.GetDBHandle("TestDB2")
db2, err := dbProvider.GetDBHandle("testmultidbbasicrw2")
testutil.AssertNoError(t, err, "")

batch1 := statedb.NewUpdateBatch()
Expand Down Expand Up @@ -100,7 +100,7 @@ func TestMultiDBBasicRW(t *testing.T, dbProvider statedb.VersionedDBProvider) {

// TestDeletes tests deteles
func TestDeletes(t *testing.T, dbProvider statedb.VersionedDBProvider) {
db, err := dbProvider.GetDBHandle("TestDB")
db, err := dbProvider.GetDBHandle("testdeletes")
testutil.AssertNoError(t, err, "")

batch := statedb.NewUpdateBatch()
Expand Down Expand Up @@ -135,7 +135,7 @@ func TestDeletes(t *testing.T, dbProvider statedb.VersionedDBProvider) {

// TestIterator tests the iterator
func TestIterator(t *testing.T, dbProvider statedb.VersionedDBProvider) {
db, err := dbProvider.GetDBHandle("TestDB")
db, err := dbProvider.GetDBHandle("testiterator")
testutil.AssertNoError(t, err, "")
db.Open()
defer db.Close()
Expand Down Expand Up @@ -178,7 +178,7 @@ func testItr(t *testing.T, itr statedb.ResultsIterator, expectedKeys []string) {

// TestQuery tests queries
func TestQuery(t *testing.T, dbProvider statedb.VersionedDBProvider) {
db, err := dbProvider.GetDBHandle("TestDB")
db, err := dbProvider.GetDBHandle("testquery")
testutil.AssertNoError(t, err, "")
db.Open()
defer db.Close()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,20 @@ func TestMain(m *testing.M) {

//call a helper method to load the core.yaml, will be used to detect if CouchDB is enabled
ledgertestutil.SetupCoreYAMLConfig("./../../../../../../peer")

viper.Set("ledger.state.couchDBConfig.couchDBAddress", "127.0.0.1:5984")
viper.Set("ledger.state.stateDatabase", "CouchDB")
viper.Set("ledger.state.couchDBConfig.couchDBAddress", "couchdb:5984")
viper.Set("peer.fileSystemPath", "/tmp/fabric/ledgertests/kvledger/txmgmt/statedb/statecouchdb")

os.Exit(m.Run())
result := m.Run()
viper.Set("ledger.state.stateDatabase", "goleveldb")
os.Exit(result)
}

func TestBasicRW(t *testing.T) {
if ledgerconfig.IsCouchDBEnabled() == true {

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

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

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

}
Expand All @@ -67,8 +68,8 @@ func TestMultiDBBasicRW(t *testing.T) {
func TestDeletes(t *testing.T) {
if ledgerconfig.IsCouchDBEnabled() == true {
env := NewTestVDBEnv(t)
env.Cleanup("TestDB")
defer env.Cleanup("TestDB")
env.Cleanup("testdeletes")
defer env.Cleanup("testdeletes")
commontests.TestDeletes(t, env.DBProvider)
}
}
Expand All @@ -77,8 +78,8 @@ func TestIterator(t *testing.T) {
if ledgerconfig.IsCouchDBEnabled() == true {

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

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

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

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (
)

//Basic setup to test couch
var connectURL = "localhost:5984"
var badConnectURL = "localhost:5990"
var connectURL = "couchdb:5984"
var badConnectURL = "couchdb:5990"
var username = ""
var password = ""

Expand Down
46 changes: 25 additions & 21 deletions core/ledger/util/couchdb/couchdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,17 @@ import (
//Basic setup to test couch
var connectURL = "couchdb:5984"
var badConnectURL = "couchdb:5990"
var database = "couch_util_testdb"
var username = ""
var password = ""

func cleanup() error {
func cleanup(database string) error {
//create a new connection
couchInstance, _ := CreateCouchInstance(connectURL, username, password)
db, err := CreateCouchDatabase(*couchInstance, database)
couchInstance, err := CreateCouchInstance(connectURL, username, password)
if err != nil {
fmt.Println("Unexpected error", err)
return err
}
db := CouchDatabase{couchInstance: *couchInstance, dbName: database}
//drop the test database
db.DropDatabase()
return nil
Expand All @@ -62,8 +61,7 @@ var assetJSON = []byte(`{"asset_name":"marble1","color":"blue","size":"35","owne

func TestMain(m *testing.M) {
ledgertestutil.SetupCoreYAMLConfig("./../../../../peer")
//TODO CouchDB tests are disabled. Re-enable once intermittent failures are resolved.
//viper.Set("ledger.state.stateDatabase", "CouchDB")
viper.Set("ledger.state.stateDatabase", "CouchDB")
result := m.Run()
viper.Set("ledger.state.stateDatabase", "goleveldb")
os.Exit(result)
Expand Down Expand Up @@ -92,9 +90,10 @@ func TestDBCreateSaveWithoutRevision(t *testing.T) {

if ledgerconfig.IsCouchDBEnabled() == true {

err := cleanup()
database := "testdbcreatesavewithoutrevision"
err := cleanup(database)
testutil.AssertNoError(t, err, fmt.Sprintf("Error when trying to cleanup Error: %s", err))
defer cleanup()
defer cleanup(database)

if err == nil {
//create a new instance and database object
Expand Down Expand Up @@ -162,9 +161,10 @@ func TestDBCreateDatabaseAndPersist(t *testing.T) {

if ledgerconfig.IsCouchDBEnabled() == true {

err := cleanup()
database := "testdbcreatedatabaseandpersist"
err := cleanup(database)
testutil.AssertNoError(t, err, fmt.Sprintf("Error when trying to cleanup Error: %s", err))
defer cleanup()
defer cleanup(database)

if err == nil {
//create a new instance and database object
Expand Down Expand Up @@ -250,9 +250,10 @@ func TestDBBadJSON(t *testing.T) {

if ledgerconfig.IsCouchDBEnabled() == true {

err := cleanup()
database := "testdbbadjson"
err := cleanup(database)
testutil.AssertNoError(t, err, fmt.Sprintf("Error when trying to cleanup Error: %s", err))
defer cleanup()
defer cleanup(database)

if err == nil {

Expand Down Expand Up @@ -286,9 +287,10 @@ func TestPrefixScan(t *testing.T) {
if !ledgerconfig.IsCouchDBEnabled() {
return
}
err := cleanup()
database := "testprefixscan"
err := cleanup(database)
testutil.AssertNoError(t, err, fmt.Sprintf("Error when trying to cleanup Error: %s", err))
defer cleanup()
defer cleanup(database)

if err == nil {
//create a new instance and database object
Expand Down Expand Up @@ -339,17 +341,17 @@ func TestPrefixScan(t *testing.T) {
//Retrieve the info for the new database and make sure the name matches
_, _, errdbinfo := db.GetDatabaseInfo()
testutil.AssertError(t, errdbinfo, fmt.Sprintf("Error should have been thrown for missing database"))

}
}

func TestDBSaveAttachment(t *testing.T) {

if ledgerconfig.IsCouchDBEnabled() == true {

err := cleanup()
database := "testdbsaveattachment"
err := cleanup(database)
testutil.AssertNoError(t, err, fmt.Sprintf("Error when trying to cleanup Error: %s", err))
defer cleanup()
defer cleanup(database)

if err == nil {

Expand Down Expand Up @@ -390,9 +392,10 @@ func TestDBDeleteDocument(t *testing.T) {

if ledgerconfig.IsCouchDBEnabled() == true {

err := cleanup()
database := "testdbdeletedocument"
err := cleanup(database)
testutil.AssertNoError(t, err, fmt.Sprintf("Error when trying to cleanup Error: %s", err))
defer cleanup()
defer cleanup(database)

if err == nil {
//create a new instance and database object
Expand Down Expand Up @@ -427,9 +430,10 @@ func TestDBDeleteNonExistingDocument(t *testing.T) {

if ledgerconfig.IsCouchDBEnabled() == true {

err := cleanup()
database := "testdbdeletenonexistingdocument"
err := cleanup(database)
testutil.AssertNoError(t, err, fmt.Sprintf("Error when trying to cleanup Error: %s", err))
defer cleanup()
defer cleanup(database)

if err == nil {
//create a new instance and database object
Expand Down
5 changes: 3 additions & 2 deletions core/ledger/util/couchdb/couchdbutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ import (
func TestCreateCouchDBConnectionAndDB(t *testing.T) {
if ledgerconfig.IsCouchDBEnabled() == true {

cleanup()
defer cleanup()
database := "testcreatecouchdbconnectionanddb"
cleanup(database)
defer cleanup(database)
//create a new connection
couchInstance, err := CreateCouchInstance(connectURL, "", "")
testutil.AssertNoError(t, err, fmt.Sprintf("Error when trying to CreateCouchInstance"))
Expand Down

0 comments on commit bdd972f

Please sign in to comment.