MCPcopy Index your code
hub / github.com/prometheus/prometheus / TestQueryTimeout

Function TestQueryTimeout

web/api/v1/api_test.go:4915–4968  ·  view source on GitHub ↗

Test query timeout parameter.

(t *testing.T)

Source from the content-addressed store, hash-verified

4913
4914// Test query timeout parameter.
4915func TestQueryTimeout(t *testing.T) {
4916 s := promqltest.LoadedStorage(t, `
4917 load 1m
4918 test_metric1{foo="bar"} 0+100x100
4919 `)
4920
4921 now := time.Now()
4922
4923 for _, tc := range []struct {
4924 name string
4925 method string
4926 }{
4927 {
4928 name: "GET method",
4929 method: http.MethodGet,
4930 },
4931 {
4932 name: "POST method",
4933 method: http.MethodPost,
4934 },
4935 } {
4936 t.Run(tc.name, func(t *testing.T) {
4937 engine := &fakeEngine{}
4938 api := &API{
4939 Queryable: s,
4940 QueryEngine: engine,
4941 ExemplarQueryable: s,
4942 alertmanagerRetriever: testAlertmanagerRetriever{}.toFactory(),
4943 flagsMap: sampleFlagMap,
4944 now: func() time.Time { return now },
4945 config: func() config.Config { return samplePrometheusCfg },
4946 ready: func(f http.HandlerFunc) http.HandlerFunc { return f },
4947 parser: testParser,
4948 }
4949
4950 query := url.Values{
4951 "query": []string{"2"},
4952 "timeout": []string{"1s"},
4953 }
4954 ctx := context.Background()
4955 req, err := http.NewRequest(tc.method, fmt.Sprintf("http://example.com?%s", query.Encode()), http.NoBody)
4956 require.NoError(t, err)
4957 req.RemoteAddr = "127.0.0.1:20201"
4958
4959 res := api.query(req.WithContext(ctx))
4960 assertAPIError(t, res.err, errorNone)
4961
4962 require.Len(t, engine.query.execCalls, 1)
4963 deadline, ok := engine.query.execCalls[0].Deadline()
4964 require.True(t, ok)
4965 require.Equal(t, now.Add(time.Second), deadline)
4966 })
4967 }
4968}
4969
4970// fakeEngine is a fake QueryEngine implementation.
4971type fakeEngine struct {

Callers

nothing calls this directly

Calls 9

queryMethod · 0.95
LoadedStorageFunction · 0.92
assertAPIErrorFunction · 0.85
DeadlineMethod · 0.80
RunMethod · 0.65
EncodeMethod · 0.65
LenMethod · 0.65
AddMethod · 0.65
toFactoryMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…