Skip to content

Commit f250aff

Browse files
committed
Fix the WaitForSSH interrupt tests.
They were sending a message to a buffered channel, but that meant if they ever got a second attempt at sending to the channel they might block. And that could happen because we would reset the 'look for addresseses' timer to re-trigger in 1ms. And if there was any significant hiccup in any of the Addresses code, the timer would be ready to fire again as soon as the overall loop came back. In testing, adding a time.Sleep(3ms) in the Addresses call was able to cause up to 14 calls to Addresses to be made before it noticed the interrupted channel. (select is not fair on each possibility.) By closing the channel we can guarantee they never deadlock.
1 parent 5779135 commit f250aff

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

provider/common/bootstrap_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -556,8 +556,8 @@ func (s *BootstrapSuite) TestWaitSSHTimesOutWaitingForAddresses(c *gc.C) {
556556

557557
func (s *BootstrapSuite) TestWaitSSHKilledWaitingForAddresses(c *gc.C) {
558558
ctx := cmdtesting.Context(c)
559-
interrupted := make(chan os.Signal, 1)
560-
interrupted <- os.Interrupt
559+
interrupted := make(chan os.Signal)
560+
close(interrupted)
561561
_, err := common.WaitSSH(
562562
ctx.Stderr, interrupted, ssh.DefaultClient, "/bin/true", neverAddresses{}, s.callCtx, testSSHTimeout,
563563
common.DefaultHostSSHOptions,
@@ -633,7 +633,10 @@ func (i *interruptOnDial) Addresses(ctx context.ProviderCallContext) ([]network.
633633
if !i.returned {
634634
i.returned = true
635635
} else {
636-
i.interrupted <- os.Interrupt
636+
if i.interrupted != nil {
637+
close(i.interrupted)
638+
i.interrupted = nil
639+
}
637640
}
638641
return network.NewAddresses(i.name), nil
639642
}
@@ -642,7 +645,7 @@ func (s *BootstrapSuite) TestWaitSSHKilledWaitingForDial(c *gc.C) {
642645
ctx := cmdtesting.Context(c)
643646
timeout := testSSHTimeout
644647
timeout.Timeout = 1 * time.Minute
645-
interrupted := make(chan os.Signal, 1)
648+
interrupted := make(chan os.Signal)
646649
_, err := common.WaitSSH(
647650
ctx.Stderr, interrupted, ssh.DefaultClient, "", &interruptOnDial{name: "0.1.2.3", interrupted: interrupted}, s.callCtx, timeout,
648651
common.DefaultHostSSHOptions,

0 commit comments

Comments
 (0)