From e5baf32f22cde2a9fb2c83fbdadee93437ea05ac Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Wed, 9 Jun 2021 11:53:46 -0400 Subject: [PATCH] Merge pull request #10367 from hashicorp/dnephin/submatview-store-get-tests submatview: add test cases for store.Get with timeout and no index --- agent/rpcclient/health/health.go | 2 +- agent/submatview/store_test.go | 62 ++++++++++++++++++++++++++++++-- 2 files changed, 60 insertions(+), 4 deletions(-) diff --git a/agent/rpcclient/health/health.go b/agent/rpcclient/health/health.go index b8b023e774..4cd7b0f4d1 100644 --- a/agent/rpcclient/health/health.go +++ b/agent/rpcclient/health/health.go @@ -126,7 +126,7 @@ func (r serviceRequest) CacheInfo() cache.RequestInfo { } func (r serviceRequest) Type() string { - return "service-health" + return "agent.rpcclient.health.serviceRequest" } func (r serviceRequest) NewMaterializer() (*submatview.Materializer, error) { diff --git a/agent/submatview/store_test.go b/agent/submatview/store_test.go index 9363c772b3..93b04d1e80 100644 --- a/agent/submatview/store_test.go +++ b/agent/submatview/store_test.go @@ -138,6 +138,60 @@ func TestStore_Get(t *testing.T) { require.Equal(t, store.expiryHeap.Next().Entry, e.expiry) }) + + runStep(t, "with no index returns latest value", func(t *testing.T) { + req.index = 0 + result, err := store.Get(ctx, req) + require.NoError(t, err) + require.Equal(t, uint64(41), result.Index) + + r, ok := result.Value.(fakeResult) + require.True(t, ok) + require.Len(t, r.srvs, 2) + require.Equal(t, uint64(41), r.index) + + store.lock.Lock() + defer store.lock.Unlock() + require.Len(t, store.byKey, 1) + e := store.byKey[makeEntryKey(req.Type(), req.CacheInfo())] + require.Equal(t, 0, e.expiry.Index()) + require.Equal(t, 0, e.requests) + + require.Equal(t, store.expiryHeap.Next().Entry, e.expiry) + }) + + runStep(t, "blocks until timeout", func(t *testing.T) { + req.index = 50 + req.timeout = 25 * time.Millisecond + + chResult := make(chan resultOrError, 1) + go func() { + result, err := store.Get(ctx, req) + chResult <- resultOrError{Result: result, Err: err} + }() + + var getResult resultOrError + select { + case getResult = <-chResult: + t.Fatalf("expected Get to block until timeout") + case <-time.After(10 * time.Millisecond): + } + + select { + case getResult = <-chResult: + case <-time.After(100 * time.Millisecond): + t.Fatalf("expected Get to unblock after timeout") + } + + require.NoError(t, getResult.Err) + require.Equal(t, uint64(41), getResult.Result.Index) + + r, ok := getResult.Result.Value.(fakeResult) + require.True(t, ok) + require.Len(t, r.srvs, 2) + require.Equal(t, uint64(41), r.index) + }) + } type resultOrError struct { @@ -146,9 +200,10 @@ type resultOrError struct { } type fakeRequest struct { - index uint64 - key string - client *TestStreamingClient + index uint64 + timeout time.Duration + key string + client *TestStreamingClient } func (r *fakeRequest) CacheInfo() cache.RequestInfo { @@ -160,6 +215,7 @@ func (r *fakeRequest) CacheInfo() cache.RequestInfo { Key: key, Token: "abcd", Datacenter: "dc1", + Timeout: r.timeout, MinIndex: r.index, } }