Skip to content

Commit d9abbec

Browse files
authored
Workaround for Go 1.16 (#642)
* Fix Go1.16 * Add more tests * Add build tag * Add Go 1.16 to appveyor.yml
1 parent 045585d commit d9abbec

File tree

4 files changed

+208
-20
lines changed

4 files changed

+208
-20
lines changed

appveyor.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ environment:
3939
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2019
4040
GOVERSION: 115
4141
SQLINSTANCE: SQL2017
42+
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2019
43+
GOVERSION: 116
44+
SQLINSTANCE: SQL2017
4245

4346
install:
4447
- set GOROOT=c:\go%GOVERSION%

net.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
)
88

99
type timeoutConn struct {
10-
c net.Conn
11-
timeout time.Duration
10+
c net.Conn
11+
timeout time.Duration
1212
}
1313

1414
func newTimeoutConn(conn net.Conn, timeout time.Duration) *timeoutConn {
@@ -51,21 +51,21 @@ func (c timeoutConn) RemoteAddr() net.Addr {
5151
}
5252

5353
func (c timeoutConn) SetDeadline(t time.Time) error {
54-
panic("Not implemented")
54+
return c.c.SetDeadline(t)
5555
}
5656

5757
func (c timeoutConn) SetReadDeadline(t time.Time) error {
58-
panic("Not implemented")
58+
return c.c.SetReadDeadline(t)
5959
}
6060

6161
func (c timeoutConn) SetWriteDeadline(t time.Time) error {
62-
panic("Not implemented")
62+
return c.c.SetWriteDeadline(t)
6363
}
6464

6565
// this connection is used during TLS Handshake
6666
// TDS protocol requires TLS handshake messages to be sent inside TDS packets
6767
type tlsHandshakeConn struct {
68-
buf *tdsBuffer
68+
buf *tdsBuffer
6969
packetPending bool
7070
continueRead bool
7171
}
@@ -105,27 +105,27 @@ func (c *tlsHandshakeConn) Write(b []byte) (n int, err error) {
105105
}
106106

107107
func (c *tlsHandshakeConn) Close() error {
108-
panic("Not implemented")
108+
return c.buf.transport.Close()
109109
}
110110

111111
func (c *tlsHandshakeConn) LocalAddr() net.Addr {
112-
panic("Not implemented")
112+
return nil
113113
}
114114

115115
func (c *tlsHandshakeConn) RemoteAddr() net.Addr {
116-
panic("Not implemented")
116+
return nil
117117
}
118118

119-
func (c *tlsHandshakeConn) SetDeadline(t time.Time) error {
120-
panic("Not implemented")
119+
func (c *tlsHandshakeConn) SetDeadline(_ time.Time) error {
120+
return nil
121121
}
122122

123-
func (c *tlsHandshakeConn) SetReadDeadline(t time.Time) error {
124-
panic("Not implemented")
123+
func (c *tlsHandshakeConn) SetReadDeadline(_ time.Time) error {
124+
return nil
125125
}
126126

127-
func (c *tlsHandshakeConn) SetWriteDeadline(t time.Time) error {
128-
panic("Not implemented")
127+
func (c *tlsHandshakeConn) SetWriteDeadline(_ time.Time) error {
128+
return nil
129129
}
130130

131131
// this connection just delegates all methods to it's wrapped connection
@@ -148,21 +148,21 @@ func (c passthroughConn) Close() error {
148148
}
149149

150150
func (c passthroughConn) LocalAddr() net.Addr {
151-
panic("Not implemented")
151+
return c.c.LocalAddr()
152152
}
153153

154154
func (c passthroughConn) RemoteAddr() net.Addr {
155-
panic("Not implemented")
155+
return c.c.RemoteAddr()
156156
}
157157

158158
func (c passthroughConn) SetDeadline(t time.Time) error {
159-
panic("Not implemented")
159+
return c.c.SetDeadline(t)
160160
}
161161

162162
func (c passthroughConn) SetReadDeadline(t time.Time) error {
163-
panic("Not implemented")
163+
return c.c.SetReadDeadline(t)
164164
}
165165

166166
func (c passthroughConn) SetWriteDeadline(t time.Time) error {
167-
panic("Not implemented")
167+
return c.c.SetWriteDeadline(t)
168168
}

net_go116_test.go

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
// +build go1.16
2+
3+
package mssql
4+
5+
import (
6+
"context"
7+
"net"
8+
"testing"
9+
"time"
10+
)
11+
12+
func assertPanic(t *testing.T, paniced bool) {
13+
v := recover()
14+
if paniced && v == nil {
15+
t.Fatalf(`expected panic but it did not`)
16+
}
17+
18+
if !paniced && v != nil {
19+
t.Fatalf(`expected no panic but it did`)
20+
}
21+
}
22+
23+
func TestTimeoutConn(t *testing.T) {
24+
_, conn := net.Pipe()
25+
26+
tconn := newTimeoutConn(conn, time.Minute)
27+
t.Run(`set deadline`, func(t *testing.T) {
28+
defer assertPanic(t, false)
29+
deadline := time.Now().Add(time.Millisecond * 100)
30+
31+
err := tconn.SetDeadline(deadline)
32+
if err != nil {
33+
t.Fatalf(`SetDeadline should return nil`)
34+
}
35+
})
36+
37+
t.Run(`set read deadline`, func(t *testing.T) {
38+
defer assertPanic(t, false)
39+
deadline := time.Now().Add(time.Minute)
40+
41+
err := tconn.SetReadDeadline(deadline)
42+
if err != nil {
43+
t.Fatalf(`SetReadDeadline should return nil`)
44+
}
45+
})
46+
47+
t.Run(`set write deadline`, func(t *testing.T) {
48+
defer assertPanic(t, false)
49+
deadline := time.Now().Add(time.Minute)
50+
51+
err := tconn.SetWriteDeadline(deadline)
52+
if err != nil {
53+
t.Fatalf(`SetWriteDeadline should return nil`)
54+
}
55+
})
56+
}
57+
58+
func TestTLSHandshakeConn(t *testing.T) {
59+
SetLogger(testLogger{t})
60+
61+
connector, err := NewConnector(makeConnStr(t).String())
62+
if err != nil {
63+
t.Error(err)
64+
}
65+
66+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
67+
defer cancel()
68+
69+
toconn, err := dialConnection(ctx, connector, connector.params)
70+
if err != nil {
71+
t.Error(err)
72+
}
73+
74+
outbuf := newTdsBuffer(connector.params.packetSize, toconn)
75+
handshakeConn := tlsHandshakeConn{buf: outbuf}
76+
77+
t.Run(`set deadline`, func(t *testing.T) {
78+
defer assertPanic(t, false)
79+
deadline := time.Now().Add(time.Millisecond * 100)
80+
81+
err := handshakeConn.SetDeadline(deadline)
82+
if err != nil {
83+
t.Fatalf(`SetDeadline should return nil`)
84+
}
85+
})
86+
87+
t.Run(`set read deadline`, func(t *testing.T) {
88+
defer assertPanic(t, false)
89+
deadline := time.Now().Add(time.Minute)
90+
91+
err := handshakeConn.SetReadDeadline(deadline)
92+
if err != nil {
93+
t.Fatalf(`SetReadDeadline should return nil`)
94+
}
95+
})
96+
97+
t.Run(`set write deadline`, func(t *testing.T) {
98+
defer assertPanic(t, false)
99+
deadline := time.Now().Add(time.Minute)
100+
101+
err := handshakeConn.SetWriteDeadline(deadline)
102+
if err != nil {
103+
t.Fatalf(`SetWriteDeadline should return nil`)
104+
}
105+
})
106+
107+
t.Run(`get remote addr`, func(t *testing.T) {
108+
addr := handshakeConn.RemoteAddr()
109+
if addr != nil {
110+
t.Fatalf(`RemoteAddr should return nil`)
111+
}
112+
})
113+
}
114+
115+
func TestPassthroughConn(t *testing.T) {
116+
SetLogger(testLogger{t})
117+
118+
connector, err := NewConnector(makeConnStr(t).String())
119+
if err != nil {
120+
t.Error(err)
121+
}
122+
123+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
124+
defer cancel()
125+
126+
toconn, err := dialConnection(ctx, connector, connector.params)
127+
if err != nil {
128+
t.Error(err)
129+
}
130+
131+
outbuf := newTdsBuffer(connector.params.packetSize, toconn)
132+
133+
handshakeConn := tlsHandshakeConn{buf: outbuf}
134+
passthrough := passthroughConn{c: &handshakeConn}
135+
136+
t.Run(`set deadline`, func(t *testing.T) {
137+
defer assertPanic(t, false)
138+
deadline := time.Now().Add(time.Millisecond * 100)
139+
140+
err := passthrough.SetDeadline(deadline)
141+
if err != nil {
142+
t.Fatalf(`SetDeadline should return nil`)
143+
}
144+
})
145+
146+
t.Run(`set read deadline`, func(t *testing.T) {
147+
defer assertPanic(t, false)
148+
deadline := time.Now().Add(time.Minute)
149+
150+
err := passthrough.SetReadDeadline(deadline)
151+
if err != nil {
152+
t.Fatalf(`SetReadDeadline should return nil`)
153+
}
154+
})
155+
156+
t.Run(`set write deadline`, func(t *testing.T) {
157+
defer assertPanic(t, false)
158+
deadline := time.Now().Add(time.Minute)
159+
160+
err := passthrough.SetWriteDeadline(deadline)
161+
if err != nil {
162+
t.Fatalf(`SetWriteDeadline should return nil`)
163+
}
164+
})
165+
166+
t.Run(`get remote addr`, func(t *testing.T) {
167+
addr := passthrough.RemoteAddr()
168+
if addr != nil {
169+
t.Fatalf(`RemoteAddr should return nil`)
170+
}
171+
})
172+
}

queries_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2230,3 +2230,16 @@ func TestDisconnect2(t *testing.T) {
22302230
t.Fatal("timeout")
22312231
}
22322232
}
2233+
2234+
func TestClose(t *testing.T) {
2235+
conn := open(t)
2236+
2237+
defer func() {
2238+
v := recover()
2239+
if v != nil {
2240+
t.Fatal(`Close should not panic:`, v)
2241+
}
2242+
}()
2243+
2244+
conn.Close()
2245+
}

0 commit comments

Comments
 (0)