github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/m3em/os/fs/file_reader_iter_test.go (about) 1 // Copyright (c) 2017 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package fs 22 23 import ( 24 "os" 25 "testing" 26 27 "github.com/m3db/m3/src/m3em/checksum" 28 29 "github.com/stretchr/testify/require" 30 ) 31 32 func TestIterChecksumLargeBuffer(t *testing.T) { 33 var ( 34 content = []byte("temporary file content") 35 tmpdir = newTempDir(t) 36 tmpfile = newTempFile(t, tmpdir, content) 37 ) 38 defer os.RemoveAll(tmpdir) // clean up 39 40 largeBufferSize := 100 41 iter, err := NewSizedFileReaderIter(tmpfile.Name(), largeBufferSize) 42 require.NoError(t, err) 43 var returnedBytes []byte 44 numIter := 0 45 for iter.Next() { 46 returnedBytes = append(returnedBytes, iter.Current()...) 47 numIter++ 48 } 49 require.Equal(t, 2, numIter) // once for all the data, once for returning done 50 require.NoError(t, iter.Err()) 51 require.Equal(t, content, returnedBytes) 52 require.Equal(t, checksum.Fn(content), iter.Checksum()) 53 require.Nil(t, iter.(*bufferedFileReaderIter).fileHandle) 54 } 55 56 func TestIterChecksumSmallBuffer(t *testing.T) { 57 var ( 58 content = []byte("temporary file content") 59 tmpdir = newTempDir(t) 60 tmpfile = newTempFile(t, tmpdir, content) 61 ) 62 defer os.RemoveAll(tmpdir) 63 64 largeBufferSize := 1 65 iter, err := NewSizedFileReaderIter(tmpfile.Name(), largeBufferSize) 66 require.NoError(t, err) 67 numIter := 0 68 var returnedBytes []byte 69 for iter.Next() { 70 returnedBytes = append(returnedBytes, iter.Current()...) 71 numIter++ 72 } 73 numExpectedIter := 1 + len(content) // one for each byte, plus one to return done 74 require.Equal(t, numExpectedIter, numIter) 75 require.NoError(t, iter.Err()) 76 require.Equal(t, content, returnedBytes) 77 require.Equal(t, checksum.Fn(content), iter.Checksum()) 78 require.Nil(t, iter.(*bufferedFileReaderIter).fileHandle) 79 }