(t *testing.T)
| 568 | } |
| 569 | |
| 570 | func TestSearchAccessesCache(t *testing.T) { |
| 571 | tenant := "foo" |
| 572 | meta := &backend.BlockMeta{ |
| 573 | StartTime: time.Unix(15, 0), |
| 574 | EndTime: time.Unix(16, 0), |
| 575 | Size_: defaultTargetBytesPerRequest, |
| 576 | TotalRecords: 1, |
| 577 | BlockID: backend.MustParse("00000000-0000-0000-0000-000000000123"), |
| 578 | } |
| 579 | |
| 580 | rdr := &mockReader{ |
| 581 | metas: []*backend.BlockMeta{meta}, |
| 582 | } |
| 583 | |
| 584 | // setup mock cache |
| 585 | c := test.NewMockClient() |
| 586 | p := test.NewMockProvider() |
| 587 | err := p.AddCache(cache.RoleFrontendSearch, c) |
| 588 | require.NoError(t, err) |
| 589 | f := frontendWithSettings(t, nil, rdr, nil, p) |
| 590 | |
| 591 | // setup query |
| 592 | query := "{}" |
| 593 | hash := hashForSearchRequest(&tempopb.SearchRequest{Query: query, Limit: 3, SpansPerSpanSet: 2}) |
| 594 | start := uint32(10) |
| 595 | end := uint32(20) |
| 596 | cacheKey := searchJobCacheKey(tenant, hash, time.Unix(int64(start), 0), time.Unix(int64(end), 0), meta, 0, 1) |
| 597 | |
| 598 | // confirm cache key doesn't exist |
| 599 | _, bufs, _ := c.Fetch(context.Background(), []string{cacheKey}) |
| 600 | require.Equal(t, 0, len(bufs)) |
| 601 | |
| 602 | // execute query |
| 603 | path := fmt.Sprintf("/?start=%d&end=%d&q=%s&limit=3&spss=2", start, end, query) // encapsulates block above |
| 604 | req := httptest.NewRequest("GET", path, nil) |
| 605 | ctx := req.Context() |
| 606 | ctx = user.InjectOrgID(ctx, tenant) |
| 607 | req = req.WithContext(ctx) |
| 608 | |
| 609 | respWriter := httptest.NewRecorder() |
| 610 | f.SearchHandler.ServeHTTP(respWriter, req) |
| 611 | |
| 612 | resp := respWriter.Result() |
| 613 | require.Equal(t, 200, resp.StatusCode) |
| 614 | |
| 615 | actualResp := &tempopb.SearchResponse{} |
| 616 | bytesResp, err := io.ReadAll(resp.Body) |
| 617 | require.NoError(t, err) |
| 618 | err = jsonpb.Unmarshal(bytes.NewReader(bytesResp), actualResp) |
| 619 | require.NoError(t, err) |
| 620 | |
| 621 | // confirm cache key exists and matches the response above |
| 622 | _, bufs, _ = c.Fetch(context.Background(), []string{cacheKey}) |
| 623 | require.Equal(t, 1, len(bufs)) |
| 624 | |
| 625 | actualCache := &tempopb.SearchResponse{} |
| 626 | err = jsonpb.Unmarshal(bytes.NewReader(bufs[0]), actualCache) |
| 627 | require.NoError(t, err) |
nothing calls this directly
no test coverage detected