Dragonfly2

Форк
0
/
multi_readcloser_test.go 
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

17
package io
18

19
import (
20
	"errors"
21
	"testing"
22

23
	"github.com/stretchr/testify/assert"
24
)
25

26
type mockReadCloser struct{}
27

28
func (m *mockReadCloser) Read(p []byte) (int, error) {
29
	return 1, nil
30
}
31

32
func (m *mockReadCloser) Close() error {
33
	return nil
34
}
35

36
type mockReadCloserWithReadError struct{}
37

38
func (m *mockReadCloserWithReadError) Read(p []byte) (int, error) {
39
	return 0, errors.New("foo")
40
}
41

42
func (m *mockReadCloserWithReadError) Close() error {
43
	return nil
44
}
45

46
type mockReadCloserWithCloseError struct{}
47

48
func (m *mockReadCloserWithCloseError) Read(p []byte) (int, error) {
49
	return 1, nil
50
}
51

52
func (m *mockReadCloserWithCloseError) Close() error {
53
	return errors.New("foo")
54
}
55

56
func TestMultiReadCloser(t *testing.T) {
57
	assert := assert.New(t)
58
	readCloser := MultiReadCloser(&mockReadCloser{})
59
	n, err := readCloser.Read([]byte{})
60
	assert.NoError(err)
61
	assert.Equal(n, 1)
62

63
	err = readCloser.Close()
64
	assert.NoError(err)
65

66
	readCloser = MultiReadCloser(&mockReadCloserWithReadError{})
67
	n, err = readCloser.Read([]byte{})
68
	assert.Error(err)
69
	assert.Equal(n, 0)
70

71
	err = readCloser.Close()
72
	assert.NoError(err)
73

74
	readCloser = MultiReadCloser(&mockReadCloserWithCloseError{})
75
	n, err = readCloser.Read([]byte{})
76
	assert.NoError(err)
77
	assert.Equal(n, 1)
78

79
	err = readCloser.Close()
80
	assert.Error(err)
81
}
82

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.