1 // Copyright 2014 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.
18 "golang.org/x/net/http2/hpack"
21 var knownFailing = flag.Bool("known_failing", false, "Run known-failing tests.")
23 func condSkipFailingTest(t *testing.T) {
25 t.Skip("Skipping known-failing test without --known_failing")
31 DebugGoroutines = true
32 flag.BoolVar(&VerboseLogs, "verboseh2", VerboseLogs, "Verbose HTTP/2 debug logging")
35 func TestSettingString(t *testing.T) {
40 {Setting{SettingMaxFrameSize, 123}, "[MAX_FRAME_SIZE = 123]"},
41 {Setting{1<<16 - 1, 123}, "[UNKNOWN_SETTING_65535 = 123]"},
43 for i, tt := range tests {
44 got := fmt.Sprint(tt.s)
46 t.Errorf("%d. for %#v, string = %q; want %q", i, tt.s, got, tt.want)
53 st *serverTester // optional
56 func (w twriter) Write(p []byte) (n int, err error) {
59 for _, phrase := range w.st.logFilter {
60 if strings.Contains(ps, phrase) {
61 return len(p), nil // no logging
69 // like encodeHeader, but don't add implicit pseudo headers.
70 func encodeHeaderNoImplicit(t *testing.T, headers ...string) []byte {
72 enc := hpack.NewEncoder(&buf)
73 for len(headers) > 0 {
74 k, v := headers[0], headers[1]
76 if err := enc.WriteField(hpack.HeaderField{Name: k, Value: v}); err != nil {
77 t.Fatalf("HPACK encoding error for %q/%q: %v", k, v, err)
83 // Verify that curl has http2.
84 func requireCurl(t *testing.T) {
85 out, err := dockerLogs(curl(t, "--version"))
87 t.Skipf("failed to determine curl features; skipping test")
89 if !strings.Contains(string(out), "HTTP2") {
90 t.Skip("curl doesn't support HTTP2; skipping test")
94 func curl(t *testing.T, args ...string) (container string) {
95 out, err := exec.Command("docker", append([]string{"run", "-d", "--net=host", "gohttp2/curl"}, args...)...).Output()
97 t.Skipf("Failed to run curl in docker: %v, %s", err, out)
99 return strings.TrimSpace(string(out))
102 // Verify that h2load exists.
103 func requireH2load(t *testing.T) {
104 out, err := dockerLogs(h2load(t, "--version"))
106 t.Skipf("failed to probe h2load; skipping test: %s", out)
108 if !strings.Contains(string(out), "h2load nghttp2/") {
109 t.Skipf("h2load not present; skipping test. (Output=%q)", out)
113 func h2load(t *testing.T, args ...string) (container string) {
114 out, err := exec.Command("docker", append([]string{"run", "-d", "--net=host", "--entrypoint=/usr/local/bin/h2load", "gohttp2/curl"}, args...)...).Output()
116 t.Skipf("Failed to run h2load in docker: %v, %s", err, out)
118 return strings.TrimSpace(string(out))
121 type puppetCommand struct {
122 fn func(w http.ResponseWriter, r *http.Request)
126 type handlerPuppet struct {
127 ch chan puppetCommand
130 func newHandlerPuppet() *handlerPuppet {
131 return &handlerPuppet{
132 ch: make(chan puppetCommand),
136 func (p *handlerPuppet) act(w http.ResponseWriter, r *http.Request) {
137 for cmd := range p.ch {
143 func (p *handlerPuppet) done() { close(p.ch) }
144 func (p *handlerPuppet) do(fn func(http.ResponseWriter, *http.Request)) {
145 done := make(chan bool)
146 p.ch <- puppetCommand{fn, done}
149 func dockerLogs(container string) ([]byte, error) {
150 out, err := exec.Command("docker", "wait", container).CombinedOutput()
154 exitStatus, err := strconv.Atoi(strings.TrimSpace(string(out)))
156 return out, errors.New("unexpected exit status from docker wait")
158 out, err = exec.Command("docker", "logs", container).CombinedOutput()
159 exec.Command("docker", "rm", container).Run()
160 if err == nil && exitStatus != 0 {
161 err = fmt.Errorf("exit status %d: %s", exitStatus, out)
166 func kill(container string) {
167 exec.Command("docker", "kill", container).Run()
168 exec.Command("docker", "rm", container).Run()
171 func cleanDate(res *http.Response) {
172 if d := res.Header["Date"]; len(d) == 1 {
177 func TestSorterPoolAllocs(t *testing.T) {
178 ss := []string{"a", "b", "c"}
184 sorter := new(sorter)
186 if allocs := testing.AllocsPerRun(100, func() {
187 sorter.SortStrings(ss)
189 t.Logf("SortStrings allocs = %v; want <1", allocs)
192 if allocs := testing.AllocsPerRun(5, func() {
193 if len(sorter.Keys(h)) != 3 {
194 t.Fatal("wrong result")
197 t.Logf("Keys allocs = %v; want <1", allocs)