github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/query/functions/fetch_test.go (about) 1 // Copyright (c) 2018 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 functions 22 23 import ( 24 "context" 25 "testing" 26 "time" 27 28 "github.com/m3db/m3/src/metrics/policy" 29 "github.com/m3db/m3/src/query/block" 30 "github.com/m3db/m3/src/query/executor/transform" 31 "github.com/m3db/m3/src/query/models" 32 "github.com/m3db/m3/src/query/parser" 33 "github.com/m3db/m3/src/query/storage" 34 "github.com/m3db/m3/src/query/storage/m3/storagemetadata" 35 "github.com/m3db/m3/src/query/storage/mock" 36 "github.com/m3db/m3/src/query/test" 37 "github.com/m3db/m3/src/query/test/executor" 38 "github.com/m3db/m3/src/query/test/transformtest" 39 xtime "github.com/m3db/m3/src/x/time" 40 41 "github.com/golang/mock/gomock" 42 "github.com/stretchr/testify/assert" 43 "github.com/stretchr/testify/require" 44 "github.com/uber-go/tally" 45 ) 46 47 func TestFetch(t *testing.T) { 48 values, bounds := test.GenerateValuesAndBounds(nil, nil) 49 b := test.NewBlockFromValues(bounds, values) 50 c, sink := executor.NewControllerWithSink(parser.NodeID(rune(1))) 51 mockStorage := mock.NewMockStorage() 52 mockStorage.SetFetchBlocksResult(block.Result{Blocks: []block.Block{b}}, nil) 53 source := (&FetchOp{}).Node(c, mockStorage, 54 transformtest.Options(t, transform.OptionsParams{})) 55 err := source.Execute(models.NoopQueryContext()) 56 require.NoError(t, err) 57 expected := values 58 assert.Len(t, sink.Values, 2) 59 assert.Equal(t, expected, sink.Values) 60 } 61 62 type predicateMatcher struct { 63 name string 64 fn func(interface{}) bool 65 } 66 67 var _ gomock.Matcher = &predicateMatcher{} 68 69 func (m *predicateMatcher) Matches(i interface{}) bool { 70 return m.fn(i) 71 } 72 73 func (m *predicateMatcher) String() string { 74 return m.name 75 } 76 77 func TestOffsetFetch(t *testing.T) { 78 ctrl := gomock.NewController(t) 79 defer ctrl.Finish() 80 81 store := storage.NewMockStorage(ctrl) 82 op := &FetchOp{ 83 Offset: time.Minute, 84 } 85 86 now := time.Now() 87 start := now.Add(time.Hour * -1) 88 opts := transformtest.Options(t, transform.OptionsParams{ 89 TimeSpec: transform.TimeSpec{ 90 Start: xtime.ToUnixNano(start), 91 End: xtime.ToUnixNano(now), 92 Now: now, 93 }, 94 }) 95 96 qMatcher := &predicateMatcher{ 97 name: "query", 98 fn: func(i interface{}) bool { 99 q, ok := i.(*storage.FetchQuery) 100 if !ok { 101 return false 102 } 103 104 return q.Start.Equal(start.Add(time.Minute*-1)) && 105 q.End.Equal(now.Add(time.Minute*-1)) 106 }, 107 } 108 109 optsMatcher := &predicateMatcher{ 110 name: "opts", 111 fn: func(i interface{}) bool { 112 _, ok := i.(*storage.FetchOptions) 113 return ok 114 }, 115 } 116 117 store.EXPECT().FetchBlocks(gomock.Any(), qMatcher, optsMatcher) 118 119 c, _ := executor.NewControllerWithSink(parser.NodeID(rune(1))) 120 node := op.Node(c, store, opts) 121 122 err := node.Execute(models.NoopQueryContext()) 123 require.NoError(t, err) 124 } 125 126 func TestFetchWithRestrictFetch(t *testing.T) { 127 values, bounds := test.GenerateValuesAndBounds(nil, nil) 128 b := test.NewBlockFromValues(bounds, values) 129 c, sink := executor.NewControllerWithSink(parser.NodeID(rune(1))) 130 mockStorage := mock.NewMockStorage() 131 mockStorage.SetFetchBlocksResult(block.Result{Blocks: []block.Block{b}}, nil) 132 source := (&FetchOp{}).Node(c, mockStorage, 133 transformtest.Options(t, transform.OptionsParams{})) 134 135 ctx := models.NewQueryContext(context.Background(), 136 tally.NoopScope, 137 models.QueryContextOptions{ 138 RestrictFetchType: &models.RestrictFetchTypeQueryContextOptions{ 139 MetricsType: uint(storagemetadata.AggregatedMetricsType), 140 StoragePolicy: policy.MustParseStoragePolicy("10s:42d"), 141 }, 142 }) 143 err := source.Execute(ctx) 144 require.NoError(t, err) 145 expected := values 146 assert.Len(t, sink.Values, 2) 147 assert.Equal(t, expected, sink.Values) 148 149 fetchOpts := mockStorage.LastFetchOptions() 150 restrictByType := fetchOpts.RestrictQueryOptions.GetRestrictByType() 151 require.NotNil(t, restrictByType) 152 assert.Equal(t, storagemetadata.AggregatedMetricsType, 153 storagemetadata.MetricsType(restrictByType.MetricsType)) 154 assert.Equal(t, "10s:42d", restrictByType.StoragePolicy.String()) 155 }