OSDN Git Service

new repo
[bytom/vapor.git] / vendor / golang.org / x / net / http2 / go19_test.go
1 // Copyright 2017 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // +build go1.9
6
7 package http2
8
9 import (
10         "context"
11         "net/http"
12         "reflect"
13         "testing"
14         "time"
15 )
16
17 func TestServerGracefulShutdown(t *testing.T) {
18         var st *serverTester
19         handlerDone := make(chan struct{})
20         st = newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
21                 defer close(handlerDone)
22                 go st.ts.Config.Shutdown(context.Background())
23
24                 ga := st.wantGoAway()
25                 if ga.ErrCode != ErrCodeNo {
26                         t.Errorf("GOAWAY error = %v; want ErrCodeNo", ga.ErrCode)
27                 }
28                 if ga.LastStreamID != 1 {
29                         t.Errorf("GOAWAY LastStreamID = %v; want 1", ga.LastStreamID)
30                 }
31
32                 w.Header().Set("x-foo", "bar")
33         })
34         defer st.Close()
35
36         st.greet()
37         st.bodylessReq1()
38
39         select {
40         case <-handlerDone:
41         case <-time.After(5 * time.Second):
42                 t.Fatalf("server did not shutdown?")
43         }
44         hf := st.wantHeaders()
45         goth := st.decodeHeader(hf.HeaderBlockFragment())
46         wanth := [][2]string{
47                 {":status", "200"},
48                 {"x-foo", "bar"},
49                 {"content-type", "text/plain; charset=utf-8"},
50                 {"content-length", "0"},
51         }
52         if !reflect.DeepEqual(goth, wanth) {
53                 t.Errorf("Got headers %v; want %v", goth, wanth)
54         }
55
56         n, err := st.cc.Read([]byte{0})
57         if n != 0 || err == nil {
58                 t.Errorf("Read = %v, %v; want 0, non-nil", n, err)
59         }
60 }