timed_map_test.go 680 B

1234567891011121314151617181920212223242526272829303132333435
  1. package collect
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/v2ray/v2ray-core/testing/unit"
  6. )
  7. func TestTimedStringMap(t *testing.T) {
  8. assert := unit.Assert(t)
  9. nowSec := time.Now().UTC().Unix()
  10. m := NewTimedStringMap(3)
  11. m.Set("Key1", "Value1", nowSec)
  12. m.Set("Key2", "Value2", nowSec+20)
  13. v1, ok := m.Get("Key1")
  14. assert.Bool(ok).IsTrue()
  15. assert.String(v1.(string)).Equals("Value1")
  16. v2, ok := m.Get("Key2")
  17. assert.Bool(ok).IsTrue()
  18. assert.String(v2.(string)).Equals("Value2")
  19. tick := time.Tick(5 * time.Second)
  20. <-tick
  21. v1, ok = m.Get("Key1")
  22. assert.Bool(ok).IsFalse()
  23. v2, ok = m.Get("Key2")
  24. assert.Bool(ok).IsTrue()
  25. assert.String(v2.(string)).Equals("Value2")
  26. }