| 1 |
/* |
| 2 |
Copyright 2017 Mailgun Technologies Inc |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
package collections |
| 17 |
|
| 18 |
import ( |
| 19 |
"testing" |
| 20 |
|
| 21 |
"github.com/stretchr/testify/suite" |
| 22 |
"github.com/vulcand/oxy/v2/internal/holsterv4/clock" |
| 23 |
) |
| 24 |
|
| 25 |
type TTLMapSuite struct { |
| 26 |
suite.Suite |
| 27 |
} |
| 28 |
|
| 29 |
func TestTTLMapSuite(t *testing.T) { |
| 30 |
suite.Run(t, new(TTLMapSuite)) |
| 31 |
} |
| 32 |
|
| 33 |
func (s *TTLMapSuite) SetupTest() { |
| 34 |
clock.Freeze(clock.Date(2012, 3, 4, 5, 6, 7, 0, clock.UTC)) |
| 35 |
} |
| 36 |
|
| 37 |
func (s *TTLMapSuite) TearDownSuite() { |
| 38 |
clock.Unfreeze() |
| 39 |
} |
| 40 |
|
| 41 |
func (s *TTLMapSuite) TestSetWrong() { |
| 42 |
m := NewTTLMap(1) |
| 43 |
|
| 44 |
err := m.Set("a", 1, -1) |
| 45 |
s.Require().EqualError(err, "ttlSeconds should be >= 0, got -1") |
| 46 |
|
| 47 |
err = m.Set("a", 1, 0) |
| 48 |
s.Require().EqualError(err, "ttlSeconds should be >= 0, got 0") |
| 49 |
|
| 50 |
_, err = m.Increment("a", 1, 0) |
| 51 |
s.Require().EqualError(err, "ttlSeconds should be >= 0, got 0") |
| 52 |
|
| 53 |
_, err = m.Increment("a", 1, -1) |
| 54 |
s.Require().EqualError(err, "ttlSeconds should be >= 0, got -1") |
| 55 |
} |
| 56 |
|
| 57 |
func (s *TTLMapSuite) TestRemoveExpiredEmpty() { |
| 58 |
m := NewTTLMap(1) |
| 59 |
m.RemoveExpired(100) |
| 60 |
} |
| 61 |
|
| 62 |
func (s *TTLMapSuite) TestRemoveLastUsedEmpty() { |
| 63 |
m := NewTTLMap(1) |
| 64 |
m.RemoveLastUsed(100) |
| 65 |
} |
| 66 |
|
| 67 |
func (s *TTLMapSuite) TestGetSetExpire() { |
| 68 |
m := NewTTLMap(1) |
| 69 |
|
| 70 |
err := m.Set("a", 1, 1) |
| 71 |
s.Require().Equal(nil, err) |
| 72 |
|
| 73 |
valI, exists := m.Get("a") |
| 74 |
s.Require().Equal(true, exists) |
| 75 |
s.Require().Equal(1, valI) |
| 76 |
|
| 77 |
clock.Advance(1 * clock.Second) |
| 78 |
|
| 79 |
_, exists = m.Get("a") |
| 80 |
s.Require().Equal(false, exists) |
| 81 |
} |
| 82 |
|
| 83 |
func (s *TTLMapSuite) TestSetOverwrite() { |
| 84 |
m := NewTTLMap(1) |
| 85 |
|
| 86 |
err := m.Set("o", 1, 1) |
| 87 |
s.Require().Equal(nil, err) |
| 88 |
|
| 89 |
valI, exists := m.Get("o") |
| 90 |
s.Require().Equal(true, exists) |
| 91 |
s.Require().Equal(1, valI) |
| 92 |
|
| 93 |
err = m.Set("o", 2, 1) |
| 94 |
s.Require().Equal(nil, err) |
| 95 |
|
| 96 |
valI, exists = m.Get("o") |
| 97 |
s.Require().Equal(true, exists) |
| 98 |
s.Require().Equal(2, valI) |
| 99 |
} |
| 100 |
|
| 101 |
func (s *TTLMapSuite) TestRemoveExpiredEdgeCase() { |
| 102 |
m := NewTTLMap(1) |
| 103 |
|
| 104 |
err := m.Set("a", 1, 1) |
| 105 |
s.Require().Equal(nil, err) |
| 106 |
|
| 107 |
clock.Advance(1 * clock.Second) |
| 108 |
|
| 109 |
err = m.Set("b", 2, 1) |
| 110 |
s.Require().Equal(nil, err) |
| 111 |
|
| 112 |
valI, exists := m.Get("a") |
| 113 |
s.Require().Equal(false, exists) |
| 114 |
|
| 115 |
valI, exists = m.Get("b") |
| 116 |
s.Require().Equal(true, exists) |
| 117 |
s.Require().Equal(2, valI) |
| 118 |
|
| 119 |
s.Require().Equal(1, m.Len()) |
| 120 |
} |
| 121 |
|
| 122 |
func (s *TTLMapSuite) TestRemoveOutOfCapacity() { |
| 123 |
m := NewTTLMap(2) |
| 124 |
|
| 125 |
err := m.Set("a", 1, 5) |
| 126 |
s.Require().Equal(nil, err) |
| 127 |
|
| 128 |
clock.Advance(1 * clock.Second) |
| 129 |
|
| 130 |
err = m.Set("b", 2, 6) |
| 131 |
s.Require().Equal(nil, err) |
| 132 |
|
| 133 |
err = m.Set("c", 3, 10) |
| 134 |
s.Require().Equal(nil, err) |
| 135 |
|
| 136 |
valI, exists := m.Get("a") |
| 137 |
s.Require().Equal(false, exists) |
| 138 |
|
| 139 |
valI, exists = m.Get("b") |
| 140 |
s.Require().Equal(true, exists) |
| 141 |
s.Require().Equal(2, valI) |
| 142 |
|
| 143 |
valI, exists = m.Get("c") |
| 144 |
s.Require().Equal(true, exists) |
| 145 |
s.Require().Equal(3, valI) |
| 146 |
|
| 147 |
s.Require().Equal(2, m.Len()) |
| 148 |
} |
| 149 |
|
| 150 |
func (s *TTLMapSuite) TestGetNotExists() { |
| 151 |
m := NewTTLMap(1) |
| 152 |
_, exists := m.Get("a") |
| 153 |
s.Require().Equal(false, exists) |
| 154 |
} |
| 155 |
|
| 156 |
func (s *TTLMapSuite) TestGetIntNotExists() { |
| 157 |
m := NewTTLMap(1) |
| 158 |
_, exists, err := m.GetInt("a") |
| 159 |
s.Require().Equal(nil, err) |
| 160 |
s.Require().Equal(false, exists) |
| 161 |
} |
| 162 |
|
| 163 |
func (s *TTLMapSuite) TestGetInvalidType() { |
| 164 |
m := NewTTLMap(1) |
| 165 |
m.Set("a", "banana", 5) |
| 166 |
|
| 167 |
_, _, err := m.GetInt("a") |
| 168 |
s.Require().EqualError(err, "Expected existing value to be integer, got string") |
| 169 |
|
| 170 |
_, err = m.Increment("a", 4, 1) |
| 171 |
s.Require().EqualError(err, "Expected existing value to be integer, got string") |
| 172 |
} |
| 173 |
|
| 174 |
func (s *TTLMapSuite) TestIncrementGetExpire() { |
| 175 |
m := NewTTLMap(1) |
| 176 |
|
| 177 |
m.Increment("a", 5, 1) |
| 178 |
val, exists, err := m.GetInt("a") |
| 179 |
|
| 180 |
s.Require().Equal(nil, err) |
| 181 |
s.Require().Equal(true, exists) |
| 182 |
s.Require().Equal(5, val) |
| 183 |
|
| 184 |
clock.Advance(1 * clock.Second) |
| 185 |
|
| 186 |
m.Increment("a", 4, 1) |
| 187 |
val, exists, err = m.GetInt("a") |
| 188 |
|
| 189 |
s.Require().Equal(nil, err) |
| 190 |
s.Require().Equal(true, exists) |
| 191 |
s.Require().Equal(4, val) |
| 192 |
} |
| 193 |
|
| 194 |
func (s *TTLMapSuite) TestIncrementOverwrite() { |
| 195 |
m := NewTTLMap(1) |
| 196 |
|
| 197 |
m.Increment("a", 5, 1) |
| 198 |
val, exists, err := m.GetInt("a") |
| 199 |
|
| 200 |
s.Require().Equal(nil, err) |
| 201 |
s.Require().Equal(true, exists) |
| 202 |
s.Require().Equal(5, val) |
| 203 |
|
| 204 |
m.Increment("a", 4, 1) |
| 205 |
val, exists, err = m.GetInt("a") |
| 206 |
|
| 207 |
s.Require().Equal(nil, err) |
| 208 |
s.Require().Equal(true, exists) |
| 209 |
s.Require().Equal(9, val) |
| 210 |
} |
| 211 |
|
| 212 |
func (s *TTLMapSuite) TestIncrementOutOfCapacity() { |
| 213 |
m := NewTTLMap(1) |
| 214 |
|
| 215 |
m.Increment("a", 5, 1) |
| 216 |
val, exists, err := m.GetInt("a") |
| 217 |
|
| 218 |
s.Require().Equal(nil, err) |
| 219 |
s.Require().Equal(true, exists) |
| 220 |
s.Require().Equal(5, val) |
| 221 |
|
| 222 |
m.Increment("b", 4, 1) |
| 223 |
val, exists, err = m.GetInt("b") |
| 224 |
|
| 225 |
s.Require().Equal(nil, err) |
| 226 |
s.Require().Equal(true, exists) |
| 227 |
s.Require().Equal(4, val) |
| 228 |
|
| 229 |
val, exists, err = m.GetInt("a") |
| 230 |
|
| 231 |
s.Require().Equal(nil, err) |
| 232 |
s.Require().Equal(false, exists) |
| 233 |
} |
| 234 |
|
| 235 |
func (s *TTLMapSuite) TestIncrementRemovesExpired() { |
| 236 |
m := NewTTLMap(2) |
| 237 |
|
| 238 |
m.Increment("a", 1, 1) |
| 239 |
m.Increment("b", 2, 2) |
| 240 |
|
| 241 |
clock.Advance(1 * clock.Second) |
| 242 |
m.Increment("c", 3, 3) |
| 243 |
|
| 244 |
val, exists, err := m.GetInt("a") |
| 245 |
|
| 246 |
s.Require().Equal(nil, err) |
| 247 |
s.Require().Equal(false, exists) |
| 248 |
|
| 249 |
val, exists, err = m.GetInt("b") |
| 250 |
s.Require().Equal(nil, err) |
| 251 |
s.Require().Equal(true, exists) |
| 252 |
s.Require().Equal(2, val) |
| 253 |
|
| 254 |
val, exists, err = m.GetInt("c") |
| 255 |
s.Require().Equal(nil, err) |
| 256 |
s.Require().Equal(true, exists) |
| 257 |
s.Require().Equal(3, val) |
| 258 |
} |
| 259 |
|
| 260 |
func (s *TTLMapSuite) TestIncrementRemovesLastUsed() { |
| 261 |
m := NewTTLMap(2) |
| 262 |
|
| 263 |
m.Increment("a", 1, 10) |
| 264 |
m.Increment("b", 2, 11) |
| 265 |
m.Increment("c", 3, 12) |
| 266 |
|
| 267 |
val, exists, err := m.GetInt("a") |
| 268 |
|
| 269 |
s.Require().Equal(nil, err) |
| 270 |
s.Require().Equal(false, exists) |
| 271 |
|
| 272 |
val, exists, err = m.GetInt("b") |
| 273 |
s.Require().Equal(nil, err) |
| 274 |
s.Require().Equal(true, exists) |
| 275 |
|
| 276 |
s.Require().Equal(2, val) |
| 277 |
|
| 278 |
val, exists, err = m.GetInt("c") |
| 279 |
s.Require().Equal(nil, err) |
| 280 |
s.Require().Equal(true, exists) |
| 281 |
s.Require().Equal(3, val) |
| 282 |
} |
| 283 |
|
| 284 |
func (s *TTLMapSuite) TestIncrementUpdatesTtl() { |
| 285 |
m := NewTTLMap(1) |
| 286 |
|
| 287 |
m.Increment("a", 1, 1) |
| 288 |
m.Increment("a", 1, 10) |
| 289 |
|
| 290 |
clock.Advance(1 * clock.Second) |
| 291 |
|
| 292 |
val, exists, err := m.GetInt("a") |
| 293 |
s.Require().Equal(nil, err) |
| 294 |
s.Require().Equal(true, exists) |
| 295 |
s.Require().Equal(2, val) |
| 296 |
} |
| 297 |
|
| 298 |
func (s *TTLMapSuite) TestUpdate() { |
| 299 |
m := NewTTLMap(1) |
| 300 |
|
| 301 |
m.Increment("a", 1, 1) |
| 302 |
m.Increment("a", 1, 10) |
| 303 |
|
| 304 |
clock.Advance(1 * clock.Second) |
| 305 |
|
| 306 |
val, exists, err := m.GetInt("a") |
| 307 |
s.Require().Equal(nil, err) |
| 308 |
s.Require().Equal(true, exists) |
| 309 |
s.Require().Equal(2, val) |
| 310 |
} |
| 311 |
|
| 312 |
func (s *TTLMapSuite) TestCallOnExpire() { |
| 313 |
var called bool |
| 314 |
var key string |
| 315 |
var val interface{} |
| 316 |
m := NewTTLMap(1) |
| 317 |
m.OnExpire = func(k string, el interface{}) { |
| 318 |
called = true |
| 319 |
key = k |
| 320 |
val = el |
| 321 |
} |
| 322 |
|
| 323 |
err := m.Set("a", 1, 1) |
| 324 |
s.Require().Equal(nil, err) |
| 325 |
|
| 326 |
valI, exists := m.Get("a") |
| 327 |
s.Require().Equal(true, exists) |
| 328 |
s.Require().Equal(1, valI) |
| 329 |
|
| 330 |
clock.Advance(1 * clock.Second) |
| 331 |
|
| 332 |
_, exists = m.Get("a") |
| 333 |
s.Require().Equal(false, exists) |
| 334 |
s.Require().Equal(true, called) |
| 335 |
s.Require().Equal("a", key) |
| 336 |
s.Require().Equal(1, val) |
| 337 |
} |