timed_queue_test.go 1020 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package collect
  2. import (
  3. "testing"
  4. "time"
  5. v2testing "github.com/v2ray/v2ray-core/testing"
  6. "github.com/v2ray/v2ray-core/testing/assert"
  7. )
  8. func TestTimedQueue(t *testing.T) {
  9. v2testing.Current(t)
  10. removed := make(map[string]bool)
  11. nowSec := time.Now().Unix()
  12. q := NewTimedQueue(2, func(v interface{}) {
  13. removed[v.(string)] = true
  14. })
  15. q.Add("Value1", nowSec)
  16. q.Add("Value2", nowSec+5)
  17. v1, ok := removed["Value1"]
  18. assert.Bool(ok).IsFalse()
  19. v2, ok := removed["Value2"]
  20. assert.Bool(ok).IsFalse()
  21. tick := time.Tick(4 * time.Second)
  22. <-tick
  23. v1, ok = removed["Value1"]
  24. assert.Bool(ok).IsTrue()
  25. assert.Bool(v1).IsTrue()
  26. removed["Value1"] = false
  27. v2, ok = removed["Value2"]
  28. assert.Bool(ok).IsFalse()
  29. <-tick
  30. v2, ok = removed["Value2"]
  31. assert.Bool(ok).IsTrue()
  32. assert.Bool(v2).IsTrue()
  33. removed["Value2"] = false
  34. <-tick
  35. assert.Bool(removed["Values"]).IsFalse()
  36. q.Add("Value1", time.Now().Unix()+10)
  37. <-tick
  38. v1, ok = removed["Value1"]
  39. assert.Bool(ok).IsTrue()
  40. assert.Bool(v1).IsFalse()
  41. }