-
Notifications
You must be signed in to change notification settings - Fork 0
/
token_reader_test.go
188 lines (148 loc) · 4.92 KB
/
token_reader_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright 2024 Aerospike, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package backup
import (
"io"
"log/slog"
"testing"
"github.com/aerospike/backup-go/mocks"
"github.com/aerospike/backup-go/models"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
// MockReadCloser is a mock implementation of io.ReadCloser
type MockReadCloser struct {
mock.Mock
}
func (m *MockReadCloser) Read(p []byte) (n int, err error) {
args := m.Called(p)
return args.Int(0), args.Error(1)
}
func (m *MockReadCloser) Close() error {
args := m.Called()
return args.Error(0)
}
func TestTokenReader_ReadSingleToken(t *testing.T) {
logger := slog.Default()
readersCh := make(chan io.ReadCloser, 1)
mockReader := new(MockReadCloser)
readersCh <- mockReader
mockDecoder := mocks.NewMockDecoder(t)
mockDecoder.EXPECT().NextToken().Return(&models.Token{Type: models.TokenTypeRecord}, nil).Once()
convertFn := func(io.ReadCloser) Decoder {
return mockDecoder
}
tr := newTokenReader(readersCh, logger, convertFn)
token, err := tr.Read()
assert.NoError(t, err)
assert.NotNil(t, token)
assert.Equal(t, models.TokenTypeRecord, token.Type)
mockDecoder.AssertExpectations(t)
}
func TestTokenReader_ReadMultipleTokensFromSingleReader(t *testing.T) {
logger := slog.Default()
readersCh := make(chan io.ReadCloser, 1)
mockReader := new(MockReadCloser)
readersCh <- mockReader
mockDecoder := mocks.NewMockDecoder(t)
mockDecoder.EXPECT().NextToken().Return(&models.Token{Type: models.TokenTypeRecord}, nil).Times(3)
mockDecoder.EXPECT().NextToken().Return((*models.Token)(nil), io.EOF).Once()
convertFn := func(io.ReadCloser) Decoder {
return mockDecoder
}
tr := newTokenReader(readersCh, logger, convertFn)
for i := 0; i < 3; i++ {
token, err := tr.Read()
assert.NoError(t, err)
assert.NotNil(t, token)
assert.Equal(t, models.TokenTypeRecord, token.Type)
}
close(readersCh)
mockReader.On("Close").Return(nil).Once()
_, err := tr.Read()
assert.Equal(t, io.EOF, err)
mockDecoder.AssertExpectations(t)
mockReader.AssertExpectations(t)
}
func TestTokenReader_ReadFromMultipleReaders(t *testing.T) {
logger := slog.Default()
readersCh := make(chan io.ReadCloser, 2)
mockReader1 := new(MockReadCloser)
mockReader2 := new(MockReadCloser)
readersCh <- mockReader1
readersCh <- mockReader2
mockDecoder1 := mocks.NewMockDecoder(t)
mockDecoder1.EXPECT().NextToken().Return(&models.Token{Type: models.TokenTypeRecord}, nil).Once()
mockDecoder1.EXPECT().NextToken().Return((*models.Token)(nil), io.EOF).Once()
mockDecoder2 := mocks.NewMockDecoder(t)
mockDecoder2.EXPECT().NextToken().Return(&models.Token{Type: models.TokenTypeUDF}, nil).Once()
currentDecoder := mockDecoder1
convertFn := func(io.ReadCloser) Decoder {
defer func() {
currentDecoder = mockDecoder2
}()
return currentDecoder
}
tr := newTokenReader(readersCh, logger, convertFn)
// Read from first decoder
token, err := tr.Read()
assert.NoError(t, err)
assert.NotNil(t, token)
assert.Equal(t, models.TokenTypeRecord, token.Type)
mockReader1.On("Close").Return(nil).Once()
// Read from second decoder
token, err = tr.Read()
assert.NoError(t, err)
assert.NotNil(t, token)
assert.Equal(t, models.TokenTypeUDF, token.Type)
mockDecoder1.AssertExpectations(t)
mockDecoder2.AssertExpectations(t)
mockReader1.AssertExpectations(t)
}
func TestTokenReader_ReadFromClosedChannel(t *testing.T) {
logger := slog.Default()
readersCh := make(chan io.ReadCloser)
close(readersCh)
tr := newTokenReader(readersCh, logger, nil)
token, err := tr.Read()
assert.Equal(t, io.EOF, err)
assert.Nil(t, token)
}
func TestTokenReader_ReadWithDecoderError(t *testing.T) {
logger := slog.Default()
readersCh := make(chan io.ReadCloser, 1)
mockReader := new(MockReadCloser)
readersCh <- mockReader
mockDecoder := mocks.NewMockDecoder(t)
expectedErr := io.ErrUnexpectedEOF
mockDecoder.EXPECT().NextToken().Return((*models.Token)(nil), expectedErr).Once()
convertFn := func(io.ReadCloser) Decoder {
return mockDecoder
}
tr := newTokenReader(readersCh, logger, convertFn)
token, err := tr.Read()
assert.Error(t, err)
assert.Equal(t, expectedErr, err)
assert.Nil(t, token)
mockDecoder.AssertExpectations(t)
}
func TestTokenReader_Close(t *testing.T) {
logger := slog.Default()
readersCh := make(chan io.ReadCloser)
tr := newTokenReader(readersCh, logger, nil)
// Close is a no-op, so we just ensure it doesn't panic
assert.NotPanics(t, func() {
tr.Close()
})
}