libnetwork to make use of boltdb ConnectionTimeout to avoid deadlock

Signed-off-by: Madhu Venugopal <madhu@docker.com>
This commit is contained in:
Madhu Venugopal 2015-09-22 10:49:15 -07:00
parent dd6b8cb408
commit 27397dcbdf
2 changed files with 31 additions and 3 deletions

View File

@ -3,6 +3,7 @@ package libnetwork
import (
"encoding/json"
"fmt"
"time"
log "github.com/Sirupsen/logrus"
"github.com/docker/libkv/store"
@ -11,13 +12,15 @@ import (
)
var (
defaultBoltTimeout = 3 * time.Second
defaultLocalStoreConfig = config.DatastoreCfg{
Embedded: true,
Client: config.DatastoreClientCfg{
Provider: "boltdb",
Address: defaultPrefix + "/boltdb.db",
Config: &store.Config{
Bucket: "libnetwork",
Bucket: "libnetwork",
ConnectionTimeout: defaultBoltTimeout,
},
},
}

View File

@ -5,6 +5,7 @@ import (
"io/ioutil"
"os"
"testing"
"time"
"github.com/docker/libkv/store"
"github.com/docker/libnetwork/config"
@ -34,7 +35,9 @@ func TestBoltdbBackend(t *testing.T) {
defer os.Remove(defaultLocalStoreConfig.Client.Address)
testLocalBackend(t, "", "", nil)
defer os.Remove("/tmp/boltdb.db")
testLocalBackend(t, "boltdb", "/tmp/boltdb.db", &store.Config{Bucket: "testBackend"})
config := &store.Config{Bucket: "testBackend", ConnectionTimeout: 3 * time.Second}
testLocalBackend(t, "boltdb", "/tmp/boltdb.db", config)
}
func testLocalBackend(t *testing.T, provider, url string, storeConfig *store.Config) {
@ -91,6 +94,28 @@ func OptionBoltdbWithRandomDBFile() ([]config.Option, error) {
cfgOptions := []config.Option{}
cfgOptions = append(cfgOptions, config.OptionLocalKVProvider("boltdb"))
cfgOptions = append(cfgOptions, config.OptionLocalKVProviderURL(tmp.Name()))
cfgOptions = append(cfgOptions, config.OptionLocalKVProviderConfig(&store.Config{Bucket: "testBackend"}))
sCfg := &store.Config{Bucket: "testBackend", ConnectionTimeout: 3 * time.Second}
cfgOptions = append(cfgOptions, config.OptionLocalKVProviderConfig(sCfg))
return cfgOptions, nil
}
func TestLocalStoreLockTimeout(t *testing.T) {
cfgOptions, err := OptionBoltdbWithRandomDBFile()
if err != nil {
t.Fatalf("Error getting random boltdb configs %v", err)
}
ctrl, err := New(cfgOptions...)
if err != nil {
t.Fatalf("Error new controller: %v", err)
}
// Use the same boltdb file without closing the previous controller
_, err = New(cfgOptions...)
if err == nil {
t.Fatalf("Multiple boldtdb connection must fail")
}
store := ctrl.(*controller).localStore.KVStore()
if store == nil {
t.Fatalf("Invalid LocalStore access connection")
}
store.Close()
}