github.com/m3db/m3@v1.5.0/src/dbnode/digest/writer_test.go (about)

     1  // Copyright (c) 2016 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 digest
    22  
    23  import (
    24  	"bufio"
    25  	"errors"
    26  	"io/ioutil"
    27  	"os"
    28  	"testing"
    29  
    30  	"github.com/stretchr/testify/require"
    31  )
    32  
    33  const (
    34  	testWriterBufferSize = 10
    35  )
    36  
    37  func createTestFdWithDigestWriter(t *testing.T) (*fdWithDigestWriter, *os.File, *mockDigest) {
    38  	fd, md := createTestFdWithDigest(t)
    39  	writer := NewFdWithDigestWriter(testWriterBufferSize).(*fdWithDigestWriter)
    40  	writer.FdWithDigest.(*fdWithDigest).digest = md
    41  	writer.writer = bufio.NewWriterSize(nil, 2)
    42  	writer.Reset(fd)
    43  	return writer, fd, md
    44  }
    45  
    46  func createTestFdWithDigestContentsWriter(t *testing.T) (*fdWithDigestContentsWriter, *os.File, *mockDigest) {
    47  	fwd, fd, md := createTestFdWithDigestWriter(t)
    48  	writer := NewFdWithDigestContentsWriter(testWriterBufferSize).(*fdWithDigestContentsWriter)
    49  	writer.FdWithDigestWriter = fwd
    50  	return writer, fd, md
    51  }
    52  
    53  func TestFdWithDigestWriterReset(t *testing.T) {
    54  	writer, _, _ := createTestFdWithDigestWriter(t)
    55  	require.NotNil(t, writer.Fd())
    56  	writer.Reset(nil)
    57  	require.Nil(t, writer.Fd())
    58  }
    59  
    60  func TestFdWithDigestWriteBytesFileWriteError(t *testing.T) {
    61  	writer, fd, _ := createTestFdWithDigestWriter(t)
    62  	defer func() {
    63  		fd.Close()
    64  		os.Remove(fd.Name())
    65  	}()
    66  
    67  	writer.Reset(nil)
    68  	_, err := writer.Write([]byte{0x1, 0x2, 0x3})
    69  	require.Error(t, err)
    70  }
    71  
    72  func TestFdWithDigestWriteBytesDigestWriteError(t *testing.T) {
    73  	writer, fd, md := createTestFdWithDigestWriter(t)
    74  	defer func() {
    75  		fd.Close()
    76  		os.Remove(fd.Name())
    77  	}()
    78  
    79  	md.err = errors.New("foo")
    80  	_, err := writer.Write([]byte{0x1, 0x2, 0x3})
    81  	require.Equal(t, "foo", err.Error())
    82  }
    83  
    84  func TestFdWithDigestWriteBytesSuccess(t *testing.T) {
    85  	writer, fd, md := createTestFdWithDigestWriter(t)
    86  	defer func() {
    87  		fd.Close()
    88  		os.Remove(fd.Name())
    89  	}()
    90  
    91  	md.digest = 123
    92  	data := []byte{0x1, 0x2, 0x3}
    93  	res, err := writer.Write(data)
    94  	require.NoError(t, err)
    95  	require.Equal(t, len(data), res)
    96  	require.Equal(t, data, md.b)
    97  }
    98  
    99  func TestFdWithDigestWriterCloseSuccess(t *testing.T) {
   100  	writer, fd, _ := createTestFdWithDigestWriter(t)
   101  	defer func() {
   102  		fd.Close()
   103  		os.Remove(fd.Name())
   104  	}()
   105  
   106  	require.NotNil(t, writer.Fd())
   107  	require.NoError(t, writer.Close())
   108  	require.Nil(t, writer.Fd())
   109  }
   110  
   111  func TestFdWithDigestWriteDigestsError(t *testing.T) {
   112  	writer, fd, _ := createTestFdWithDigestContentsWriter(t)
   113  	defer func() {
   114  		fd.Close()
   115  		os.Remove(fd.Name())
   116  	}()
   117  
   118  	writer.Reset(nil)
   119  	require.Error(t, writer.WriteDigests(1, 2))
   120  }
   121  
   122  func TestFdWithDigestWriteDigestsSuccess(t *testing.T) {
   123  	writer, fd, md := createTestFdWithDigestContentsWriter(t)
   124  	defer func() {
   125  		fd.Close()
   126  		os.Remove(fd.Name())
   127  	}()
   128  
   129  	expected := []byte{0x1, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0}
   130  	require.NoError(t, writer.WriteDigests(1, 2))
   131  	require.Equal(t, expected, md.b)
   132  
   133  	_, err := fd.Seek(0, 0)
   134  	require.NoError(t, err)
   135  	b, err := ioutil.ReadAll(fd)
   136  	require.NoError(t, err)
   137  	require.Equal(t, expected, b)
   138  }