|
| 1 | +package loader |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "sync/atomic" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | +) |
| 11 | + |
| 12 | +func TestConcurrencySingleKey(t *testing.T) { |
| 13 | + var counter int32 |
| 14 | + fetch := func(key interface{}) (interface{}, error) { |
| 15 | + atomic.AddInt32(&counter, 1) |
| 16 | + time.Sleep(100 * time.Millisecond) |
| 17 | + return key, nil |
| 18 | + } |
| 19 | + l := New(fetch, 500*time.Millisecond, InMemoryCache()) |
| 20 | + type result struct { |
| 21 | + dur time.Duration |
| 22 | + val interface{} |
| 23 | + } |
| 24 | + c := make(chan *result, 3) |
| 25 | + |
| 26 | + var start time.Time |
| 27 | + var dur time.Duration |
| 28 | + |
| 29 | + start = time.Now() |
| 30 | + for i := 0; i < 3; i++ { |
| 31 | + go func() { |
| 32 | + start := time.Now() |
| 33 | + val, _ := l.Get("x") |
| 34 | + c <- &result{val: val, dur: time.Now().Sub(start)} |
| 35 | + }() |
| 36 | + time.Sleep(10 * time.Millisecond) |
| 37 | + } |
| 38 | + for i := 0; i < 3; i++ { |
| 39 | + res := <-c |
| 40 | + assert.InDelta(t, 100, res.dur.Milliseconds(), 25, "each get should within 1s") |
| 41 | + assert.Equal(t, "x", res.val, "Value must be x") |
| 42 | + } |
| 43 | + dur = time.Now().Sub(start) |
| 44 | + assert.InDelta(t, 100, dur.Milliseconds(), 25, "all get should within 1s") |
| 45 | + |
| 46 | + start = time.Now() |
| 47 | + val, _ := l.Get("x") |
| 48 | + dur = time.Now().Sub(start) |
| 49 | + assert.Less(t, dur.Milliseconds(), int64(50), "After cached get must be fast") |
| 50 | + assert.Equal(t, "x", val, "Value must still be x") |
| 51 | + |
| 52 | + assert.Equal(t, int32(1), counter, "fetch must be called once") |
| 53 | +} |
| 54 | + |
| 55 | +func TestConcurrencyMultiKey(t *testing.T) { |
| 56 | + var counter int32 |
| 57 | + fetch := func(key interface{}) (interface{}, error) { |
| 58 | + atomic.AddInt32(&counter, 1) |
| 59 | + time.Sleep(100 * time.Millisecond) |
| 60 | + return key, nil |
| 61 | + } |
| 62 | + l := New(fetch, 500*time.Millisecond, InMemoryCache()) |
| 63 | + type result struct { |
| 64 | + dur time.Duration |
| 65 | + val interface{} |
| 66 | + } |
| 67 | + c := make(chan *result, 3) |
| 68 | + |
| 69 | + var start time.Time |
| 70 | + var dur time.Duration |
| 71 | + |
| 72 | + start = time.Now() |
| 73 | + for i := 0; i < 3; i++ { |
| 74 | + go func(i int) { |
| 75 | + start := time.Now() |
| 76 | + val, _ := l.Get(fmt.Sprint(i)) |
| 77 | + c <- &result{val: val, dur: time.Now().Sub(start)} |
| 78 | + }(i) |
| 79 | + time.Sleep(10 * time.Millisecond) |
| 80 | + } |
| 81 | + for i := 0; i < 3; i++ { |
| 82 | + res := <-c |
| 83 | + assert.InDelta(t, 100, res.dur.Milliseconds(), 25, "each get should within 1s") |
| 84 | + assert.Equal(t, fmt.Sprint(i), res.val, "Value must be valid") |
| 85 | + } |
| 86 | + dur = time.Now().Sub(start) |
| 87 | + assert.InDelta(t, 100, dur.Milliseconds(), 25, "all get should within 1s") |
| 88 | + |
| 89 | + start = time.Now() |
| 90 | + val, _ := l.Get("1") |
| 91 | + dur = time.Now().Sub(start) |
| 92 | + assert.Less(t, dur.Milliseconds(), int64(50), "After cached get must be fast") |
| 93 | + assert.Equal(t, "1", val, "Value must still the same") |
| 94 | + |
| 95 | + assert.Equal(t, int32(3), counter, "fetch must be called once") |
| 96 | +} |
| 97 | + |
| 98 | +func TestExpire(t *testing.T) { |
| 99 | + var counter int32 |
| 100 | + fetch := func(key interface{}) (interface{}, error) { |
| 101 | + atomic.AddInt32(&counter, 1) |
| 102 | + time.Sleep(10 * time.Millisecond) |
| 103 | + return fmt.Sprintf("%d %s", counter, key), nil |
| 104 | + } |
| 105 | + l := New(fetch, 500*time.Millisecond, InMemoryCache()) |
| 106 | + val, _ := l.Get("x") |
| 107 | + assert.Equal(t, "1 x", val, "First call") |
| 108 | + assert.Equal(t, int32(1), counter, "fetch called once") |
| 109 | + |
| 110 | + time.Sleep(550 * time.Millisecond) |
| 111 | + val, _ = l.Get("x") |
| 112 | + assert.Equal(t, "1 x", val, "Use stale value") |
| 113 | + val, _ = l.Get("x") |
| 114 | + assert.Equal(t, "1 x", val, "Still use stale value") |
| 115 | + |
| 116 | + time.Sleep(100 * time.Millisecond) |
| 117 | + val, _ = l.Get("x") |
| 118 | + assert.Equal(t, "2 x", val, "Use updated value") |
| 119 | + assert.Equal(t, int32(2), counter, "fetch called twice") |
| 120 | +} |
0 commit comments