Weighs the soul of incoming HTTP requests to stop AI crawlers
at main 60 lines 1.3 kB view raw
1package decaymap 2 3import ( 4 "testing" 5 "time" 6) 7 8func TestImpl(t *testing.T) { 9 dm := New[string, string]() 10 11 dm.Set("test", "hi", 5*time.Minute) 12 13 val, ok := dm.Get("test") 14 if !ok { 15 t.Error("somehow the test key was not set") 16 } 17 18 if val != "hi" { 19 t.Errorf("wanted value %q, got: %q", "hi", val) 20 } 21 22 ok = dm.expire("test") 23 if !ok { 24 t.Error("somehow could not force-expire the test key") 25 } 26 27 _, ok = dm.Get("test") 28 if ok { 29 t.Error("got value even though it was supposed to be expired") 30 } 31} 32 33func TestCleanup(t *testing.T) { 34 dm := New[string, string]() 35 36 dm.Set("test1", "hi1", 1*time.Second) 37 dm.Set("test2", "hi2", 2*time.Second) 38 dm.Set("test3", "hi3", 3*time.Second) 39 40 dm.expire("test1") // Force expire test1 41 dm.expire("test2") // Force expire test2 42 43 dm.Cleanup() 44 45 finalLen := dm.Len() // Get the length after cleanup 46 47 if finalLen != 1 { // "test3" should be the only one left 48 t.Errorf("Cleanup failed to remove expired entries. Expected length 1, got %d", finalLen) 49 } 50 51 if _, ok := dm.Get("test1"); ok { // Verify Get still behaves correctly after Cleanup 52 t.Error("test1 should not be found after cleanup") 53 } 54 if _, ok := dm.Get("test2"); ok { 55 t.Error("test2 should not be found after cleanup") 56 } 57 if val, ok := dm.Get("test3"); !ok || val != "hi3" { 58 t.Error("test3 should still be found after cleanup") 59 } 60}