|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2023 ArangoDB GmbH, Cologne, Germany |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | +// |
| 20 | + |
| 21 | +package nctx |
| 22 | + |
| 23 | +import ( |
| 24 | + "bytes" |
| 25 | + "context" |
| 26 | + "io" |
| 27 | + "testing" |
| 28 | + |
| 29 | + "github.com/stretchr/testify/require" |
| 30 | +) |
| 31 | + |
| 32 | +func counterRequestReadMock(t *testing.T, ctx context.Context, data io.Reader) int { |
| 33 | + data = WithRequestReadBytes(ctx, data) |
| 34 | + dz, err := io.ReadAll(data) |
| 35 | + require.NoError(t, err) |
| 36 | + return len(dz) |
| 37 | +} |
| 38 | + |
| 39 | +func Test_Counter_RequestRead(t *testing.T) { |
| 40 | + data := make([]byte, 64) |
| 41 | + |
| 42 | + var c Counter |
| 43 | + |
| 44 | + t.Run("Read without wrapper", func(t *testing.T) { |
| 45 | + require.EqualValues(t, 64, counterRequestReadMock(t, context.Background(), bytes.NewReader(data))) |
| 46 | + require.EqualValues(t, 0, c.Get()) |
| 47 | + }) |
| 48 | + |
| 49 | + t.Run("Read with wrapper", func(t *testing.T) { |
| 50 | + require.EqualValues(t, 64, counterRequestReadMock(t, c.WithRequestReadBytes(context.Background()), bytes.NewReader(data))) |
| 51 | + require.EqualValues(t, 64, c.Get()) |
| 52 | + }) |
| 53 | +} |
0 commit comments