github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/net/http2/pipe_test.go (about) 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. 4 5 package http2 6 7 import ( 8 "bytes" 9 "errors" 10 "io" 11 "io/ioutil" 12 "testing" 13 ) 14 15 func TestPipeClose(t *testing.T) { 16 var p pipe 17 p.b = new(bytes.Buffer) 18 a := errors.New("a") 19 b := errors.New("b") 20 p.CloseWithError(a) 21 p.CloseWithError(b) 22 _, err := p.Read(make([]byte, 1)) 23 if err != a { 24 t.Errorf("err = %v want %v", err, a) 25 } 26 } 27 28 func TestPipeDoneChan(t *testing.T) { 29 var p pipe 30 done := p.Done() 31 select { 32 case <-done: 33 t.Fatal("done too soon") 34 default: 35 } 36 p.CloseWithError(io.EOF) 37 select { 38 case <-done: 39 default: 40 t.Fatal("should be done") 41 } 42 } 43 44 func TestPipeDoneChan_ErrFirst(t *testing.T) { 45 var p pipe 46 p.CloseWithError(io.EOF) 47 done := p.Done() 48 select { 49 case <-done: 50 default: 51 t.Fatal("should be done") 52 } 53 } 54 55 func TestPipeDoneChan_Break(t *testing.T) { 56 var p pipe 57 done := p.Done() 58 select { 59 case <-done: 60 t.Fatal("done too soon") 61 default: 62 } 63 p.BreakWithError(io.EOF) 64 select { 65 case <-done: 66 default: 67 t.Fatal("should be done") 68 } 69 } 70 71 func TestPipeDoneChan_Break_ErrFirst(t *testing.T) { 72 var p pipe 73 p.BreakWithError(io.EOF) 74 done := p.Done() 75 select { 76 case <-done: 77 default: 78 t.Fatal("should be done") 79 } 80 } 81 82 func TestPipeCloseWithError(t *testing.T) { 83 p := &pipe{b: new(bytes.Buffer)} 84 const body = "foo" 85 io.WriteString(p, body) 86 a := errors.New("test error") 87 p.CloseWithError(a) 88 all, err := ioutil.ReadAll(p) 89 if string(all) != body { 90 t.Errorf("read bytes = %q; want %q", all, body) 91 } 92 if err != a { 93 t.Logf("read error = %v, %v", err, a) 94 } 95 } 96 97 func TestPipeBreakWithError(t *testing.T) { 98 p := &pipe{b: new(bytes.Buffer)} 99 io.WriteString(p, "foo") 100 a := errors.New("test err") 101 p.BreakWithError(a) 102 all, err := ioutil.ReadAll(p) 103 if string(all) != "" { 104 t.Errorf("read bytes = %q; want empty string", all) 105 } 106 if err != a { 107 t.Logf("read error = %v, %v", err, a) 108 } 109 }