github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/query/test/seriesiter/mock_iter.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 seriesiter 22 23 import ( 24 "time" 25 26 "github.com/m3db/m3/src/dbnode/encoding" 27 m3ts "github.com/m3db/m3/src/dbnode/ts" 28 "github.com/m3db/m3/src/x/ident" 29 xtime "github.com/m3db/m3/src/x/time" 30 31 "github.com/golang/mock/gomock" 32 ) 33 34 // GenerateSingleSampleTagIterator generates a new tag iterator 35 func GenerateSingleSampleTagIterator( 36 ctrl *gomock.Controller, tag ident.Tag, 37 ) ident.TagIterator { 38 mockTagIterator := ident.NewMockTagIterator(ctrl) 39 mockTagIterator.EXPECT().Duplicate().Return(mockTagIterator).MaxTimes(1) 40 mockTagIterator.EXPECT().Remaining().Return(1).MaxTimes(1) 41 mockTagIterator.EXPECT().Next().Return(true).MaxTimes(1) 42 mockTagIterator.EXPECT().Current().Return(tag).MaxTimes(1) 43 mockTagIterator.EXPECT().Next().Return(false).MaxTimes(1) 44 mockTagIterator.EXPECT().Err().Return(nil).AnyTimes() 45 mockTagIterator.EXPECT().Rewind().Return().MaxTimes(1) 46 mockTagIterator.EXPECT().Close().AnyTimes() 47 48 return mockTagIterator 49 } 50 51 // GenerateTag generates a new tag 52 func GenerateTag() ident.Tag { 53 return ident.Tag{ 54 Name: ident.StringID("foo"), 55 Value: ident.StringID("bar"), 56 } 57 } 58 59 // NewMockSeriesIterSlice generates a slice of mock series iterators 60 func NewMockSeriesIterSlice( 61 ctrl *gomock.Controller, 62 tagGenerator func() ident.TagIterator, 63 len int, 64 numValues int, 65 ) []encoding.SeriesIterator { 66 iteratorList := make([]encoding.SeriesIterator, 0, len) 67 for i := 0; i < len; i++ { 68 mockIter := NewMockSeriesIterator(ctrl, tagGenerator, numValues) 69 iteratorList = append(iteratorList, mockIter) 70 } 71 return iteratorList 72 } 73 74 // NewMockSeriesIterator constructs a MockSeriesIterator return numValues 75 // datapoints, using tagGenerator, and otherwise configured with sensible defaults. 76 func NewMockSeriesIterator( 77 ctrl *gomock.Controller, tagGenerator func() ident.TagIterator, numValues int, 78 ) *encoding.MockSeriesIterator { 79 it := encoding.NewMockSeriesIterator(ctrl) 80 return NewMockSeriesIteratorFromBase(it, tagGenerator, numValues) 81 } 82 83 // NewMockSeriesIteratorFromBase constructs a MockSeriesIterator return numValues 84 // datapoints, using tagGenerator, and otherwise configured with sensible 85 // defaults. Any expectations already set on mockIter will be respected. 86 func NewMockSeriesIteratorFromBase( 87 mockIter *encoding.MockSeriesIterator, 88 tagGenerator func() ident.TagIterator, 89 numValues int, 90 ) *encoding.MockSeriesIterator { 91 mockIter.EXPECT().Next().Return(true).MaxTimes(numValues) 92 mockIter.EXPECT().Next().Return(false).MaxTimes(1) 93 now := xtime.Now() 94 for i := 0; i < numValues; i++ { 95 mockIter.EXPECT().Current().Return( 96 m3ts.Datapoint{ 97 TimestampNanos: now.Add(time.Duration(i*10) * time.Second), 98 Value: float64(i), 99 }, xtime.Millisecond, nil, 100 ).MaxTimes(1) 101 } 102 103 tags := tagGenerator() 104 mockIter.EXPECT().Namespace().Return(ident.StringID("foo")).AnyTimes() 105 mockIter.EXPECT().ID().Return(ident.StringID("bar")).AnyTimes() 106 mockIter.EXPECT().Tags().Return(tags).AnyTimes() 107 mockIter.EXPECT().Start().Return(now.Add(-time.Hour)).AnyTimes() 108 mockIter.EXPECT().End().Return(now).AnyTimes() 109 mockIter.EXPECT().FirstAnnotation().Return(nil).AnyTimes() 110 mockIter.EXPECT().Close().Do(func() { 111 // Make sure to close the tags generated when closing the iter 112 tags.Close() 113 }).AnyTimes() 114 mockIter.EXPECT().Err().Return(nil).AnyTimes() 115 return mockIter 116 } 117 118 // NewMockSeriesIters generates a new mock series iters 119 func NewMockSeriesIters( 120 ctrl *gomock.Controller, 121 tags ident.Tag, 122 l int, 123 numValues int, 124 ) encoding.SeriesIterators { 125 tagGenerator := func() ident.TagIterator { 126 return GenerateSingleSampleTagIterator(ctrl, tags) 127 } 128 129 iteratorList := NewMockSeriesIterSlice(ctrl, tagGenerator, l, numValues) 130 mockIters := encoding.NewMockSeriesIterators(ctrl) 131 mockIters.EXPECT().Iters().Return(iteratorList).AnyTimes() 132 mockIters.EXPECT().Len().Return(l).AnyTimes() 133 mockIters.EXPECT().Close().Do(func() { 134 // Make sure to close the iterators 135 for _, iter := range iteratorList { 136 if iter != nil { 137 iter.Close() 138 } 139 } 140 }).AnyTimes() 141 142 return mockIters 143 } 144 145 // NewMockValidTagGenerator wraps around GenerateSimpleTagIterator to 146 // construct a default TagIterator function. 147 func NewMockValidTagGenerator(ctrl *gomock.Controller) func() ident.TagIterator { 148 return func() ident.TagIterator { 149 return GenerateSingleSampleTagIterator(ctrl, GenerateTag()) 150 } 151 }