github.com/Andyfoo/golang/x/net@v0.0.0-20190901054642-57c1bf301704/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  	// Read and Write should fail.
    96  	if n, err := p.Write([]byte("abc")); err != errClosedPipeWrite || n != 0 {
    97  		t.Errorf("Write(abc) after close\ngot %v, %v\nwant 0, %v", n, err, errClosedPipeWrite)
    98  	}
    99  	if n, err := p.Read(make([]byte, 1)); err == nil || n != 0 {
   100  		t.Errorf("Read() after close\ngot %v, nil\nwant 0, %v", n, errClosedPipeWrite)
   101  	}
   102  }
   103  
   104  func TestPipeBreakWithError(t *testing.T) {
   105  	p := &pipe{b: new(bytes.Buffer)}
   106  	io.WriteString(p, "foo")
   107  	a := errors.New("test err")
   108  	p.BreakWithError(a)
   109  	all, err := ioutil.ReadAll(p)
   110  	if string(all) != "" {
   111  		t.Errorf("read bytes = %q; want empty string", all)
   112  	}
   113  	if err != a {
   114  		t.Logf("read error = %v, %v", err, a)
   115  	}
   116  	if p.b != nil {
   117  		t.Errorf("buffer should be nil after BreakWithError")
   118  	}
   119  	// Write should succeed silently.
   120  	if n, err := p.Write([]byte("abc")); err != nil || n != 3 {
   121  		t.Errorf("Write(abc) after break\ngot %v, %v\nwant 0, nil", n, err)
   122  	}
   123  	if p.b != nil {
   124  		t.Errorf("buffer should be nil after Write")
   125  	}
   126  	// Read should fail.
   127  	if n, err := p.Read(make([]byte, 1)); err == nil || n != 0 {
   128  		t.Errorf("Read() after close\ngot %v, nil\nwant 0, not nil", n)
   129  	}
   130  }