github.com/matrixorigin/matrixone@v1.2.0/pkg/fileservice/retryable_reader_test.go (about)

     1  // Copyright 2023 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package fileservice
    16  
    17  import (
    18  	"bytes"
    19  	"crypto/rand"
    20  	"io"
    21  	mathrand "math/rand"
    22  	"testing"
    23  	"testing/iotest"
    24  
    25  	"github.com/stretchr/testify/assert"
    26  )
    27  
    28  func TestRetryableReaderIOTest(t *testing.T) {
    29  	data := make([]byte, 4096)
    30  	_, err := rand.Read(data)
    31  	assert.Nil(t, err)
    32  
    33  	r, err := newRetryableReader(func(offset int64) (io.ReadCloser, error) {
    34  		return io.NopCloser(bytes.NewReader(data[offset:])), nil
    35  	}, 0, IsRetryableError)
    36  	assert.Nil(t, err)
    37  
    38  	err = iotest.TestReader(r, data)
    39  	assert.Nil(t, err)
    40  }
    41  
    42  type unexpectedEOFReader struct {
    43  	r io.Reader
    44  }
    45  
    46  var _ io.Reader = new(unexpectedEOFReader)
    47  
    48  func (u *unexpectedEOFReader) Read(buf []byte) (int, error) {
    49  	if mathrand.Intn(2) == 0 {
    50  		return 0, io.ErrUnexpectedEOF
    51  	}
    52  	return u.r.Read(buf)
    53  }
    54  
    55  func TestRetryableReaderUnexpectedEOFIOTest(t *testing.T) {
    56  	data := make([]byte, 4096)
    57  	_, err := rand.Read(data)
    58  	assert.Nil(t, err)
    59  
    60  	r, err := newRetryableReader(func(offset int64) (io.ReadCloser, error) {
    61  		return io.NopCloser(&unexpectedEOFReader{
    62  			r: bytes.NewReader(data[offset:]),
    63  		}), nil
    64  	}, 0, IsRetryableError)
    65  	assert.Nil(t, err)
    66  
    67  	err = iotest.TestReader(r, data)
    68  	assert.Nil(t, err)
    69  }
    70  
    71  func TestRetryableReaderUnexpectedEOF(t *testing.T) {
    72  	data := make([]byte, 4096)
    73  	_, err := rand.Read(data)
    74  	assert.Nil(t, err)
    75  
    76  	for i := 0; i < 1024; i++ {
    77  		r, err := newRetryableReader(func(offset int64) (io.ReadCloser, error) {
    78  			return io.NopCloser(&unexpectedEOFReader{
    79  				r: bytes.NewReader(data[offset:]),
    80  			}), nil
    81  		}, 0, IsRetryableError)
    82  		assert.Nil(t, err)
    83  		got, err := io.ReadAll(r)
    84  		assert.Nil(t, err)
    85  		assert.Equal(t, data, got)
    86  	}
    87  }