forked from cloudwego/netpoll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
poll_test.go
133 lines (118 loc) · 3.2 KB
/
poll_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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright 2021 CloudWeGo Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build !windows
// +build !windows
package netpoll
import (
"sync"
"sync/atomic"
"syscall"
"testing"
"time"
)
// Trigger has been validated, but no usage for now.
func TestPollTrigger(t *testing.T) {
t.Skip()
var trigger int
var stop = make(chan error)
var p = openDefaultPoll()
go func() {
stop <- p.Wait()
}()
time.Sleep(time.Millisecond)
Equal(t, trigger, 0)
p.Trigger()
time.Sleep(time.Millisecond)
Equal(t, trigger, 1)
p.Trigger()
time.Sleep(time.Millisecond)
Equal(t, trigger, 2)
p.Close()
err := <-stop
MustNil(t, err)
}
func TestPollMod(t *testing.T) {
var rn, wn, hn int32
var read = func(p Poll) error {
atomic.AddInt32(&rn, 1)
return nil
}
var write = func(p Poll) error {
atomic.AddInt32(&wn, 1)
return nil
}
var hup = func(p Poll) error {
atomic.AddInt32(&hn, 1)
return nil
}
var stop = make(chan error)
var p = openDefaultPoll()
go func() {
stop <- p.Wait()
}()
var rfd, wfd = GetSysFdPairs()
var rop = &FDOperator{FD: rfd, OnRead: read, OnWrite: write, OnHup: hup, poll: p}
var wop = &FDOperator{FD: wfd, OnRead: read, OnWrite: write, OnHup: hup, poll: p}
var err error
var r, w, h int32
r, w, h = atomic.LoadInt32(&rn), atomic.LoadInt32(&wn), atomic.LoadInt32(&hn)
Assert(t, r == 0 && w == 0 && h == 0, r, w, h)
err = p.Control(rop, PollReadable)
MustNil(t, err)
r, w, h = atomic.LoadInt32(&rn), atomic.LoadInt32(&wn), atomic.LoadInt32(&hn)
Assert(t, r == 0 && w == 0 && h == 0, r, w, h)
err = p.Control(wop, PollWritable) // trigger one shot
MustNil(t, err)
time.Sleep(50 * time.Millisecond)
r, w, h = atomic.LoadInt32(&rn), atomic.LoadInt32(&wn), atomic.LoadInt32(&hn)
Assert(t, r == 0 && w == 1 && h == 0, r, w, h)
err = p.Control(rop, PollR2RW) // trigger write
MustNil(t, err)
time.Sleep(time.Millisecond)
r, w, h = atomic.LoadInt32(&rn), atomic.LoadInt32(&wn), atomic.LoadInt32(&hn)
Assert(t, r == 0 && w >= 2 && h == 0, r, w, h)
// close wfd, then trigger hup rfd
err = syscall.Close(wfd) // trigger hup
MustNil(t, err)
time.Sleep(time.Millisecond)
r, w, h = atomic.LoadInt32(&rn), atomic.LoadInt32(&wn), atomic.LoadInt32(&hn)
Assert(t, r == 1 && w >= 2 && h >= 1, r, w, h)
p.Close()
err = <-stop
MustNil(t, err)
}
func TestPollClose(t *testing.T) {
var p = openDefaultPoll()
var wg sync.WaitGroup
wg.Add(1)
go func() {
p.Wait()
wg.Done()
}()
p.Close()
wg.Wait()
}
func BenchmarkPollMod(b *testing.B) {
b.StopTimer()
var p = openDefaultPoll()
r, _ := GetSysFdPairs()
var operator = &FDOperator{FD: r}
p.Control(operator, PollReadable)
// benchmark
b.ReportAllocs()
b.StartTimer()
for i := 0; i < b.N; i++ {
p.Control(operator, PollR2RW)
}
}