-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathselect_test.go
60 lines (54 loc) · 1.08 KB
/
select_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
package goselect
import (
"fmt"
"io"
"os"
"testing"
"time"
)
type fder interface {
Fd() uintptr
}
func TestReadWriteSync(t *testing.T) {
const count = 50
rrs := []io.Reader{}
wws := []io.Writer{}
rFDSet := &FDSet{}
for i := 0; i < count; i++ {
rr, ww, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
rrs = append(rrs, rr)
wws = append(wws, ww)
}
go func() {
time.Sleep(time.Second)
for i := 0; i < count; i++ {
fmt.Fprintf(wws[i], "hello %d", i)
time.Sleep(10*time.Millisecond)
}
}()
buf := make([]byte, 1024)
for i := 0; i < count; i++ {
rFDSet.Zero()
for i := 0; i < count; i++ {
rFDSet.Set(rrs[i].(fder).Fd())
}
if err := RetrySelect(1024, rFDSet, nil, nil, -1, 10, 10*time.Millisecond); err != nil {
t.Fatalf("select call failed: %s", err)
}
for j := 0; j < count; j++ {
if rFDSet.IsSet(rrs[j].(fder).Fd()) {
// println(i, j)
if i != j {
t.Fatalf("unexpected fd ready: %d,expected: %d", j, i)
}
_, err := rrs[j].Read(buf)
if err != nil {
t.Fatalf("read call failed: %s", err)
}
}
}
}
}