-
Notifications
You must be signed in to change notification settings - Fork 14
/
poll_test.go
75 lines (71 loc) · 1.31 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
// Copyright (c) 2020 Meng Huang (mhboy@outlook.com)
// This package is licensed under a MIT license that can be found in the LICENSE file.
package netpoll
import (
"net"
"sync"
"syscall"
"testing"
"time"
)
func TestPoll(t *testing.T) {
p, err := Create()
defer p.Close()
if err != nil {
t.Error(err)
}
err = p.SetTimeout(-1)
if err != ErrTimeout {
t.Error(err)
}
err = p.SetTimeout(time.Millisecond * 1)
if err != nil {
t.Error(err)
}
if n, err := p.Wait(make([]Event, len(p.events)+1)); err != nil {
t.Error(err)
} else if n != 0 {
t.Error(n)
}
network := "tcp"
addr := ":9999"
l, _ := net.Listen(network, addr)
ln, _ := l.(*net.TCPListener)
f, _ := ln.File()
epfd := int(f.Fd())
if err := syscall.SetNonblock(epfd, false); err != nil {
panic(err)
}
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
conn, _ := net.Dial(network, addr)
for {
conn.Read(make([]byte, 64))
break
}
}()
fd, _, err := syscall.Accept(epfd)
if err != nil {
t.Error(err)
}
p.Register(fd)
err = p.Write(fd)
if err != nil {
t.Error(err)
}
events := make([]Event, 128)
if n, err := p.Wait(events); err != nil {
t.Error(err)
} else if n != 1 {
t.Error(n)
} else if events[0].Mode != WRITE {
t.Error(events)
}
p.Unregister(fd)
syscall.Close(fd)
f.Close()
l.Close()
wg.Wait()
}