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