github.com/m3db/m3@v1.5.0/src/dbnode/persist/fs/msgpack/encoder_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 msgpack
    22  
    23  import (
    24  	"errors"
    25  	"testing"
    26  
    27  	"github.com/m3db/m3/src/dbnode/persist/schema"
    28  
    29  	"github.com/stretchr/testify/require"
    30  )
    31  
    32  var (
    33  	errTestVarint   = errors.New("test varint error")
    34  	errTestFloat64  = errors.New("test float64 error")
    35  	errTestBytes    = errors.New("test bytes error")
    36  	errTestArrayLen = errors.New("test array len error")
    37  )
    38  
    39  func testCapturingEncoder(t *testing.T) (*Encoder, *[]interface{}) {
    40  	encoder := NewEncoder()
    41  
    42  	var result []interface{}
    43  	actualEncodeVarintFn := encoder.encodeVarintFn
    44  	encoder.encodeVarintFn = func(value int64) {
    45  		actualEncodeVarintFn(value)
    46  		result = append(result, value)
    47  	}
    48  
    49  	actualEncodeVarUintFn := encoder.encodeVarUintFn
    50  	encoder.encodeVarUintFn = func(value uint64) {
    51  		actualEncodeVarUintFn(value)
    52  		result = append(result, value)
    53  	}
    54  
    55  	actualEncodeFloat64Fn := encoder.encodeFloat64Fn
    56  	encoder.encodeFloat64Fn = func(value float64) {
    57  		actualEncodeFloat64Fn(value)
    58  		result = append(result, value)
    59  	}
    60  
    61  	actualEncodeBytesFn := encoder.encodeBytesFn
    62  	encoder.encodeBytesFn = func(value []byte) {
    63  		actualEncodeBytesFn(value)
    64  		result = append(result, value)
    65  	}
    66  
    67  	actualEncodeArrayLenFn := encoder.encodeArrayLenFn
    68  	encoder.encodeArrayLenFn = func(value int) {
    69  		actualEncodeArrayLenFn(value)
    70  		result = append(result, value)
    71  	}
    72  
    73  	return encoder, &result
    74  }
    75  
    76  func testExpectedResultForIndexInfo(t *testing.T, indexInfo schema.IndexInfo) []interface{} {
    77  	_, currRoot := numFieldsForType(rootObjectType)
    78  	_, currIndexInfo := numFieldsForType(indexInfoType)
    79  	_, currSummariesInfo := numFieldsForType(indexSummariesInfoType)
    80  	_, currIndexBloomFilterInfo := numFieldsForType(indexBloomFilterInfoType)
    81  	return []interface{}{
    82  		int64(indexInfoVersion),
    83  		currRoot,
    84  		int64(indexInfoType),
    85  		currIndexInfo,
    86  		indexInfo.BlockStart,
    87  		indexInfo.BlockSize,
    88  		indexInfo.Entries,
    89  		indexInfo.MajorVersion,
    90  		currSummariesInfo,
    91  		indexInfo.Summaries.Summaries,
    92  		currIndexBloomFilterInfo,
    93  		indexInfo.BloomFilter.NumElementsM,
    94  		indexInfo.BloomFilter.NumHashesK,
    95  		indexInfo.SnapshotTime,
    96  		int64(indexInfo.FileType),
    97  		indexInfo.SnapshotID,
    98  		int64(indexInfo.VolumeIndex),
    99  		indexInfo.MinorVersion,
   100  	}
   101  }
   102  
   103  func testExpectedResultForIndexEntry(t *testing.T, indexEntry schema.IndexEntry) []interface{} {
   104  	_, currRoot := numFieldsForType(rootObjectType)
   105  	_, currIndexEntry := numFieldsForType(indexEntryType)
   106  	return []interface{}{
   107  		int64(indexEntryVersion),
   108  		currRoot,
   109  		int64(indexEntryType),
   110  		currIndexEntry,
   111  		indexEntry.Index,
   112  		indexEntry.ID,
   113  		indexEntry.Size,
   114  		indexEntry.Offset,
   115  		indexEntry.DataChecksum,
   116  		indexEntry.EncodedTags,
   117  		int64(testIndexEntryChecksum), // Checksum auto-added to the end of the index entry
   118  	}
   119  }
   120  
   121  func testExpectedResultForLogInfo(t *testing.T, logInfo schema.LogInfo) []interface{} {
   122  	_, currRoot := numFieldsForType(rootObjectType)
   123  	_, currLogInfo := numFieldsForType(logInfoType)
   124  	return []interface{}{
   125  		int64(logInfoVersion),
   126  		currRoot,
   127  		int64(logInfoType),
   128  		currLogInfo,
   129  		logInfo.DeprecatedDoNotUseStart,
   130  		logInfo.DeprecatedDoNotUseDuration,
   131  		logInfo.Index,
   132  	}
   133  }
   134  
   135  func testExpectedResultForLogEntry(t *testing.T, logEntry schema.LogEntry) []interface{} {
   136  	_, currRoot := numFieldsForType(rootObjectType)
   137  	_, currLogEntry := numFieldsForType(logEntryType)
   138  	return []interface{}{
   139  		int64(logEntryVersion),
   140  		currRoot,
   141  		int64(logEntryType),
   142  		currLogEntry,
   143  		logEntry.Index,
   144  		logEntry.Create,
   145  		logEntry.Metadata,
   146  		logEntry.Timestamp,
   147  		logEntry.Value,
   148  		uint64(logEntry.Unit),
   149  		logEntry.Annotation,
   150  	}
   151  }
   152  
   153  func testExpectedResultForLogMetadata(t *testing.T, logMetadata schema.LogMetadata) []interface{} {
   154  	_, currRoot := numFieldsForType(rootObjectType)
   155  	_, currLogMetadata := numFieldsForType(logMetadataType)
   156  	return []interface{}{
   157  		int64(logMetadataVersion),
   158  		currRoot,
   159  		int64(logMetadataType),
   160  		currLogMetadata,
   161  		logMetadata.ID,
   162  		logMetadata.Namespace,
   163  		uint64(logMetadata.Shard),
   164  		logMetadata.EncodedTags,
   165  	}
   166  }
   167  
   168  func TestEncodeIndexInfo(t *testing.T) {
   169  	enc, actual := testCapturingEncoder(t)
   170  	require.NoError(t, enc.EncodeIndexInfo(testIndexInfo))
   171  	expected := testExpectedResultForIndexInfo(t, testIndexInfo)
   172  	require.Equal(t, expected, *actual)
   173  }
   174  
   175  func TestEncodeIndexEntry(t *testing.T) {
   176  	enc, actual := testCapturingEncoder(t)
   177  	require.NoError(t, enc.EncodeIndexEntry(testIndexEntry))
   178  	expected := testExpectedResultForIndexEntry(t, testIndexEntry)
   179  	require.Equal(t, expected, *actual)
   180  }
   181  
   182  func TestEncodeLogInfo(t *testing.T) {
   183  	enc, actual := testCapturingEncoder(t)
   184  	require.NoError(t, enc.EncodeLogInfo(testLogInfo))
   185  	expected := testExpectedResultForLogInfo(t, testLogInfo)
   186  	require.Equal(t, expected, *actual)
   187  }
   188  
   189  func TestEncodeLogEntry(t *testing.T) {
   190  	enc, actual := testCapturingEncoder(t)
   191  	require.NoError(t, enc.EncodeLogEntry(testLogEntry))
   192  	expected := testExpectedResultForLogEntry(t, testLogEntry)
   193  	require.Equal(t, expected, *actual)
   194  }
   195  
   196  func TestEncodeLogEntryFast(t *testing.T) {
   197  	buffer, err := EncodeLogEntryFast(nil, testLogEntry)
   198  	require.NoError(t, err)
   199  
   200  	enc := NewEncoder()
   201  	enc.EncodeLogEntry(testLogEntry)
   202  	expected := enc.Bytes()
   203  
   204  	require.Equal(t, expected, buffer)
   205  }
   206  
   207  func TestEncodeLogMetadata(t *testing.T) {
   208  	enc, actual := testCapturingEncoder(t)
   209  	require.NoError(t, enc.EncodeLogMetadata(testLogMetadata))
   210  	expected := testExpectedResultForLogMetadata(t, testLogMetadata)
   211  	require.Equal(t, expected, *actual)
   212  }
   213  
   214  func TestEncodeLogMetadataFast(t *testing.T) {
   215  	buffer, err := EncodeLogMetadataFast(nil, testLogMetadata)
   216  	require.NoError(t, err)
   217  
   218  	enc := NewEncoder()
   219  	enc.EncodeLogMetadata(testLogMetadata)
   220  	expected := enc.Bytes()
   221  
   222  	require.Equal(t, expected, buffer)
   223  }
   224  
   225  func TestEncodeVarintError(t *testing.T) {
   226  	enc, _ := testCapturingEncoder(t)
   227  	enc.encodeVarintFn = func(value int64) { enc.err = errTestVarint }
   228  	require.Equal(t, errTestVarint, enc.EncodeIndexInfo(testIndexInfo))
   229  }
   230  
   231  func TestEncodeFloat64Error(t *testing.T) {
   232  	enc, _ := testCapturingEncoder(t)
   233  	enc.encodeFloat64Fn = func(value float64) { enc.err = errTestFloat64 }
   234  	require.Equal(t, errTestFloat64, enc.EncodeLogEntry(testLogEntry))
   235  }
   236  
   237  func TestEncodeBytesError(t *testing.T) {
   238  	enc, _ := testCapturingEncoder(t)
   239  	enc.encodeBytesFn = func(value []byte) { enc.err = errTestBytes }
   240  	require.Equal(t, errTestBytes, enc.EncodeIndexEntry(testIndexEntry))
   241  }
   242  
   243  func TestEncodeArrayLenError(t *testing.T) {
   244  	enc, _ := testCapturingEncoder(t)
   245  	enc.encodeArrayLenFn = func(value int) { enc.err = errTestArrayLen }
   246  	require.Equal(t, errTestArrayLen, enc.EncodeLogInfo(testLogInfo))
   247  }