From f56dab06765ff5c9fec217b993a789431103698a Mon Sep 17 00:00:00 2001 From: David Gageot Date: Sat, 26 Dec 2015 13:36:02 +0100 Subject: [PATCH] Make test 10s faster Signed-off-by: David Gageot --- .../drivers/plugin/localbinary/plugin.go | 9 +++++++-- .../drivers/plugin/localbinary/plugin_test.go | 20 ++++++++++--------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/libmachine/drivers/plugin/localbinary/plugin.go b/libmachine/drivers/plugin/localbinary/plugin.go index 81c91e02b1..4c0685eec9 100644 --- a/libmachine/drivers/plugin/localbinary/plugin.go +++ b/libmachine/drivers/plugin/localbinary/plugin.go @@ -73,6 +73,7 @@ type Plugin struct { MachineName string addrCh chan string stopCh chan bool + timeout time.Duration } type Executor struct { @@ -230,13 +231,17 @@ func (lbp *Plugin) Serve() error { func (lbp *Plugin) Address() (string, error) { if lbp.Addr == "" { + if lbp.timeout == 0 { + lbp.timeout = defaultTimeout + } + select { case lbp.Addr = <-lbp.addrCh: log.Debugf("Plugin server listening at address %s", lbp.Addr) close(lbp.addrCh) return lbp.Addr, nil - case <-time.After(defaultTimeout): - return "", fmt.Errorf("Failed to dial the plugin server in %s", defaultTimeout) + case <-time.After(lbp.timeout): + return "", fmt.Errorf("Failed to dial the plugin server in %s", lbp.timeout) } } return lbp.Addr, nil diff --git a/libmachine/drivers/plugin/localbinary/plugin_test.go b/libmachine/drivers/plugin/localbinary/plugin_test.go index 87323ed9ac..8d5dae09a7 100644 --- a/libmachine/drivers/plugin/localbinary/plugin_test.go +++ b/libmachine/drivers/plugin/localbinary/plugin_test.go @@ -10,6 +10,7 @@ import ( "os" "github.com/docker/machine/libmachine/log" + "github.com/stretchr/testify/assert" ) type FakeExecutor struct { @@ -56,15 +57,16 @@ func TestLocalBinaryPluginAddressTimeout(t *testing.T) { if testing.Short() { t.Skip("Skipping timeout test") } - lbp := &Plugin{} - lbp.addrCh = make(chan string, 1) - go func() { - _, err := lbp.Address() - if err == nil { - t.Fatalf("Expected to get a timeout error, instead got %s", err) - } - }() - time.Sleep(defaultTimeout + 1) + + lbp := &Plugin{ + addrCh: make(chan string, 1), + timeout: 1 * time.Second, + } + + addr, err := lbp.Address() + + assert.Empty(t, addr) + assert.EqualError(t, err, "Failed to dial the plugin server in 1s") } func TestLocalBinaryPluginClose(t *testing.T) {