go19_test.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. // +build go1.9
  5. package http2
  6. import (
  7. "context"
  8. "net/http"
  9. "reflect"
  10. "testing"
  11. "time"
  12. )
  13. func TestServerGracefulShutdown(t *testing.T) {
  14. var st *serverTester
  15. handlerDone := make(chan struct{})
  16. st = newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
  17. defer close(handlerDone)
  18. go st.ts.Config.Shutdown(context.Background())
  19. ga := st.wantGoAway()
  20. if ga.ErrCode != ErrCodeNo {
  21. t.Errorf("GOAWAY error = %v; want ErrCodeNo", ga.ErrCode)
  22. }
  23. if ga.LastStreamID != 1 {
  24. t.Errorf("GOAWAY LastStreamID = %v; want 1", ga.LastStreamID)
  25. }
  26. w.Header().Set("x-foo", "bar")
  27. })
  28. defer st.Close()
  29. st.greet()
  30. st.bodylessReq1()
  31. select {
  32. case <-handlerDone:
  33. case <-time.After(5 * time.Second):
  34. t.Fatalf("server did not shutdown?")
  35. }
  36. hf := st.wantHeaders()
  37. goth := st.decodeHeader(hf.HeaderBlockFragment())
  38. wanth := [][2]string{
  39. {":status", "200"},
  40. {"x-foo", "bar"},
  41. {"content-type", "text/plain; charset=utf-8"},
  42. {"content-length", "0"},
  43. }
  44. if !reflect.DeepEqual(goth, wanth) {
  45. t.Errorf("Got headers %v; want %v", goth, wanth)
  46. }
  47. n, err := st.cc.Read([]byte{0})
  48. if n != 0 || err == nil {
  49. t.Errorf("Read = %v, %v; want 0, non-nil", n, err)
  50. }
  51. }