github.com/grailbio/base@v0.0.11/file/s3file/s3transport/expiring_map_test.go (about) 1 package s3transport 2 3 import ( 4 "net" 5 "testing" 6 "time" 7 8 "github.com/stretchr/testify/assert" 9 ) 10 11 func TestExpiringMap(t *testing.T) { 12 ips := func(is ...byte) (ret []net.IP) { 13 for _, i := range is { 14 ret = append(ret, net.IP{i, i, i, i}) 15 } 16 return 17 } 18 var stubNow time.Time 19 20 m := newExpiringMap(noOpRunPeriodic, func() time.Time { return stubNow }) 21 22 stubNow = time.Unix(1600000000, 0) 23 assert.ElementsMatch(t, ips(0, 1), m.AddAndGet("s3.example.com", ips(0, 1))) 24 25 stubNow = stubNow.Add(expireAfter / 2) 26 assert.ElementsMatch(t, ips(0, 1), m.AddAndGet("s3.example.com", ips(0))) 27 assert.ElementsMatch(t, ips(0, 1, 3), m.AddAndGet("s3.example.com", ips(3))) 28 29 stubNow = stubNow.Add(expireAfter/2 + 2) 30 m.expireOnce(stubNow) // Drop ips(1). 31 assert.ElementsMatch(t, ips(0, 3, 4), m.AddAndGet("s3.example.com", ips(4))) 32 assert.ElementsMatch(t, ips(100), m.AddAndGet("s3-2.example.com", ips(100))) 33 34 stubNow = stubNow.Add(expireAfter/2 + 2) 35 m.expireOnce(stubNow) // Drop ips(0, 3). 36 assert.ElementsMatch(t, ips(4), m.AddAndGet("s3.example.com", nil)) 37 assert.ElementsMatch(t, ips(100), m.AddAndGet("s3-2.example.com", nil)) 38 39 m.logOnce() // No assertions other than it shouldn't panic. 40 }