Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(763)

Delta Between Two Patch Sets: src/pkg/http/serve_test.go

Issue 5228041: code review 5228041: http: cancel a timer in a test (Closed)
Left Patch Set: diff -r a71b3310c793 https://go.googlecode.com/hg/ Created 13 years, 5 months ago
Right Patch Set: diff -r 60279075a700 https://go.googlecode.com/hg/ Created 13 years, 5 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 // Copyright 2010 The Go Authors. All rights reserved. 1 // Copyright 2010 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style 2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file. 3 // license that can be found in the LICENSE file.
4 4
5 // End-to-end serving tests 5 // End-to-end serving tests
6 6
7 package http_test 7 package http_test
8 8
9 import ( 9 import (
10 "bufio" 10 "bufio"
(...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after
349 // Verify that the connection is closed when the declared Content-Length 349 // Verify that the connection is closed when the declared Content-Length
350 // is larger than what the handler wrote. 350 // is larger than what the handler wrote.
351 conn, err := net.Dial("tcp", ts.Listener.Addr().String()) 351 conn, err := net.Dial("tcp", ts.Listener.Addr().String())
352 if err != nil { 352 if err != nil {
353 t.Fatalf("error dialing: %v", err) 353 t.Fatalf("error dialing: %v", err)
354 } 354 }
355 _, err = conn.Write([]byte("GET /?underwrite=1 HTTP/1.1\r\nHost: foo\r\n \r\n")) 355 _, err = conn.Write([]byte("GET /?underwrite=1 HTTP/1.1\r\nHost: foo\r\n \r\n"))
356 if err != nil { 356 if err != nil {
357 t.Fatalf("error writing: %v", err) 357 t.Fatalf("error writing: %v", err)
358 } 358 }
359 » // The next ReadAll will hang for a failing test, so use a Timer instead 359
360 » // to fail more traditionally 360 » // The ReadAll will hang for a failing test, so use a Timer to
361 » timer := time.AfterFunc(2e9, func() { 361 » // fail explicitly.
362 » » t.Fatalf("Timeout expired in ReadAll.") 362 » goTimeout(t, 2e9, func() {
363 » » got, _ := ioutil.ReadAll(conn)
364 » » expectedSuffix := "\r\n\r\ntoo short"
365 » » if !strings.HasSuffix(string(got), expectedSuffix) {
366 » » » t.Errorf("Expected output to end with %q; got response b ody %q",
367 » » » » expectedSuffix, string(got))
368 » » }
363 }) 369 })
364 defer timer.Stop()
365 got, _ := ioutil.ReadAll(conn)
366 expectedSuffix := "\r\n\r\ntoo short"
367 if !strings.HasSuffix(string(got), expectedSuffix) {
368 t.Fatalf("Expected output to end with %q; got response body %q",
369 expectedSuffix, string(got))
370 }
371 } 370 }
372 371
373 func testTcpConnectionCloses(t *testing.T, req string, h Handler) { 372 func testTcpConnectionCloses(t *testing.T, req string, h Handler) {
374 s := httptest.NewServer(h) 373 s := httptest.NewServer(h)
375 defer s.Close() 374 defer s.Close()
376 375
377 conn, err := net.Dial("tcp", s.Listener.Addr().String()) 376 conn, err := net.Dial("tcp", s.Listener.Addr().String())
378 if err != nil { 377 if err != nil {
379 t.Fatal("dial error:", err) 378 t.Fatal("dial error:", err)
380 } 379 }
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
542 } 541 }
543 ts := httptest.NewUnstartedServer(HandlerFunc(func(w ResponseWriter, r * Request) {})) 542 ts := httptest.NewUnstartedServer(HandlerFunc(func(w ResponseWriter, r * Request) {}))
544 ts.Config.ReadTimeout = 250e6 543 ts.Config.ReadTimeout = 250e6
545 ts.StartTLS() 544 ts.StartTLS()
546 defer ts.Close() 545 defer ts.Close()
547 conn, err := net.Dial("tcp", ts.Listener.Addr().String()) 546 conn, err := net.Dial("tcp", ts.Listener.Addr().String())
548 if err != nil { 547 if err != nil {
549 t.Fatalf("Dial: %v", err) 548 t.Fatalf("Dial: %v", err)
550 } 549 }
551 defer conn.Close() 550 defer conn.Close()
552 » timer := time.AfterFunc(10e9, func() { t.Fatalf("Timeout") }) 551 » goTimeout(t, 10e9, func() {
553 » defer timer.Stop() 552 » » var buf [1]byte
554 553 » » n, err := conn.Read(buf[:])
555 » var buf [1]byte 554 » » if err == nil || n != 0 {
556 » n, err := conn.Read(buf[:]) 555 » » » t.Errorf("Read = %d, %v; want an error and no bytes", n, err)
557 » if err == nil || n != 0 { 556 » » }
558 » » t.Errorf("Read = %d, %v; want an error and no bytes", n, err) 557 » })
559 » }
560 } 558 }
561 559
562 func TestTLSServer(t *testing.T) { 560 func TestTLSServer(t *testing.T) {
563 ts := httptest.NewTLSServer(HandlerFunc(func(w ResponseWriter, r *Reques t) { 561 ts := httptest.NewTLSServer(HandlerFunc(func(w ResponseWriter, r *Reques t) {
564 if r.TLS != nil { 562 if r.TLS != nil {
565 w.Header().Set("X-TLS-Set", "true") 563 w.Header().Set("X-TLS-Set", "true")
566 if r.TLS.HandshakeComplete { 564 if r.TLS.HandshakeComplete {
567 w.Header().Set("X-TLS-HandshakeComplete", "true" ) 565 w.Header().Set("X-TLS-HandshakeComplete", "true" )
568 } 566 }
569 } 567 }
570 })) 568 }))
571 defer ts.Close() 569 defer ts.Close()
572 570
573 // Connect an idle TCP connection to this server before we run 571 // Connect an idle TCP connection to this server before we run
574 // our real tests. This idle connection used to block forever 572 // our real tests. This idle connection used to block forever
575 // in the TLS handshake, preventing future connections from 573 // in the TLS handshake, preventing future connections from
576 // being accepted. It may prevent future accidental blocking 574 // being accepted. It may prevent future accidental blocking
577 // in newConn. 575 // in newConn.
578 idleConn, err := net.Dial("tcp", ts.Listener.Addr().String()) 576 idleConn, err := net.Dial("tcp", ts.Listener.Addr().String())
579 if err != nil { 577 if err != nil {
580 t.Fatalf("Dial: %v", err) 578 t.Fatalf("Dial: %v", err)
581 } 579 }
582 defer idleConn.Close() 580 defer idleConn.Close()
583 » timer := time.AfterFunc(10e9, func() { t.Fatalf("Timeout") }) 581 » goTimeout(t, 10e9, func() {
584 » defer timer.Stop() 582 » » if !strings.HasPrefix(ts.URL, "https://") {
585 583 » » » t.Errorf("expected test TLS server to start with https:/ /, got %q", ts.URL)
586 » if !strings.HasPrefix(ts.URL, "https://") { 584 » » » return
587 » » t.Fatalf("expected test TLS server to start with https://, got % q", ts.URL) 585 » » }
588 » } 586 » » res, err := Get(ts.URL)
589 » res, err := Get(ts.URL) 587 » » if err != nil {
590 » if err != nil { 588 » » » t.Error(err)
591 » » t.Fatal(err) 589 » » » return
592 » } 590 » » }
593 » if res == nil { 591 » » if res == nil {
594 » » t.Fatalf("got nil Response") 592 » » » t.Errorf("got nil Response")
595 » } 593 » » » return
596 » defer res.Body.Close() 594 » » }
597 » if res.Header.Get("X-TLS-Set") != "true" { 595 » » defer res.Body.Close()
598 » » t.Errorf("expected X-TLS-Set response header") 596 » » if res.Header.Get("X-TLS-Set") != "true" {
599 » } 597 » » » t.Errorf("expected X-TLS-Set response header")
600 » if res.Header.Get("X-TLS-HandshakeComplete") != "true" { 598 » » » return
601 » » t.Errorf("expected X-TLS-HandshakeComplete header") 599 » » }
602 » } 600 » » if res.Header.Get("X-TLS-HandshakeComplete") != "true" {
601 » » » t.Errorf("expected X-TLS-HandshakeComplete header")
602 » » }
603 » })
603 } 604 }
604 605
605 type serverExpectTest struct { 606 type serverExpectTest struct {
606 contentLength int // of request body 607 contentLength int // of request body
607 expectation string // e.g. "100-continue" 608 expectation string // e.g. "100-continue"
608 readBody bool // whether handler should read the body (if fals e, sends StatusUnauthorized) 609 readBody bool // whether handler should read the body (if fals e, sends StatusUnauthorized)
609 expectedResponse string // expected substring in first line of http resp onse 610 expectedResponse string // expected substring in first line of http resp onse
610 } 611 }
611 612
612 var serverExpectTests = []serverExpectTest{ 613 var serverExpectTests = []serverExpectTest{
(...skipping 400 matching lines...) Expand 10 before | Expand all | Expand 10 after
1013 t.Errorf("read %q from server; want nothing", got) 1014 t.Errorf("read %q from server; want nothing", got)
1014 } 1015 }
1015 }() 1016 }()
1016 select { 1017 select {
1017 case <-donec: 1018 case <-donec:
1018 case <-time.After(10e9): 1019 case <-time.After(10e9):
1019 t.Fatalf("timeout") 1020 t.Fatalf("timeout")
1020 } 1021 }
1021 } 1022 }
1022 1023
1024 // goTimeout runs f, failing t if f takes more than ns to complete.
1025 func goTimeout(t *testing.T, ns int64, f func()) {
1026 ch := make(chan bool, 2)
1027 timer := time.AfterFunc(ns, func() {
1028 t.Errorf("Timeout expired after %d ns", ns)
1029 ch <- true
1030 })
1031 defer timer.Stop()
1032 go func() {
1033 defer func() { ch <- true }()
1034 f()
1035 }()
1036 <-ch
1037 }
1038
1023 type errorListener struct { 1039 type errorListener struct {
1024 errs []os.Error 1040 errs []os.Error
1025 } 1041 }
1026 1042
1027 func (l *errorListener) Accept() (c net.Conn, err os.Error) { 1043 func (l *errorListener) Accept() (c net.Conn, err os.Error) {
1028 if len(l.errs) == 0 { 1044 if len(l.errs) == 0 {
1029 return nil, os.EOF 1045 return nil, os.EOF
1030 } 1046 }
1031 err = l.errs[0] 1047 err = l.errs[0]
1032 l.errs = l.errs[1:] 1048 l.errs = l.errs[1:]
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
1074 panic("ReadAll: " + err.String()) 1090 panic("ReadAll: " + err.String())
1075 } 1091 }
1076 body := string(all) 1092 body := string(all)
1077 if body != "Hello world.\n" { 1093 if body != "Hello world.\n" {
1078 panic("Got body: " + body) 1094 panic("Got body: " + body)
1079 } 1095 }
1080 } 1096 }
1081 1097
1082 b.StopTimer() 1098 b.StopTimer()
1083 } 1099 }
LEFTRIGHT
« no previous file | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b