v2ray-core/common/collect/timed_map_test.go

49 lines
948 B
Go
Raw Normal View History

2015-09-23 18:14:12 +00:00
package collect
import (
"testing"
"time"
"github.com/v2ray/v2ray-core/testing/unit"
)
func TestTimedStringMap(t *testing.T) {
assert := unit.Assert(t)
nowSec := time.Now().UTC().Unix()
2015-09-23 18:22:09 +00:00
m := NewTimedStringMap(2)
2015-09-23 18:14:12 +00:00
m.Set("Key1", "Value1", nowSec)
2015-09-23 18:22:09 +00:00
m.Set("Key2", "Value2", nowSec+5)
2015-09-23 18:14:12 +00:00
v1, ok := m.Get("Key1")
assert.Bool(ok).IsTrue()
assert.String(v1.(string)).Equals("Value1")
v2, ok := m.Get("Key2")
assert.Bool(ok).IsTrue()
assert.String(v2.(string)).Equals("Value2")
2015-09-23 20:02:55 +00:00
tick := time.Tick(4 * time.Second)
2015-09-23 18:14:12 +00:00
<-tick
v1, ok = m.Get("Key1")
assert.Bool(ok).IsFalse()
v2, ok = m.Get("Key2")
assert.Bool(ok).IsTrue()
assert.String(v2.(string)).Equals("Value2")
2015-09-23 18:22:09 +00:00
<-tick
v2, ok = m.Get("Key2")
assert.Bool(ok).IsFalse()
<-tick
v2, ok = m.Get("Key2")
assert.Bool(ok).IsFalse()
2015-09-23 18:24:30 +00:00
m.Set("Key1", "Value1", time.Now().UTC().Unix()+10)
2015-09-23 18:22:09 +00:00
v1, ok = m.Get("Key1")
assert.Bool(ok).IsTrue()
assert.String(v1.(string)).Equals("Value1")
2015-09-23 18:14:12 +00:00
}