Skip to content

Commit

Permalink
tapdb: add TestUniverseStatsAsyncCache test
Browse files Browse the repository at this point in the history
With this commit we add a simple test for the asynchronous cache
population that was introduced previously. We make a single RPC call to
the aggregate stats endpoint with a client timeout that is too small.
This leads to an RPC failure, so we check whether the cache was updated
in the background.
  • Loading branch information
GeorgeTsagk committed Jan 21, 2025
1 parent d48b5c0 commit c7f16f6
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions tapdb/universe_stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,61 @@ func TestUniverseStatsEvents(t *testing.T) {
})
}

// TestUniverseStatsAsyncCache tests that the cache of the universe aggregate
// stats is asynchronously populated regardless of what the outcome of the
// RPC call is.
func TestUniverseStatsAsyncCache(t *testing.T) {
t.Parallel()

db := NewTestDB(t)

yesterday := time.Now().UTC().Add(-24 * time.Hour)
testClock := clock.NewTestClock(yesterday)
statsDB, _ := newUniverseStatsWithDB(db.BaseDB, testClock)

const numTranches = 3

sh := newUniStatsHarness(t, numTranches, db.BaseDB, statsDB)

// Record the number of groups in this asset.
var numGroups uint64
for i := 0; i < numTranches; i++ {
if sh.universeLeaves[i].Leaf.GroupKey != nil {
numGroups++
}
}

// First let's make sure the cache is empty. This should be the case as
// no calls have been made so far.
val := sh.db.statsSnapshot.Load()
require.Nil(t, val)

const (
quickTimeoutDuration = time.Microsecond * 1
defaultTick = time.Millisecond * 250
)

// We now create a client context with a very quick timeout. This is
// meant to quickly fail the RPC call.
ctx, cancel := context.WithTimeout(
context.Background(), quickTimeoutDuration,
)
defer cancel()

// The tiny timeout duration should make the following call result in a
// context deadline related error.
_, err := sh.db.AggregateSyncStats(ctx)
require.Error(t, err)
require.ErrorContains(t, err, "context deadline exceeded")

// Regardless of the above call failing, the cache should asynchronously
// get updated in the background, so let's wait until a value is loaded.
require.Eventually(t, func() bool {
val := sh.db.statsSnapshot.Load()
return val != nil
}, DefaultStoreTimeout, defaultTick)
}

// TestUniverseQuerySyncStatsSorting tests that we're able to properly sort the
// response using any of the available params.
func TestUniverseQuerySyncStatsSorting(t *testing.T) {
Expand Down

0 comments on commit c7f16f6

Please sign in to comment.