Skip to content

Commit

Permalink
sql: fix flake in TestQueryCache/group/statschange
Browse files Browse the repository at this point in the history
This commit fixes a flake in TestQueryCache/group/statschange,
which was introduced by #51616. That PR made updates to the stats
cache asynchronous, so we can no longer expect the query cache to
be invalidated immediately after a stats update. This commit fixes
the problem by introducing a retry mechanism into the test.

Fixes #51693

Release note: None
  • Loading branch information
rytaft committed Jul 31, 2020
1 parent 96e9255 commit d8ad6a8
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions pkg/sql/plan_opt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,20 @@ func (h *queryCacheTestHelper) AssertStats(tb *testing.T, expHits, expMisses int
assert.Equal(tb, expMisses, misses, "misses")
}

// CheckStats is similar to AssertStats, but returns an error instead of
// failing the test if the actual stats don't match the expected stats.
func (h *queryCacheTestHelper) CheckStats(tb *testing.T, expHits, expMisses int) error {
tb.Helper()
hits, misses := h.GetStats()
if expHits != hits {
return errors.Errorf("expected %d hits but found %d", expHits, hits)
}
if expMisses != misses {
return errors.Errorf("expected %d misses but found %d", expMisses, misses)
}
return nil
}

func TestQueryCache(t *testing.T) {
defer leaktest.AfterTest(t)()

Expand Down Expand Up @@ -328,8 +342,17 @@ SELECT cte.x, cte.y FROM cte LEFT JOIN cte as cte2 on cte.y = cte2.x`, j)
h.AssertStats(t, 1 /* hits */, 1 /* misses */)
r0.Exec(t, "CREATE STATISTICS s FROM t")
h.AssertStats(t, 1 /* hits */, 1 /* misses */)
r1.CheckQueryResults(t, "SELECT * FROM t", [][]string{{"1", "1"}})
h.AssertStats(t, 1 /* hits */, 2 /* misses */)
hits := 1
testutils.SucceedsSoon(t, func() error {
// The stats cache is updated asynchronously, so we may get some hits
// before we get a miss.
r1.CheckQueryResults(t, "SELECT * FROM t", [][]string{{"1", "1"}})
if err := h.CheckStats(t, hits, 2 /* misses */); err != nil {
hits++
return err
}
return nil
})
})

// Test that a schema change triggers cache invalidation.
Expand Down

0 comments on commit d8ad6a8

Please sign in to comment.