2015-09-23 14:14:12 -04: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 14:22:09 -04:00
|
|
|
m := NewTimedStringMap(2)
|
2015-09-23 14:14:12 -04:00
|
|
|
m.Set("Key1", "Value1", nowSec)
|
2015-09-23 14:22:09 -04:00
|
|
|
m.Set("Key2", "Value2", nowSec+5)
|
2015-09-23 14:14:12 -04: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 16:02:55 -04:00
|
|
|
tick := time.Tick(4 * time.Second)
|
2015-09-23 14:14:12 -04: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 14:22:09 -04:00
|
|
|
|
|
|
|
<-tick
|
|
|
|
v2, ok = m.Get("Key2")
|
|
|
|
assert.Bool(ok).IsFalse()
|
|
|
|
|
|
|
|
<-tick
|
|
|
|
v2, ok = m.Get("Key2")
|
|
|
|
assert.Bool(ok).IsFalse()
|
|
|
|
|
2015-09-23 14:24:30 -04:00
|
|
|
m.Set("Key1", "Value1", time.Now().UTC().Unix()+10)
|
2015-09-23 14:22:09 -04:00
|
|
|
v1, ok = m.Get("Key1")
|
|
|
|
assert.Bool(ok).IsTrue()
|
|
|
|
assert.String(v1.(string)).Equals("Value1")
|
2015-09-23 14:14:12 -04:00
|
|
|
}
|