Dragonfly2
81 строка · 1.9 Кб
1/*
2* Copyright 2022 The Dragonfly Authors
3*
4* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License.
6* You may obtain a copy of the License at
7*
8* http://www.apache.org/licenses/LICENSE-2.0
9*
10* Unless required by applicable law or agreed to in writing, software
11* distributed under the License is distributed on an "AS IS" BASIS,
12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13* See the License for the specific language governing permissions and
14* limitations under the License.
15*/
16
17package io18
19import (20"errors"21"testing"22
23"github.com/stretchr/testify/assert"24)
25
26type mockReadCloser struct{}27
28func (m *mockReadCloser) Read(p []byte) (int, error) {29return 1, nil30}
31
32func (m *mockReadCloser) Close() error {33return nil34}
35
36type mockReadCloserWithReadError struct{}37
38func (m *mockReadCloserWithReadError) Read(p []byte) (int, error) {39return 0, errors.New("foo")40}
41
42func (m *mockReadCloserWithReadError) Close() error {43return nil44}
45
46type mockReadCloserWithCloseError struct{}47
48func (m *mockReadCloserWithCloseError) Read(p []byte) (int, error) {49return 1, nil50}
51
52func (m *mockReadCloserWithCloseError) Close() error {53return errors.New("foo")54}
55
56func TestMultiReadCloser(t *testing.T) {57assert := assert.New(t)58readCloser := MultiReadCloser(&mockReadCloser{})59n, err := readCloser.Read([]byte{})60assert.NoError(err)61assert.Equal(n, 1)62
63err = readCloser.Close()64assert.NoError(err)65
66readCloser = MultiReadCloser(&mockReadCloserWithReadError{})67n, err = readCloser.Read([]byte{})68assert.Error(err)69assert.Equal(n, 0)70
71err = readCloser.Close()72assert.NoError(err)73
74readCloser = MultiReadCloser(&mockReadCloserWithCloseError{})75n, err = readCloser.Read([]byte{})76assert.NoError(err)77assert.Equal(n, 1)78
79err = readCloser.Close()80assert.Error(err)81}
82