/
testutil_test.go
59 lines (46 loc) · 1.3 KB
/
testutil_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
package ndt5_test
import (
"context"
"errors"
"net"
)
const UserAgent = "ndt5-client-go-testing/0.1.0"
var ErrMocked = errors.New("mocked error")
type RecordParametersDialer struct {
Address string
Network string
}
func (d *RecordParametersDialer) Dial(network, address string) (net.Conn, error) {
return d.DialContext(context.Background(), network, address)
}
func (d *RecordParametersDialer) DialContext(
ctx context.Context, network, address string) (net.Conn, error) {
d.Network = network
d.Address = address
conn, _ := net.Pipe()
return conn, nil
}
type AlwaysFailingDialer struct{}
func (d *AlwaysFailingDialer) Dial(network, address string) (net.Conn, error) {
return d.DialContext(context.Background(), network, address)
}
func (d *AlwaysFailingDialer) DialContext(
ctx context.Context, network, address string) (net.Conn, error) {
return nil, ErrMocked
}
type PipeDialer struct {
ServerConn net.Conn
ClientConn net.Conn
}
func NewPipeDialer() *PipeDialer {
d := new(PipeDialer)
d.ClientConn, d.ServerConn = net.Pipe()
return d
}
func (d *PipeDialer) Dial(network, address string) (net.Conn, error) {
return d.DialContext(context.Background(), network, address)
}
func (d *PipeDialer) DialContext(
ctx context.Context, network, address string) (net.Conn, error) {
return d.ClientConn, nil
}