| Tim Windelschmidt | 6d33a43 | 2025-02-04 14:34:25 +0100 | [diff] [blame] | 1 | // Copyright The Monogon Project Authors. |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| Serge Bazanski | e643fd6 | 2023-02-14 00:01:38 +0100 | [diff] [blame] | 4 | package sinbin |
| 5 | |
| 6 | import ( |
| 7 | "testing" |
| 8 | "time" |
| 9 | ) |
| 10 | |
| 11 | // TestSinbinBasics performs some basic tests on the Sinbin. |
| 12 | func TestSinbinBasics(t *testing.T) { |
| 13 | var s Sinbin[string] |
| 14 | |
| 15 | if s.Penalized("foo") { |
| 16 | t.Errorf("'foo' should not be penalized as it hasn't yet been added") |
| 17 | } |
| 18 | s.Add("foo", time.Now().Add(-1*time.Second)) |
| 19 | if s.Penalized("foo") { |
| 20 | t.Errorf("'foo' should not be penalized as it has been added with an expired time") |
| 21 | } |
| 22 | s.Add("bar", time.Now().Add(time.Hour)) |
| 23 | if !s.Penalized("bar") { |
| 24 | t.Errorf("'bar' should be penalized as it has been added with an hour long penalty") |
| 25 | } |
| 26 | |
| 27 | // Force sweep. |
| 28 | s.lastSweep = time.Now().Add(-1 * time.Hour) |
| 29 | s.sweepUnlocked() |
| 30 | |
| 31 | if len(s.bench) != 1 { |
| 32 | t.Errorf("there should only be one element on the bench") |
| 33 | } |
| 34 | if _, ok := s.bench["bar"]; !ok { |
| 35 | t.Errorf("the bench should contain 'bar'") |
| 36 | } |
| 37 | } |