Debian Codegraph @

instrumentation/github.com/gorilla/mux/otelmux/internal/request/body_wrapper_test.go

raw
1 // Code generated by gotmpl. DO NOT MODIFY.
2 // source: internal/shared/request/body_wrapper_test.go.tmpl
3
4 // Copyright The OpenTelemetry Authors
5 // SPDX-License-Identifier: Apache-2.0
6
7 package request
8
9 import (
10 "errors"
11 "io"
12 "strings"
13 "testing"
14 "time"
15
16 "github.com/stretchr/testify/assert"
17 "github.com/stretchr/testify/require"
18 )
19
20 var errFirstCall = errors.New("first call")
21
22 func TestBodyWrapper(t *testing.T) {
23 bw := NewBodyWrapper(io.NopCloser(strings.NewReader("hello world")), func(int64) {})
24
25 data, err := io.ReadAll(bw)
26 require.NoError(t, err)
27 assert.Equal(t, "hello world", string(data))
28
29 assert.Equal(t, int64(11), bw.BytesRead())
30 assert.Equal(t, io.EOF, bw.Error())
31 }
32
33 type multipleErrorsReader struct {
34 calls int
35 }
36
37 type errorWrapper struct{}
38
39 func (errorWrapper) Error() string {
40 return "subsequent calls"
41 }
42
43 func (mer *multipleErrorsReader) Read([]byte) (int, error) {
44 mer.calls = mer.calls + 1
45 if mer.calls == 1 {
46 return 0, errFirstCall
47 }
48
49 return 0, errorWrapper{}
50 }
51
52 func TestBodyWrapperWithErrors(t *testing.T) {
53 bw := NewBodyWrapper(io.NopCloser(&multipleErrorsReader{}), func(int64) {})
54
55 data, err := io.ReadAll(bw)
56 require.Equal(t, errFirstCall, err)
57 assert.Empty(t, string(data))
58 require.Equal(t, errFirstCall, bw.Error())
59
60 data, err = io.ReadAll(bw)
61 require.Equal(t, errorWrapper{}, err)
62 assert.Empty(t, string(data))
63 require.Equal(t, errorWrapper{}, bw.Error())
64 }
65
66 func TestConcurrentBodyWrapper(t *testing.T) {
67 bw := NewBodyWrapper(io.NopCloser(strings.NewReader("hello world")), func(int64) {})
68
69 go func() {
70 _, _ = io.ReadAll(bw)
71 }()
72
73 assert.NotNil(t, bw.BytesRead())
74 assert.Eventually(t, func() bool {
75 return errors.Is(bw.Error(), io.EOF)
76 }, time.Second, 10*time.Millisecond)
77 }