Skip to content

Commit 57f106d

Browse files
committed
fix tests socket leakage
1 parent b0a1d0a commit 57f106d

File tree

4 files changed

+104
-12
lines changed

4 files changed

+104
-12
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ _testmain.go
2727
.wercker
2828

2929
cover.html
30+
.idea/

.travis.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ go:
44
- 1.7.x
55
- 1.8.x
66
- 1.9.x
7+
- 1.10.x
8+
- 1.11.x
79
- tip
810

911
cache: apt
@@ -18,8 +20,8 @@ before_script:
1820
- sudo apt-get update
1921
- sudo apt-get install rethinkdb
2022
- rethinkdb > /dev/null 2>&1 &
21-
- rethinkdb --port-offset 1 --directory rethinkdb_data1 --join localhost:29016 > /dev/null 2>&1 &
22-
- rethinkdb --port-offset 2 --directory rethinkdb_data2 --join localhost:29016 > /dev/null 2>&1 &
23-
- rethinkdb --port-offset 3 --directory rethinkdb_data3 --join localhost:29016 > /dev/null 2>&1 &
23+
- rethinkdb --port-offset 1 --directory rethinkdb_data1 --join localhost:29015 > /dev/null 2>&1 &
24+
- rethinkdb --port-offset 2 --directory rethinkdb_data2 --join localhost:29015 > /dev/null 2>&1 &
25+
- rethinkdb --port-offset 3 --directory rethinkdb_data3 --join localhost:29015 > /dev/null 2>&1 &
2426

2527
script: go test -tags='cluster' -short -race -v ./...

internal/integration/reql_tests/common.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ func runAndAssert(suite suite.Suite, expected, v interface{}, session *r.Session
5757

5858
func fetchAndAssert(suite suite.Suite, expected, result interface{}, count int) {
5959
switch v := expected.(type) {
60-
case Expected:
60+
case compare.Expected:
6161
v.Fetch = true
6262
v.FetchCount = count
6363

6464
expected = v
6565
default:
66-
expected = Expected(compare.Expected{
66+
expected = compare.Expected(compare.Expected{
6767
Val: v,
6868
Fetch: true,
6969
FetchCount: count,
@@ -105,17 +105,15 @@ func assertExpected(suite suite.Suite, expected interface{}, obtainedCursor *r.C
105105
switch expected := expected.(type) {
106106
case Err:
107107
expected.assert(suite, obtainedCursor, obtainedErr)
108-
case Expected:
109-
expected.assert(suite, obtainedCursor, obtainedErr)
108+
case compare.Expected:
109+
assert(suite, expected, obtainedCursor, obtainedErr)
110110
default:
111-
Expected(compare.Expected{Val: expected}).assert(suite, obtainedCursor, obtainedErr)
111+
assert(suite, compare.Expected{Val: expected}, obtainedCursor, obtainedErr)
112112
}
113113
}
114114

115-
type Expected compare.Expected
116-
117-
func (expected Expected) assert(suite suite.Suite, obtainedCursor *r.Cursor, obtainedErr error) {
118-
if suite.NoError(obtainedErr, "Query returned unexpected error") {
115+
func assert(suite suite.Suite, expected compare.Expected, obtainedCursor *r.Cursor, obtainedErr error) {
116+
if !suite.NoError(obtainedErr, "Query returned unexpected error") {
119117
return
120118
}
121119

internal/utils/conn_counter.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package utils
2+
3+
import (
4+
"fmt"
5+
"net"
6+
"os"
7+
"regexp"
8+
"runtime"
9+
"runtime/debug"
10+
"strings"
11+
"sync"
12+
"sync/atomic"
13+
"time"
14+
)
15+
16+
var connsCount int64
17+
var printer sync.Once
18+
19+
var mu sync.Mutex
20+
var createdStacks = make(map[string]int)
21+
var reg = regexp.MustCompile(`0[xX][0-9a-fA-F]+`)
22+
23+
type connCounting struct {
24+
net.Conn
25+
26+
closed bool
27+
}
28+
29+
// Socket leak debug net.Conn wrapper
30+
func NewCountingConn(conn net.Conn) net.Conn{
31+
c := &connCounting{
32+
Conn: conn,
33+
closed: false,
34+
}
35+
runtime.SetFinalizer(c, func(cc *connCounting){
36+
if !cc.closed {
37+
atomic.AddInt64(&connsCount, -1)
38+
cc.closed = true
39+
}
40+
})
41+
42+
atomic.AddInt64(&connsCount, 1)
43+
printer.Do(func(){
44+
go func() {
45+
t := time.NewTicker(time.Second)
46+
f, err := os.Create("sockets.ticker")
47+
if err != nil {
48+
fmt.Fprintf(os.Stderr, "Failed to create sockets.ticker file: %v\n", err)
49+
return
50+
}
51+
for {
52+
<-t.C
53+
fmt.Fprintf(f, "Connections count: %v\n", atomic.LoadInt64(&connsCount))
54+
f.Sync()
55+
}
56+
}()
57+
})
58+
59+
st := string(debug.Stack())
60+
st = st[strings.Index(st, "\n")+1:]
61+
st = reg.ReplaceAllString(st, "")
62+
63+
mu.Lock()
64+
_, has := createdStacks[st]
65+
if !has {
66+
createdStacks[st] = 1
67+
} else {
68+
createdStacks[st]++
69+
}
70+
printStacks()
71+
mu.Unlock()
72+
73+
return c
74+
}
75+
76+
func (c *connCounting) Close() error {
77+
if !c.closed {
78+
atomic.AddInt64(&connsCount, -1)
79+
c.closed = true
80+
}
81+
return c.Conn.Close()
82+
}
83+
84+
func printStacks() {
85+
f, _ := os.Create("sockets.created")
86+
for s, c := range createdStacks {
87+
fmt.Fprintf(f, "%v:\n%v\n\n", c, s)
88+
}
89+
f.Sync()
90+
f.Close()
91+
}

0 commit comments

Comments
 (0)