-
Notifications
You must be signed in to change notification settings - Fork 8
/
connection_close_test.go
97 lines (75 loc) · 1.64 KB
/
connection_close_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
package tnt
import (
"net"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestClose(t *testing.T) {
assert := assert.New(t)
raddr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:0")
assert.NoError(err)
listener, err := net.ListenTCP("tcp", raddr)
if !assert.NoError(err) {
return
}
defer listener.Close()
// go func() {
// time.Sleep(time.Second)
// // log.Fatal(1)
// pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
// }()
// pp.Println(listener.Addr().String())
conn, err := Connect(listener.Addr().String(), nil)
if !assert.NoError(err) {
return
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
data, err := conn.Execute(&Select{
Value: PackInt(0),
})
assert.Empty(data)
assert.Error(err)
assert.Equal("Connection closed", err.Error())
wg.Done()
}()
time.Sleep(10 * time.Millisecond)
conn.Close()
wg.Wait()
}
func TestCloseExecute(t *testing.T) {
assert := assert.New(t)
raddr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:0")
assert.NoError(err)
listener, err := net.ListenTCP("tcp", raddr)
if !assert.NoError(err) {
return
}
conn, err := Connect(listener.Addr().String(), nil)
if !assert.NoError(err) {
return
}
go func() {
time.Sleep(time.Duration(100 * time.Millisecond))
listener.Close()
}()
data, err := conn.Execute(&Select{
Value: PackInt(1),
Space: 1,
})
assert.Nil(data)
assert.Error(err)
assert.IsType(&ConnectionError{}, err)
time.Sleep(100 * time.Millisecond)
// execute on closed connection
data, err = conn.Execute(&Select{
Value: PackInt(1),
Space: 1,
})
assert.Nil(data)
assert.Error(err)
assert.IsType(&ConnectionError{}, err)
}