-
Notifications
You must be signed in to change notification settings - Fork 0
/
loop.go
154 lines (147 loc) · 2.98 KB
/
loop.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package levelup
import (
"net"
"sync"
"time"
)
func (self Database) UpdateLoop() {
self.Events.Writing = func(self Database) (event Event) {
if self.IsActive {
self.Unload()
}
var lastTick time.Time
var delay time.Duration = -1
if events.Tick != nil {
delay = 0
}
if events.Tick != nil {
now := time.Now()
if now.Sub(lastTick) > delay {
var action Action
lastTick = now
delay, action = events.Tick(now)
if delay < 0 {
delay = 0
}
if action == Shutdown {
return nil
}
}
}
go func() {
c, err := net.Dial("tcp", addr)
if err != nil {
t.Fatal(err)
}
defer c.Close()
var data [64]byte
n, _ := c.Read(data[:])
if string(data[:n]) != "HI THERE" {
t.Fatalf("expected '%s', got '%s'", " THERE", data[:n])
}
c.Write([]byte("HELLO"))
n, _ = c.Read(data[:])
if string(data[:n]) != "HELLO" {
t.Fatalf("expected '%s', got '%s'", "HELLO", data[:n])
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
c, err := net.Dial("tcp", addr)
if err != nil {
t.Fatal(err)
}
defer c.Close()
var data [64]byte
n, _ := c.Read(data[:])
if string(data[:n]) != "HI THERE" {
t.Fatalf("expected '%s', got '%s'", " THERE", data[:n])
}
n, _ = c.Read(data[:])
if string(data[:n]) != "HERE" {
t.Fatalf("expected '%s', got '%s'", "HERE", data[:n])
}
n, _ = c.Read(data[:])
if n != 0 {
t.Fatalf("expected zero")
}
// add 15 connections
for i := 0; i < 15; i++ {
net.Dial("tcp", addr)
}
}()
wg.Wait()
c.Write([]byte("SHUTDOWN"))
n, _ = c.Read(data[:])
if string(data[:n]) != "GOOD BYE" {
t.Fatalf("expected '%s', got '%s'", "GOOD BYE", data[:n])
}
}()
return
}
preWriteCount := 0
events.PreWrite = func() {
preWriteCount++
}
var c2 Conn
var max int
events.Opened = func(c Conn) (out []byte, action Action) {
if c.LocalAddr().String() == "" {
t.Fatal("should not be empty")
}
if c.RemoteAddr().String() == "" {
t.Fatal("should not be empty")
}
max++
opened++
if opened == 2 {
c2 = c
}
return []byte("HI THERE"), None
}
events.Closed = func(c Conn) (action Action) {
opened--
return
}
events.Data = func(c Conn, in []byte) (out []byte, action Action) {
if string(in) == "SHUTDOWN" {
return []byte("GOOD BYE"), Shutdown
}
return in, None
}
numTicks := 0
var c2n int
events.Tick = func(now time.Time) (delay time.Duration, action Action) {
numTicks++
if numTicks == 1 {
return -10, None
}
delay = time.Second / 10
if c2 != nil {
if c2n == 0 {
c2.Write([]byte("HERE"))
} else if c2n == 1 {
c2.Close()
c2 = nil
}
c2n++
}
return
}
if err := Serve(events, "tcp://"+addr); err != nil {
t.Fatal(err)
}
if preWriteCount == 0 {
t.Fatal("expected preWrites")
}
if opened != 0 {
t.Fatal("expected zero")
}
if max != 17 {
t.Fatalf("expected 17, got %v", max)
}
// should not cause problems
c2.Write(nil)
c2.Close()
}