Skip to content

Commit 1eb5ca1

Browse files
committed
Added test case for #27
1 parent 30d1302 commit 1eb5ca1

File tree

2 files changed

+95
-6
lines changed

2 files changed

+95
-6
lines changed

serverConn.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -264,14 +264,16 @@ func (sc *serverConn) handleStreams() {
264264
streamPool.Put(strm)
265265

266266
if sc.debug {
267-
sc.logger.Printf("Stream destroyed %d\n", strmID)
267+
sc.logger.Printf("Stream destroyed %d. Open streams: %d\n", strmID, openStreams)
268268
}
269269
}
270270

271271
loop:
272272
for {
273273
select {
274274
case <-sc.maxRequestTimer.C:
275+
reqTimerArmed = false
276+
275277
deleteUntil := 0
276278
for _, strm := range strms {
277279
// the request is due if the startedAt time + maxRequestTime is in the past
@@ -308,6 +310,10 @@ loop:
308310
when := strm.startedAt.Add(sc.maxRequestTime).Sub(time.Now())
309311
// if the time is negative or zero it triggers imm
310312
sc.maxRequestTimer.Reset(when)
313+
314+
if sc.debug {
315+
sc.logger.Printf("Next request will timeout in %f seconds\n", when.Seconds())
316+
}
311317
}
312318
}
313319
case fr, ok := <-sc.reader:
@@ -380,9 +386,17 @@ loop:
380386

381387
sc.createStream(sc.c, fr.Type(), strm)
382388

389+
if sc.debug {
390+
sc.logger.Printf("Stream %d created. Open streams: %d\n", strm.ID(), openStreams)
391+
}
392+
383393
if !reqTimerArmed && sc.maxRequestTime > 0 {
384394
reqTimerArmed = true
385395
sc.maxRequestTimer.Reset(sc.maxRequestTime)
396+
397+
if sc.debug {
398+
sc.logger.Printf("Next request will timeout in %f seconds\n", sc.maxRequestTime.Seconds())
399+
}
386400
}
387401
}
388402

server_test.go

+80-5
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func getConn(s *Server) (*Conn, net.Listener, error) {
3939
return nc, ln, nc.doHandshake()
4040
}
4141

42-
func makeHeaders(id uint32, enc *HPACK, endStream bool, hs map[string]string) *FrameHeader {
42+
func makeHeaders(id uint32, enc *HPACK, endHeaders, endStream bool, hs map[string]string) *FrameHeader {
4343
fr := AcquireFrameHeader()
4444

4545
fr.SetStream(id)
@@ -56,7 +56,7 @@ func makeHeaders(id uint32, enc *HPACK, endStream bool, hs map[string]string) *F
5656

5757
h.SetPadding(false)
5858
h.SetEndStream(endStream)
59-
h.SetEndHeaders(true)
59+
h.SetEndHeaders(endHeaders)
6060

6161
return fr
6262
}
@@ -89,21 +89,21 @@ func testIssue52(t *testing.T) {
8989

9090
msg := []byte("Hello world, how are you doing?")
9191

92-
h1 := makeHeaders(3, c.enc, false, map[string]string{
92+
h1 := makeHeaders(3, c.enc, true, false, map[string]string{
9393
string(StringAuthority): "localhost",
9494
string(StringMethod): "POST",
9595
string(StringPath): "/hello/world",
9696
string(StringScheme): "https",
9797
"Content-Length": strconv.Itoa(len(msg)),
9898
})
99-
h2 := makeHeaders(9, c.enc, false, map[string]string{
99+
h2 := makeHeaders(9, c.enc, true, false, map[string]string{
100100
string(StringAuthority): "localhost",
101101
string(StringMethod): "POST",
102102
string(StringPath): "/hello/world",
103103
string(StringScheme): "https",
104104
"Content-Length": strconv.Itoa(len(msg)),
105105
})
106-
h3 := makeHeaders(7, c.enc, true, map[string]string{
106+
h3 := makeHeaders(7, c.enc, true, true, map[string]string{
107107
string(StringAuthority): "localhost",
108108
string(StringMethod): "GET",
109109
string(StringPath): "/hello/world",
@@ -153,3 +153,78 @@ func testIssue52(t *testing.T) {
153153
t.Fatalf("expected EOF, got %s", err)
154154
}
155155
}
156+
157+
func TestIssue27(t *testing.T) {
158+
s := &Server{
159+
s: &fasthttp.Server{
160+
Handler: func(ctx *fasthttp.RequestCtx) {
161+
io.WriteString(ctx, "Hello world")
162+
},
163+
ReadTimeout: time.Second * 1,
164+
},
165+
cnf: ServerConfig{
166+
Debug: false,
167+
},
168+
}
169+
170+
c, ln, err := getConn(s)
171+
if err != nil {
172+
t.Fatal(err)
173+
}
174+
defer c.Close()
175+
defer ln.Close()
176+
177+
msg := []byte("Hello world, how are you doing?")
178+
179+
h1 := makeHeaders(3, c.enc, true, false, map[string]string{
180+
string(StringAuthority): "localhost",
181+
string(StringMethod): "POST",
182+
string(StringPath): "/hello/world",
183+
string(StringScheme): "https",
184+
"Content-Length": strconv.Itoa(len(msg)),
185+
})
186+
h2 := makeHeaders(5, c.enc, true, false, map[string]string{
187+
string(StringAuthority): "localhost",
188+
string(StringMethod): "POST",
189+
string(StringPath): "/hello/world",
190+
string(StringScheme): "https",
191+
"Content-Length": strconv.Itoa(len(msg)),
192+
})
193+
h3 := makeHeaders(7, c.enc, false, false, map[string]string{
194+
string(StringAuthority): "localhost",
195+
string(StringMethod): "GET",
196+
string(StringPath): "/hello/world",
197+
string(StringScheme): "https",
198+
"Content-Length": strconv.Itoa(len(msg)),
199+
})
200+
201+
c.writeFrame(h1)
202+
c.writeFrame(h2)
203+
204+
time.Sleep(time.Second)
205+
c.writeFrame(h3)
206+
207+
id := uint32(3)
208+
209+
for i := 0; i < 3; i++ {
210+
fr, err := c.readNext()
211+
if err != nil {
212+
t.Fatal(err)
213+
}
214+
215+
if fr.Stream() != id {
216+
t.Fatalf("Expecting update on stream %d, got %d", id, fr.Stream())
217+
}
218+
219+
if fr.Type() != FrameResetStream {
220+
t.Fatalf("Expecting Reset, got %s", fr.Type())
221+
}
222+
223+
rst := fr.Body().(*RstStream)
224+
if rst.Code() != StreamCanceled {
225+
t.Fatalf("Expecting StreamCanceled, got %s", rst.Code())
226+
}
227+
228+
id += 2
229+
}
230+
}

0 commit comments

Comments
 (0)