github.com/m3db/m3@v1.5.0/src/query/test/handler.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 test 22 23 import ( 24 "bytes" 25 "io" 26 "net/http" 27 "testing" 28 "time" 29 30 "github.com/m3db/m3/src/query/generated/proto/prompb" 31 32 "github.com/golang/protobuf/proto" 33 "github.com/golang/snappy" 34 ) 35 36 // SlowHandler slows down a request by delay 37 type SlowHandler struct { 38 handler http.Handler 39 delay time.Duration 40 } 41 42 // NewSlowHandler creates a new slow handler 43 func NewSlowHandler(handler http.Handler, delay time.Duration) *SlowHandler { 44 return &SlowHandler{handler: handler, delay: delay} 45 } 46 47 // ServeHTTP implements http.handler 48 func (h *SlowHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { 49 time.Sleep(h.delay) 50 h.handler.ServeHTTP(w, r) 51 } 52 53 // GeneratePromReadRequest generates a sample prometheus remote read request 54 func GeneratePromReadRequest() *prompb.ReadRequest { 55 req := &prompb.ReadRequest{ 56 Queries: []*prompb.Query{{ 57 Matchers: []*prompb.LabelMatcher{ 58 {Type: prompb.LabelMatcher_EQ, Name: []byte("eq"), Value: []byte("a")}, 59 }, 60 StartTimestampMs: time.Now().Add(-1*time.Hour*24).UnixNano() / int64(time.Millisecond), 61 EndTimestampMs: time.Now().UnixNano() / int64(time.Millisecond), 62 }}, 63 } 64 return req 65 } 66 67 // GeneratePromReadBody generates a sample snappy encoded prometheus remote read request body 68 func GeneratePromReadBody(t *testing.T) io.Reader { 69 req := GeneratePromReadRequest() 70 data, err := proto.Marshal(req) 71 if err != nil { 72 t.Fatal("couldn't marshal prometheus request") 73 } 74 75 compressed := snappy.Encode(nil, data) 76 // Uncomment the line below to write the data into a file useful for integration testing 77 //ioutil.WriteFile("/tmp/dat1", compressed, 0644) 78 b := bytes.NewReader(compressed) 79 return b 80 }