-
Notifications
You must be signed in to change notification settings - Fork 27
/
connector_test.go
77 lines (65 loc) · 1.6 KB
/
connector_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
//go:build go1.10
// +build go1.10
package proxy
import (
"context"
"database/sql/driver"
"errors"
"io"
"testing"
)
var _ io.Closer = (*Connector)(nil)
var _ driver.Connector = (*Connector)(nil)
var _ driver.DriverContext = (*Proxy)(nil)
var _ io.Closer = (*closerConnector)(nil)
var _ driver.Connector = (*closerConnector)(nil)
type closerConnector struct {
// the result of Close() method
errClose error
// a flag whether Close() method is called
closed bool
}
func (c *closerConnector) Connect(ctx context.Context) (driver.Conn, error) {
panic("never used")
}
func (c *closerConnector) Driver() driver.Driver {
return fdriverctx
}
func (c *closerConnector) Close() error {
c.closed = true
return c.errClose
}
func TestConnectorClose(t *testing.T) {
t.Run("c.Connector doesn't implement io.Closer", func(t *testing.T) {
c0 := &fakeConnector{
driver: fdriverctx,
}
c1 := NewConnector(c0)
if err := c1.(io.Closer).Close(); err != nil {
t.Fatal(err)
}
})
t.Run("Closing c.Connector succeeds", func(t *testing.T) {
c0 := &closerConnector{}
c1 := NewConnector(c0)
if err := c1.(io.Closer).Close(); err != nil {
t.Fatal(err)
}
if !c0.closed {
t.Errorf("c.Connector should be closed, but not")
}
})
t.Run("Closing c.Connector fails", func(t *testing.T) {
errClose := errors.New("some error while closing")
c0 := &closerConnector{
errClose: errClose,
}
c1 := NewConnector(c0)
if err := c1.(io.Closer).Close(); err != errClose {
t.Errorf("want err is %v, got %v", errClose, err)
}
if !c0.closed {
t.Errorf("c.Connector should be closed, but not")
}
})
}