github.com/m3db/m3@v1.5.0/src/dbnode/storage/block/result_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 block 22 23 import ( 24 "errors" 25 "sort" 26 "testing" 27 "time" 28 29 "github.com/m3db/m3/src/x/ident" 30 xtime "github.com/m3db/m3/src/x/time" 31 32 "github.com/stretchr/testify/assert" 33 "github.com/stretchr/testify/require" 34 ) 35 36 func TestSortFetchBlockResultByTimeAscending(t *testing.T) { 37 now := xtime.Now() 38 input := []FetchBlockResult{ 39 NewFetchBlockResult(now, nil, nil), 40 NewFetchBlockResult(now.Add(time.Second), nil, nil), 41 NewFetchBlockResult(now.Add(-time.Second), nil, nil), 42 } 43 expected := []FetchBlockResult{input[2], input[0], input[1]} 44 sort.Sort(fetchBlockResultByTimeAscending(input)) 45 require.Equal(t, expected, input) 46 } 47 48 func TestSortFetchBlockMetadataResultByTimeAscending(t *testing.T) { 49 now := xtime.Now() 50 inputs := []FetchBlockMetadataResult{ 51 NewFetchBlockMetadataResult(now, 0, nil, 0, nil), 52 NewFetchBlockMetadataResult(now.Add(time.Second), 0, nil, 0, nil), 53 NewFetchBlockMetadataResult(now.Add(-time.Second), 0, nil, 0, nil), 54 } 55 expected := []FetchBlockMetadataResult{inputs[2], inputs[0], inputs[1]} 56 res := newPooledFetchBlockMetadataResults(nil, nil) 57 for _, input := range inputs { 58 res.Add(input) 59 } 60 res.Sort() 61 require.Equal(t, expected, res.Results()) 62 } 63 64 func TestFilteredBlocksMetadataIter(t *testing.T) { 65 now := xtime.Now() 66 sizes := []int64{1, 2, 3} 67 checksums := []uint32{6, 7, 8} 68 lastRead := now.Add(-100 * time.Millisecond) 69 inputs := []FetchBlocksMetadataResult{ 70 NewFetchBlocksMetadataResult(ident.StringID("foo"), 71 ident.EmptyTagIterator, newPooledFetchBlockMetadataResults( 72 []FetchBlockMetadataResult{ 73 NewFetchBlockMetadataResult(now.Add(-time.Second), sizes[0], &checksums[0], lastRead, nil), 74 }, nil)), 75 NewFetchBlocksMetadataResult(ident.StringID("bar"), 76 ident.EmptyTagIterator, newPooledFetchBlockMetadataResults( 77 []FetchBlockMetadataResult{ 78 NewFetchBlockMetadataResult(now, sizes[1], &checksums[1], lastRead, nil), 79 NewFetchBlockMetadataResult(now.Add(time.Second), sizes[2], &checksums[2], lastRead, errors.New("foo")), 80 NewFetchBlockMetadataResult(now.Add(2*time.Second), 0, nil, lastRead, nil), 81 }, nil)), 82 } 83 84 res := newPooledFetchBlocksMetadataResults(nil, nil) 85 for _, input := range inputs { 86 res.Add(input) 87 } 88 89 iter := NewFilteredBlocksMetadataIter(res) 90 91 var actual []Metadata 92 for iter.Next() { 93 _, metadata := iter.Current() 94 actual = append(actual, metadata) 95 } 96 require.NoError(t, iter.Err()) 97 98 expected := []Metadata{ 99 NewMetadata(ident.StringID("foo"), ident.Tags{}, now.Add(-time.Second), 100 sizes[0], &checksums[0], lastRead), 101 NewMetadata(ident.StringID("bar"), ident.Tags{}, now, 102 sizes[1], &checksums[1], lastRead), 103 NewMetadata(ident.StringID("bar"), ident.Tags{}, now.Add(2*time.Second), 104 int64(0), nil, lastRead), 105 } 106 107 require.Equal(t, len(expected), len(actual)) 108 for i := range expected { 109 assert.True(t, expected[i].ID.Equal(actual[i].ID)) 110 assert.True(t, expected[i].Start.Equal(actual[i].Start)) 111 assert.Equal(t, expected[i].Size, actual[i].Size) 112 assert.Equal(t, expected[i].Checksum, actual[i].Checksum) 113 assert.Equal(t, expected[i].LastRead, actual[i].LastRead) 114 } 115 116 for _, fetchMetadataResult := range res.Results() { 117 // Ensure that the consumed (and closed) tags are marked as nil so subsequent code paths 118 // can't trigger a double close. 119 require.Nil(t, fetchMetadataResult.Tags) 120 } 121 }