github.com/m3db/m3@v1.5.0/src/query/executor/engine_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 executor 22 23 import ( 24 "context" 25 "fmt" 26 "testing" 27 "time" 28 29 "github.com/m3db/m3/src/dbnode/client" 30 "github.com/m3db/m3/src/query/block" 31 "github.com/m3db/m3/src/query/models" 32 "github.com/m3db/m3/src/query/parser/promql" 33 "github.com/m3db/m3/src/query/storage" 34 "github.com/m3db/m3/src/query/test/m3" 35 "github.com/m3db/m3/src/x/instrument" 36 xtest "github.com/m3db/m3/src/x/test" 37 xtime "github.com/m3db/m3/src/x/time" 38 39 "github.com/golang/mock/gomock" 40 "github.com/stretchr/testify/assert" 41 "github.com/stretchr/testify/require" 42 ) 43 44 func newEngine( 45 s storage.Storage, 46 lookbackDuration time.Duration, 47 instrumentOpts instrument.Options, 48 ) Engine { 49 engineOpts := NewEngineOptions(). 50 SetStore(s). 51 SetLookbackDuration(lookbackDuration). 52 SetInstrumentOptions(instrumentOpts) 53 54 return NewEngine(engineOpts) 55 } 56 57 func TestExecute(t *testing.T) { 58 ctrl := xtest.NewController(t) 59 store, session := m3.NewStorageAndSession(t, ctrl) 60 session.EXPECT().FetchTagged(gomock.Any(), gomock.Any(), gomock.Any(), 61 gomock.Any()).Return(nil, client.FetchResponseMetadata{Exhaustive: false}, fmt.Errorf("dummy")) 62 session.EXPECT().IteratorPools().Return(nil, nil) 63 64 // Results is closed by execute 65 engine := newEngine(store, time.Minute, instrument.NewOptions()) 66 _, err := engine.ExecuteProm(context.TODO(), 67 &storage.FetchQuery{}, &QueryOptions{}, storage.NewFetchOptions()) 68 assert.NotNil(t, err) 69 } 70 71 func TestExecuteExpr(t *testing.T) { 72 ctrl := xtest.NewController(t) 73 defer ctrl.Finish() 74 75 parser, err := promql.Parse("foo", time.Second, 76 models.NewTagOptions(), promql.NewParseOptions()) 77 require.NoError(t, err) 78 79 store := storage.NewMockStorage(ctrl) 80 store.EXPECT().FetchBlocks(gomock.Any(), gomock.Any(), gomock.Any()). 81 Return(block.Result{ 82 Blocks: []block.Block{block.NewMockBlock(ctrl)}, 83 }, nil) 84 engine := newEngine(store, defaultLookbackDuration, 85 instrument.NewOptions()) 86 _, err = engine.ExecuteExpr(context.TODO(), parser, 87 &QueryOptions{}, storage.NewFetchOptions(), models.RequestParams{ 88 Start: xtime.Now().Add(-2 * time.Second), 89 End: xtime.Now(), 90 Step: time.Second, 91 }) 92 93 require.NoError(t, err) 94 }