| 1 |
package sturdyc_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
"time" |
| 6 |
|
| 7 |
"github.com/viccon/sturdyc" |
| 8 |
) |
| 9 |
|
| 10 |
func TestPanicsIfCapacityIsLessThanZero(t *testing.T) { |
| 11 |
t.Parallel() |
| 12 |
|
| 13 |
defer func() { |
| 14 |
err := recover() |
| 15 |
if err == nil { |
| 16 |
t.Error("expected a panic when capacity is less than zero") |
| 17 |
} |
| 18 |
}() |
| 19 |
sturdyc.New[string](-1, 1, time.Hour, 5) |
| 20 |
} |
| 21 |
|
| 22 |
func TestPanicsIfTheNumberOfShardsIsLessThanOne(t *testing.T) { |
| 23 |
t.Parallel() |
| 24 |
|
| 25 |
defer func() { |
| 26 |
err := recover() |
| 27 |
if err == nil { |
| 28 |
t.Error("expected a panic when trying to use zero shards") |
| 29 |
} |
| 30 |
}() |
| 31 |
sturdyc.New[string](100, 0, time.Hour, 5) |
| 32 |
} |
| 33 |
|
| 34 |
func TestPanicsIfTTLIsLessThanOne(t *testing.T) { |
| 35 |
t.Parallel() |
| 36 |
|
| 37 |
defer func() { |
| 38 |
err := recover() |
| 39 |
if err == nil { |
| 40 |
t.Error("expected a panic when trying to use zero as TTL") |
| 41 |
} |
| 42 |
}() |
| 43 |
sturdyc.New[string](100, 2, 0, 5) |
| 44 |
} |
| 45 |
|
| 46 |
func TestPanicsIfEvictionPercentageIsLessThanZero(t *testing.T) { |
| 47 |
t.Parallel() |
| 48 |
|
| 49 |
defer func() { |
| 50 |
err := recover() |
| 51 |
if err == nil { |
| 52 |
t.Error("expected a panic when trying to use -1 as eviction percentage") |
| 53 |
} |
| 54 |
}() |
| 55 |
sturdyc.New[string](100, 10, time.Minute, -1) |
| 56 |
} |
| 57 |
|
| 58 |
func TestPanicsIfEvictionPercentageIsGreaterThanOneHundred(t *testing.T) { |
| 59 |
t.Parallel() |
| 60 |
|
| 61 |
defer func() { |
| 62 |
err := recover() |
| 63 |
if err == nil { |
| 64 |
t.Error("expected a panic when trying to use 101 as eviction percentage") |
| 65 |
} |
| 66 |
}() |
| 67 |
sturdyc.New[string](100, 10, time.Minute, 101) |
| 68 |
} |
| 69 |
|
| 70 |
func TestPanicsIfRefreshBufferingIsEnabledWithoutStampedeProtection(t *testing.T) { |
| 71 |
t.Parallel() |
| 72 |
|
| 73 |
defer func() { |
| 74 |
err := recover() |
| 75 |
if err == nil { |
| 76 |
t.Error("expected a panic when trying to use buffered refreshes without stampede protection") |
| 77 |
} |
| 78 |
}() |
| 79 |
sturdyc.New[string](100, 10, time.Minute, 5, |
| 80 |
sturdyc.WithRefreshCoalescing(10, time.Minute), |
| 81 |
) |
| 82 |
} |
| 83 |
|
| 84 |
func TestPanicsIfTheRefreshBufferSizeIsLessThanOne(t *testing.T) { |
| 85 |
t.Parallel() |
| 86 |
|
| 87 |
defer func() { |
| 88 |
err := recover() |
| 89 |
if err == nil { |
| 90 |
t.Error("expected a panic when trying to use buffered refreshes with zero as size") |
| 91 |
} |
| 92 |
}() |
| 93 |
sturdyc.New[string](100, 10, time.Minute, 5, |
| 94 |
sturdyc.WithEarlyRefreshes(time.Minute, time.Hour, time.Hour*2, time.Second), |
| 95 |
sturdyc.WithRefreshCoalescing(0, time.Minute), |
| 96 |
) |
| 97 |
} |
| 98 |
|
| 99 |
func TestPanicsIfTheRefreshBufferTimeoutIsLessThanOne(t *testing.T) { |
| 100 |
t.Parallel() |
| 101 |
|
| 102 |
defer func() { |
| 103 |
err := recover() |
| 104 |
if err == nil { |
| 105 |
t.Error("expected a panic when trying to use 0 as buffer timeout") |
| 106 |
} |
| 107 |
}() |
| 108 |
sturdyc.New[string](100, 10, time.Minute, 5, |
| 109 |
sturdyc.WithEarlyRefreshes(time.Minute, time.Hour, time.Hour*2, time.Second), |
| 110 |
sturdyc.WithRefreshCoalescing(10, 0), |
| 111 |
) |
| 112 |
} |
| 113 |
|
| 114 |
func TestPanicsIfTheEvictionIntervalIsSetToLessThanOne(t *testing.T) { |
| 115 |
t.Parallel() |
| 116 |
|
| 117 |
defer func() { |
| 118 |
err := recover() |
| 119 |
if err == nil { |
| 120 |
t.Error("expected a panic when trying to use 0 as eviction interval") |
| 121 |
} |
| 122 |
}() |
| 123 |
sturdyc.New[string](100, 10, time.Minute, 5, |
| 124 |
sturdyc.WithEvictionInterval(0), |
| 125 |
) |
| 126 |
} |
| 127 |
|
| 128 |
func TestPanicsIfTheMinRefreshTimeIsGreaterThanTheMaxRefreshTime(t *testing.T) { |
| 129 |
t.Parallel() |
| 130 |
|
| 131 |
defer func() { |
| 132 |
err := recover() |
| 133 |
if err == nil { |
| 134 |
t.Error("expected a panic when trying to use hour as min refresh time and minute as max") |
| 135 |
} |
| 136 |
}() |
| 137 |
sturdyc.New[string](100, 10, time.Minute, 5, |
| 138 |
sturdyc.WithEarlyRefreshes(time.Hour, time.Minute, time.Hour*2, time.Second), |
| 139 |
) |
| 140 |
} |
| 141 |
|
| 142 |
func TestPanicsIfTheBackgroundRefreshTimeIsGreaterThanTheSynchronousRefreshTime(t *testing.T) { |
| 143 |
t.Parallel() |
| 144 |
|
| 145 |
defer func() { |
| 146 |
err := recover() |
| 147 |
if err == nil { |
| 148 |
t.Error("expected a panic when trying to use a greater background refresh time than the synchronous refresh time") |
| 149 |
} |
| 150 |
}() |
| 151 |
sturdyc.New[string](100, 10, time.Minute, 5, |
| 152 |
sturdyc.WithEarlyRefreshes(time.Minute, time.Hour*2, time.Hour*1, time.Second), |
| 153 |
) |
| 154 |
} |
| 155 |
|
| 156 |
func TestPanicsIfTheRetryBaseDelayIsLessThanZero(t *testing.T) { |
| 157 |
t.Parallel() |
| 158 |
|
| 159 |
defer func() { |
| 160 |
err := recover() |
| 161 |
if err == nil { |
| 162 |
t.Error("expected a panic when trying to use -1 as retry base delay") |
| 163 |
} |
| 164 |
}() |
| 165 |
sturdyc.New[string](100, 10, time.Minute, 5, |
| 166 |
sturdyc.WithEarlyRefreshes(time.Minute, time.Hour, time.Hour*2, -1), |
| 167 |
) |
| 168 |
} |