github.com/prebid/prebid-server/v2@v2.18.0/stored_requests/caches/cachestest/reliable.go (about)

     1  package cachestest
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"testing"
     7  
     8  	"github.com/prebid/prebid-server/v2/stored_requests"
     9  )
    10  
    11  const (
    12  	reqCacheKey = "known-req"
    13  	reqCacheVal = `{"req":true}`
    14  )
    15  
    16  // AssertCacheRobustness runs tests which can be used to validate any Cache that is 100% reliable.
    17  // That is, its Save() and Invalidate() functions _alway_ work.
    18  //
    19  // The cacheSupplier should be a function which returns a new Cache (with no data inside) on every call.
    20  // This will be called from separate Goroutines to make sure that different tests don't conflict.
    21  func AssertCacheRobustness(t *testing.T, cacheSupplier func() stored_requests.CacheJSON) {
    22  	t.Run("TestCacheMiss", cacheMissTester(cacheSupplier()))
    23  	t.Run("TestCacheHit", cacheHitTester(cacheSupplier()))
    24  	t.Run("TestCacheSaveInvalidate", cacheSaveInvalidateTester(cacheSupplier()))
    25  }
    26  
    27  func cacheMissTester(cache stored_requests.CacheJSON) func(*testing.T) {
    28  	return func(t *testing.T) {
    29  		storedData := cache.Get(context.Background(), []string{"unknown"})
    30  		assertMapLength(t, 0, storedData)
    31  	}
    32  }
    33  
    34  func cacheHitTester(cache stored_requests.CacheJSON) func(*testing.T) {
    35  	return func(t *testing.T) {
    36  		cache.Save(context.Background(), map[string]json.RawMessage{
    37  			reqCacheKey: json.RawMessage(reqCacheVal),
    38  		})
    39  		reqData := cache.Get(context.Background(), []string{reqCacheKey})
    40  		assertMapLength(t, 1, reqData)
    41  		assertHasValue(t, reqData, reqCacheKey, reqCacheVal)
    42  	}
    43  }
    44  
    45  func cacheSaveInvalidateTester(cache stored_requests.CacheJSON) func(*testing.T) {
    46  	return func(t *testing.T) {
    47  		cache.Save(context.Background(), map[string]json.RawMessage{
    48  			reqCacheKey: json.RawMessage(reqCacheVal),
    49  		})
    50  		reqData := cache.Get(context.Background(), []string{reqCacheKey})
    51  		assertMapLength(t, 1, reqData)
    52  
    53  		cache.Invalidate(context.Background(), []string{reqCacheKey})
    54  		reqData = cache.Get(context.Background(), []string{reqCacheKey})
    55  		assertMapLength(t, 0, reqData)
    56  	}
    57  }
    58  
    59  func assertMapLength(t *testing.T, expectedLen int, theMap map[string]json.RawMessage) {
    60  	t.Helper()
    61  	if len(theMap) != expectedLen {
    62  		t.Errorf("Wrong map length. Expected %d, Got %d.", expectedLen, len(theMap))
    63  	}
    64  }
    65  
    66  func assertHasValue(t *testing.T, m map[string]json.RawMessage, key string, val string) {
    67  	t.Helper()
    68  	realVal, ok := m[key]
    69  	if !ok {
    70  		t.Errorf("Map missing required key: %s", key)
    71  	}
    72  	if val != string(realVal) {
    73  		t.Errorf("Unexpected value at key %s. Expected %s, Got %s", key, val, string(realVal))
    74  	}
    75  }