2015-09-27 19:11:40 -04:00
|
|
|
package collect
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2015-12-02 09:27:18 -05:00
|
|
|
v2testing "github.com/v2ray/v2ray-core/testing"
|
|
|
|
"github.com/v2ray/v2ray-core/testing/assert"
|
2015-09-27 19:11:40 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestTimedQueue(t *testing.T) {
|
2015-12-02 09:27:18 -05:00
|
|
|
v2testing.Current(t)
|
2015-09-27 19:11:40 -04:00
|
|
|
|
|
|
|
removed := make(map[string]bool)
|
|
|
|
|
2015-10-29 07:04:14 -04:00
|
|
|
nowSec := time.Now().Unix()
|
2015-09-27 19:11:40 -04:00
|
|
|
q := NewTimedQueue(2)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
entry := <-q.RemovedEntries()
|
|
|
|
removed[entry.(string)] = true
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
q.Add("Value1", nowSec)
|
|
|
|
q.Add("Value2", nowSec+5)
|
|
|
|
|
|
|
|
v1, ok := removed["Value1"]
|
|
|
|
assert.Bool(ok).IsFalse()
|
|
|
|
|
|
|
|
v2, ok := removed["Value2"]
|
|
|
|
assert.Bool(ok).IsFalse()
|
|
|
|
|
|
|
|
tick := time.Tick(4 * time.Second)
|
|
|
|
<-tick
|
|
|
|
|
|
|
|
v1, ok = removed["Value1"]
|
|
|
|
assert.Bool(ok).IsTrue()
|
|
|
|
assert.Bool(v1).IsTrue()
|
|
|
|
removed["Value1"] = false
|
|
|
|
|
|
|
|
v2, ok = removed["Value2"]
|
|
|
|
assert.Bool(ok).IsFalse()
|
|
|
|
|
|
|
|
<-tick
|
|
|
|
v2, ok = removed["Value2"]
|
|
|
|
assert.Bool(ok).IsTrue()
|
|
|
|
assert.Bool(v2).IsTrue()
|
|
|
|
removed["Value2"] = false
|
|
|
|
|
|
|
|
<-tick
|
|
|
|
assert.Bool(removed["Values"]).IsFalse()
|
|
|
|
|
2015-10-29 07:04:14 -04:00
|
|
|
q.Add("Value1", time.Now().Unix()+10)
|
2015-09-27 19:11:40 -04:00
|
|
|
|
|
|
|
<-tick
|
|
|
|
v1, ok = removed["Value1"]
|
|
|
|
assert.Bool(ok).IsTrue()
|
|
|
|
assert.Bool(v1).IsFalse()
|
|
|
|
}
|