timed_queue_test.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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)
  13. go func() {
  14. for {
  15. entry := <-q.RemovedEntries()
  16. removed[entry.(string)] = true
  17. }
  18. }()
  19. q.Add("Value1", nowSec)
  20. q.Add("Value2", nowSec+5)
  21. v1, ok := removed["Value1"]
  22. assert.Bool(ok).IsFalse()
  23. v2, ok := removed["Value2"]
  24. assert.Bool(ok).IsFalse()
  25. tick := time.Tick(4 * time.Second)
  26. <-tick
  27. v1, ok = removed["Value1"]
  28. assert.Bool(ok).IsTrue()
  29. assert.Bool(v1).IsTrue()
  30. removed["Value1"] = false
  31. v2, ok = removed["Value2"]
  32. assert.Bool(ok).IsFalse()
  33. <-tick
  34. v2, ok = removed["Value2"]
  35. assert.Bool(ok).IsTrue()
  36. assert.Bool(v2).IsTrue()
  37. removed["Value2"] = false
  38. <-tick
  39. assert.Bool(removed["Values"]).IsFalse()
  40. q.Add("Value1", time.Now().Unix()+10)
  41. <-tick
  42. v1, ok = removed["Value1"]
  43. assert.Bool(ok).IsTrue()
  44. assert.Bool(v1).IsFalse()
  45. }