-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconn_test.go
154 lines (146 loc) · 3.49 KB
/
conn_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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright (c) Jim Lambert
// SPDX-License-Identifier: MIT
package gldap
import (
"context"
"net"
"testing"
"github.com/hashicorp/go-hclog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_newConn(t *testing.T) {
testCtx := context.Background()
server, client := net.Pipe()
t.Cleanup(func() { server.Close(); client.Close() })
buf := testSafeBuf(t)
testLogger := hclog.New(&hclog.LoggerOptions{
Name: "test",
Output: buf,
})
tests := map[string]struct {
ctx context.Context
id int
netConn net.Conn
logger hclog.Logger
router *Mux
want *conn
wantErr bool
wantErrIs error
wantErrContains string
}{
"missing-ctx": {
wantErr: true,
wantErrIs: ErrInvalidParameter,
wantErrContains: "missing shutdown context",
},
"missing-id": {
ctx: testCtx,
wantErr: true,
wantErrIs: ErrInvalidParameter,
wantErrContains: "missing connection id",
},
"missing-conn": {
ctx: testCtx,
id: 1,
wantErr: true,
wantErrIs: ErrInvalidParameter,
wantErrContains: "missing connection",
},
"missing-logger": {
ctx: testCtx,
id: 1,
netConn: server,
wantErr: true,
wantErrIs: ErrInvalidParameter,
wantErrContains: "missing logger",
},
"missing-router": {
ctx: testCtx,
id: 1,
netConn: server,
logger: testLogger,
wantErr: true,
wantErrIs: ErrInvalidParameter,
wantErrContains: "missing router",
},
"success": {
ctx: testCtx,
id: 1,
netConn: server,
logger: testLogger,
router: &Mux{},
want: &conn{
shutdownCtx: testCtx,
connID: 1,
netConn: server,
logger: testLogger,
router: &Mux{},
},
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
got, err := newConn(tc.ctx, tc.id, tc.netConn, tc.logger, tc.router)
if tc.wantErr {
require.Error(err)
if tc.wantErrIs != nil {
assert.ErrorIs(err, tc.wantErrIs)
}
if tc.wantErrContains != "" {
assert.Contains(err.Error(), tc.wantErrContains)
}
return
}
require.NoError(err)
assert.NotNil(got)
assert.NotEmpty(got.reader)
assert.NotEmpty(got.writer)
tc.want.reader = got.reader
tc.want.writer = got.writer
assert.Equal(tc.want, got)
})
}
}
func Test_initConn(t *testing.T) {
server, client := net.Pipe()
t.Cleanup(func() { server.Close(); client.Close() })
tests := map[string]struct {
c *conn
netConn net.Conn
wantErr bool
wantErrIs error
wantErrContains string
}{
"missing-conn": {
c: &conn{},
wantErr: true,
wantErrIs: ErrInvalidParameter,
wantErrContains: "missing net conn",
},
"success": {
c: &conn{},
netConn: server,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
err := tc.c.initConn(tc.netConn)
if tc.wantErr {
require.Error(err)
if tc.wantErrIs != nil {
assert.ErrorIs(err, tc.wantErrIs)
}
if tc.wantErrContains != "" {
assert.Contains(err.Error(), tc.wantErrContains)
}
return
}
require.NoError(err)
assert.NotEmpty(tc.c.reader)
assert.NotEmpty(tc.c.writer)
})
}
}