forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apiclient_whitebox_test.go
81 lines (73 loc) · 2.04 KB
/
apiclient_whitebox_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright 2018 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package api
import (
"context"
"fmt"
"net"
"regexp"
"time"
"github.com/juju/testing"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
jtesting "github.com/juju/juju/testing"
)
type apiclientWhiteboxSuite struct {
testing.IsolationSuite
}
var _ = gc.Suite(&apiclientWhiteboxSuite{})
func (s *apiclientWhiteboxSuite) TestDialWebsocketMultiCancelled(c *gc.C) {
ctx := context.TODO()
ctx, cancel := context.WithCancel(ctx)
started := make(chan struct{})
go func() {
select {
case <-started:
case <-time.After(jtesting.LongWait):
c.Fatalf("timed out waiting %s for started", jtesting.LongWait)
}
<-time.After(10 * time.Millisecond)
if cancel != nil {
c.Logf("cancelling")
cancel()
}
}()
listen, err := net.Listen("tcp4", ":0")
c.Assert(err, jc.ErrorIsNil)
addr := listen.Addr().String()
c.Logf("listening at: %s", addr)
// Note that we Listen, but we never Accept
close(started)
info := &Info{
Addrs: []string{addr},
}
opts := DialOpts{
DialAddressInterval: 50 * time.Millisecond,
RetryDelay: 40 * time.Millisecond,
Timeout: 100 * time.Millisecond,
DialTimeout: 100 * time.Millisecond,
}
// Close before we connect
listen.Close()
_, err = dialAPI(ctx, info, opts)
c.Check(err, gc.ErrorMatches, fmt.Sprintf("dial tcp %s:.*", regexp.QuoteMeta(addr)))
}
func (s *apiclientWhiteboxSuite) TestDialWebsocketMultiClosed(c *gc.C) {
listen, err := net.Listen("tcp4", ":0")
c.Assert(err, jc.ErrorIsNil)
addr := listen.Addr().String()
c.Logf("listening at: %s", addr)
// Note that we Listen, but we never Accept
info := &Info{
Addrs: []string{addr},
}
opts := DialOpts{
DialAddressInterval: 1 * time.Second,
RetryDelay: 1 * time.Second,
Timeout: 2 * time.Second,
DialTimeout: 3 * time.Second,
}
listen.Close()
_, _, err = DialAPI(info, opts)
c.Check(err, gc.ErrorMatches, fmt.Sprintf("unable to connect to API: dial tcp %s:.*", regexp.QuoteMeta(addr)))
}