Add interface test for adding a duplicate driver.

This commit adds an interface test to ensure that attempting to add a
duplicate driver for a given database type does not overwrite an existing
one.
This commit is contained in:
Dave Collins 2013-10-13 19:28:40 -05:00
parent 562294f938
commit 53ea2cf0ba

View file

@ -5,6 +5,7 @@
package btcdb_test
import (
"fmt"
"github.com/conformal/btcdb"
"testing"
)
@ -52,3 +53,43 @@ func TestEmptyDB(t *testing.T) {
teardown()
}
}
// TestAddDuplicateDriver ensures that adding a duplicate driver does not
// overwrite an existing one.
func TestAddDuplicateDriver(t *testing.T) {
supportedDBs := btcdb.SupportedDBs()
if len(supportedDBs) == 0 {
t.Errorf("TestAddDuplicateDriver: No backends to test")
return
}
dbType := supportedDBs[0]
// bogusCreateDB is a function which acts as a bogus create and open
// driver function and intentionally returns a failure that can be
// detected if the interface allows a duplicate driver to overwrite an
// existing one.
bogusCreateDB := func(string) (btcdb.Db, error) {
return nil, fmt.Errorf("duplicate driver allowed for database "+
"type [%v]", dbType)
}
// Create a driver that tries to replace an existing one. Set its
// create and open functions to a function that causes a test failure if
// they are invoked.
driver := btcdb.DriverDB{
DbType: dbType,
Create: bogusCreateDB,
Open: bogusCreateDB,
}
btcdb.AddDBDriver(driver)
// Ensure creating a database of the type that we tried to replace
// doesn't fail (if it does, it indicates the driver was erroneously
// replaced).
_, teardown, err := createDB(dbType, "dupdrivertest", true)
if err != nil {
t.Errorf("TestAddDuplicateDriver: %v", err)
return
}
teardown()
}