| 1 |
// Package storetest provides a helper for testing throttled stores. |
| 2 |
package storetest // import "github.com/throttled/throttled/store/storetest" |
| 3 |
|
| 4 |
import ( |
| 5 |
"math/rand" |
| 6 |
"strconv" |
| 7 |
"sync/atomic" |
| 8 |
"testing" |
| 9 |
"time" |
| 10 |
|
| 11 |
"github.com/throttled/throttled" |
| 12 |
) |
| 13 |
|
| 14 |
// TestGCRAStore tests the behavior of a GCRAStore implementation for |
| 15 |
// compliance with the throttled API. It does not require support |
| 16 |
// for TTLs. |
| 17 |
func TestGCRAStore(t *testing.T, st throttled.GCRAStore) { |
| 18 |
// GetWithTime a missing key |
| 19 |
if have, _, err := st.GetWithTime("foo"); err != nil { |
| 20 |
t.Fatal(err) |
| 21 |
} else if have != -1 { |
| 22 |
t.Errorf("expected GetWithTime to return -1 for a missing key but got %d", have) |
| 23 |
} |
| 24 |
|
| 25 |
// SetIfNotExists on a new key |
| 26 |
want := int64(1) |
| 27 |
|
| 28 |
if set, err := st.SetIfNotExistsWithTTL("foo", want, 0); err != nil { |
| 29 |
t.Fatal(err) |
| 30 |
} else if !set { |
| 31 |
t.Errorf("expected SetIfNotExists on an empty key to succeed") |
| 32 |
} |
| 33 |
|
| 34 |
before := time.Now() |
| 35 |
|
| 36 |
if have, now, err := st.GetWithTime("foo"); err != nil { |
| 37 |
t.Fatal(err) |
| 38 |
} else if have != want { |
| 39 |
t.Errorf("expected GetWithTime to return %d but got %d", want, have) |
| 40 |
} else if now.UnixNano() <= 0 { |
| 41 |
t.Errorf("expected GetWithTime to return a time representable representable as a positive int64 of nanoseconds since the epoch") |
| 42 |
} else if now.Before(before) || now.After(time.Now()) { |
| 43 |
// Note that we make the assumption here that the store is running on |
| 44 |
// the same machine as this test and thus shares a clock. This can be a |
| 45 |
// little tricky in the case of Redis, which could be running |
| 46 |
// elsewhere. The test assumes that it's running either locally on on |
| 47 |
// Travis (where currently the Redis is available on localhost). If new |
| 48 |
// test environments are procured, this may need to be revisited. |
| 49 |
t.Errorf("expected GetWithTime to return a time between the time before the call and the time after the call") |
| 50 |
} |
| 51 |
|
| 52 |
// SetIfNotExists on an existing key |
| 53 |
if set, err := st.SetIfNotExistsWithTTL("foo", 123, 0); err != nil { |
| 54 |
t.Fatal(err) |
| 55 |
} else if set { |
| 56 |
t.Errorf("expected SetIfNotExists on an existing key to fail") |
| 57 |
} |
| 58 |
|
| 59 |
if have, _, err := st.GetWithTime("foo"); err != nil { |
| 60 |
t.Fatal(err) |
| 61 |
} else if have != want { |
| 62 |
t.Errorf("expected GetWithTime to return %d but got %d", want, have) |
| 63 |
} |
| 64 |
|
| 65 |
// SetIfNotExists on a different key |
| 66 |
if set, err := st.SetIfNotExistsWithTTL("bar", 456, 0); err != nil { |
| 67 |
t.Fatal(err) |
| 68 |
} else if !set { |
| 69 |
t.Errorf("expected SetIfNotExists on an empty key to succeed") |
| 70 |
} |
| 71 |
|
| 72 |
// Returns the false on a missing key |
| 73 |
if swapped, err := st.CompareAndSwapWithTTL("baz", 1, 2, 0); err != nil { |
| 74 |
t.Fatal(err) |
| 75 |
} else if swapped { |
| 76 |
t.Errorf("expected CompareAndSwap to fail on a missing key") |
| 77 |
} |
| 78 |
|
| 79 |
// Test a successful CAS |
| 80 |
want = int64(2) |
| 81 |
|
| 82 |
if swapped, err := st.CompareAndSwapWithTTL("foo", 1, want, 0); err != nil { |
| 83 |
t.Fatal(err) |
| 84 |
} else if !swapped { |
| 85 |
t.Errorf("expected CompareAndSwap to succeed") |
| 86 |
} |
| 87 |
|
| 88 |
if have, _, err := st.GetWithTime("foo"); err != nil { |
| 89 |
t.Fatal(err) |
| 90 |
} else if have != want { |
| 91 |
t.Errorf("expected GetWithTime to return %d but got %d", want, have) |
| 92 |
} |
| 93 |
|
| 94 |
// Test an unsuccessful CAS |
| 95 |
if swapped, err := st.CompareAndSwapWithTTL("foo", 1, 2, 0); err != nil { |
| 96 |
t.Fatal(err) |
| 97 |
} else if swapped { |
| 98 |
t.Errorf("expected CompareAndSwap to fail") |
| 99 |
} |
| 100 |
|
| 101 |
if have, _, err := st.GetWithTime("foo"); err != nil { |
| 102 |
t.Fatal(err) |
| 103 |
} else if have != want { |
| 104 |
t.Errorf("expected GetWithTime to return %d but got %d", want, have) |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
// TestGCRAStoreTTL tests the behavior of TTLs in a GCRAStore implementation. |
| 109 |
func TestGCRAStoreTTL(t *testing.T, st throttled.GCRAStore) { |
| 110 |
ttl := time.Second |
| 111 |
want := int64(1) |
| 112 |
key := "ttl" |
| 113 |
|
| 114 |
if _, err := st.SetIfNotExistsWithTTL(key, want, ttl); err != nil { |
| 115 |
t.Fatal(err) |
| 116 |
} |
| 117 |
|
| 118 |
if have, _, err := st.GetWithTime(key); err != nil { |
| 119 |
t.Fatal(err) |
| 120 |
} else if have != want { |
| 121 |
t.Errorf("expected GetWithTime to return %d, got %d", want, have) |
| 122 |
} |
| 123 |
|
| 124 |
// I can't think of a generic way to test expiration without a sleep |
| 125 |
time.Sleep(ttl + time.Millisecond) |
| 126 |
|
| 127 |
if have, _, err := st.GetWithTime(key); err != nil { |
| 128 |
t.Fatal(err) |
| 129 |
} else if have != -1 { |
| 130 |
t.Errorf("expected GetWithTime to fail on an expired key but got %d", have) |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
// BenchmarkGCRAStore runs parallel benchmarks against a GCRAStore implementation. |
| 135 |
// Aside from being useful for performance testing, this is useful for finding |
| 136 |
// race conditions with the Go race detector. |
| 137 |
func BenchmarkGCRAStore(b *testing.B, st throttled.GCRAStore) { |
| 138 |
seed := int64(42) |
| 139 |
var attempts, updates int64 |
| 140 |
|
| 141 |
b.RunParallel(func(pb *testing.PB) { |
| 142 |
// We need atomic behavior around the RNG or go detects a race in the test |
| 143 |
delta := int64(1) |
| 144 |
seedValue := atomic.AddInt64(&seed, delta) - delta |
| 145 |
gen := rand.New(rand.NewSource(seedValue)) |
| 146 |
|
| 147 |
for pb.Next() { |
| 148 |
key := strconv.FormatInt(gen.Int63n(50), 10) |
| 149 |
|
| 150 |
var v int64 |
| 151 |
var updated bool |
| 152 |
|
| 153 |
v, _, err := st.GetWithTime(key) |
| 154 |
if v == -1 { |
| 155 |
updated, err = st.SetIfNotExistsWithTTL(key, gen.Int63(), 0) |
| 156 |
if err != nil { |
| 157 |
b.Error(err) |
| 158 |
} |
| 159 |
} else if err != nil { |
| 160 |
b.Error(err) |
| 161 |
} else { |
| 162 |
updated, err = st.CompareAndSwapWithTTL(key, v, gen.Int63(), 0) |
| 163 |
if err != nil { |
| 164 |
b.Error(err) |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
atomic.AddInt64(&attempts, 1) |
| 169 |
if updated { |
| 170 |
atomic.AddInt64(&updates, 1) |
| 171 |
} |
| 172 |
} |
| 173 |
}) |
| 174 |
|
| 175 |
b.Logf("%d/%d update operations succeeed", updates, attempts) |
| 176 |
} |