-
Notifications
You must be signed in to change notification settings - Fork 24
/
readwrite_test.go
102 lines (94 loc) · 1.87 KB
/
readwrite_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
// Copyright 2016 Mikio Hara. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build darwin freebsd linux netbsd
package tcp_test
import (
"net"
"runtime"
"sync"
"testing"
"github.com/mikioh/tcp"
"github.com/mikioh/tcpinfo"
)
func server(t *testing.T, ln net.Listener) {
c, err := ln.Accept()
if err != nil {
return
}
go server(t, ln)
defer c.Close()
var b [1]byte
if _, err := c.Read(b[:]); err != nil {
t.Error(err)
return
}
if _, err := c.Write(b[:]); err != nil {
t.Error(err)
return
}
}
func TestConcurrentReadWriteAndInfo(t *testing.T) {
switch runtime.GOOS {
case "darwin":
vers := kernelVersion()
if vers == nil || vers[0] < 15 {
t.Skipf("not supported on %v, %s/%s", vers, runtime.GOOS, runtime.GOARCH)
}
case "freebsd", "linux", "netbsd":
default:
t.Skipf("not supported on %s/%s", runtime.GOOS, runtime.GOARCH)
}
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
defer ln.Close()
go server(t, ln)
const N = 10
var wg sync.WaitGroup
wg.Add(N)
for i := 0; i < N; i++ {
go func() {
defer wg.Done()
var d net.Dialer
c, err := d.Dial(ln.Addr().Network(), ln.Addr().String())
if err != nil {
t.Error(err)
return
}
defer c.Close()
tc, err := tcp.NewConn(c)
if err != nil {
t.Error(err)
return
}
var wwg sync.WaitGroup
wwg.Add(1)
go func() {
defer wwg.Done()
var b [1]byte
if _, err := c.Write(b[:]); err != nil {
t.Error(err)
return
}
if _, err := c.Read(b[:]); err != nil {
t.Error(err)
return
}
}()
wwg.Add(1)
go func() {
defer wwg.Done()
var o tcpinfo.Info
b := make([]byte, 256)
if _, err := tc.Option(o.Level(), o.Name(), b); err != nil {
t.Error(err)
return
}
}()
wwg.Wait()
}()
}
wg.Wait()
}