timed_queue_test.go 1.0 KB

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