Source file src/net/http/internal/http3/http3_test.go

     1  // Copyright 2024 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 http3
     6  
     7  import (
     8  	"encoding/hex"
     9  	"errors"
    10  	"strings"
    11  	"sync"
    12  	"testing"
    13  	"testing/synctest"
    14  )
    15  
    16  func unhex(s string) []byte {
    17  	b, err := hex.DecodeString(strings.Map(func(c rune) rune {
    18  		switch c {
    19  		case ' ', '\t', '\n':
    20  			return -1 // ignore
    21  		}
    22  		return c
    23  	}, s))
    24  	if err != nil {
    25  		panic(err)
    26  	}
    27  	return b
    28  }
    29  
    30  // testReader implements io.Reader.
    31  type testReader struct {
    32  	readFunc func([]byte) (int, error)
    33  }
    34  
    35  func (r testReader) Read(p []byte) (n int, err error) { return r.readFunc(p) }
    36  
    37  var errTestBodyClosed = errors.New("test body closed")
    38  
    39  // testRequestBody is a Request.Body which blocks reads until it is closed,
    40  // and records the number of times it has been closed.
    41  type testRequestBody struct {
    42  	closec chan struct{}
    43  
    44  	mu     sync.Mutex
    45  	closes int
    46  }
    47  
    48  func newTestRequestBody() *testRequestBody {
    49  	return &testRequestBody{closec: make(chan struct{})}
    50  }
    51  
    52  // Read blocks until the body is closed.
    53  func (b *testRequestBody) Read([]byte) (int, error) {
    54  	<-b.closec
    55  	return 0, errTestBodyClosed
    56  }
    57  
    58  func (b *testRequestBody) Close() error {
    59  	b.mu.Lock()
    60  	defer b.mu.Unlock()
    61  	if b.closes++; b.closes == 1 {
    62  		close(b.closec)
    63  	}
    64  	return nil
    65  }
    66  
    67  // closeCount returns the number of times the body has been closed.
    68  func (b *testRequestBody) closeCount() int {
    69  	b.mu.Lock()
    70  	defer b.mu.Unlock()
    71  	return b.closes
    72  }
    73  
    74  // synctestSubtest runs f in a subtest in a synctest.Run bubble.
    75  func synctestSubtest(t *testing.T, name string, f func(t *testing.T)) {
    76  	t.Run(name, func(t *testing.T) {
    77  		synctest.Test(t, f)
    78  	})
    79  }
    80  

View as plain text