Increase reliability of TestResetSessionTimerLocked_Renew

pull/5874/head
Freddy 2019-05-24 13:54:51 -04:00 committed by GitHub
parent 27f05b16a0
commit 6b31482333
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 35 additions and 34 deletions

View File

@ -170,51 +170,52 @@ func TestResetSessionTimerLocked(t *testing.T) {
} }
func TestResetSessionTimerLocked_Renew(t *testing.T) { func TestResetSessionTimerLocked_Renew(t *testing.T) {
t.Parallel()
dir1, s1 := testServer(t) dir1, s1 := testServer(t)
defer os.RemoveAll(dir1) defer os.RemoveAll(dir1)
defer s1.Shutdown() defer s1.Shutdown()
ttl := 100 * time.Millisecond ttl := 100 * time.Millisecond
// create the timer retry.Run(t, func(r *retry.R) {
// create the timer and make verify it was created
s1.createSessionTimer("foo", ttl) s1.createSessionTimer("foo", ttl)
if s1.sessionTimers.Get("foo") == nil { if s1.sessionTimers.Get("foo") == nil {
t.Fatalf("missing timer") r.Fatalf("missing timer")
} }
// wait until it is "expired" but at this point // wait until it is "expired" but still exists
// the session still exists. // the session will exist until 2*ttl
time.Sleep(ttl) time.Sleep(ttl)
if s1.sessionTimers.Get("foo") == nil { if s1.sessionTimers.Get("foo") == nil {
t.Fatal("missing timer") r.Fatal("missing timer")
} }
})
retry.Run(t, func(r *retry.R) {
// renew the session which will reset the TTL to 2*ttl // renew the session which will reset the TTL to 2*ttl
// since that is the current SessionTTLMultiplier // since that is the current SessionTTLMultiplier
s1.createSessionTimer("foo", ttl) s1.createSessionTimer("foo", ttl)
if s1.sessionTimers.Get("foo") == nil {
// Watch for invalidation r.Fatal("missing timer")
renew := time.Now()
deadline := renew.Add(2 * structs.SessionTTLMultiplier * ttl)
for {
now := time.Now()
if now.After(deadline) {
t.Fatal("should have expired by now")
} }
renew := time.Now()
// timer still exists // Ensure invalidation happens after ttl
for {
// if timer still exists, sleep and continue
if s1.sessionTimers.Get("foo") != nil { if s1.sessionTimers.Get("foo") != nil {
time.Sleep(time.Millisecond) time.Sleep(time.Millisecond)
continue continue
} }
// timer gone // fail if timer gone before ttl passes
now := time.Now()
if now.Sub(renew) < ttl { if now.Sub(renew) < ttl {
t.Fatalf("early invalidate") r.Fatalf("early invalidate")
} }
break break
} }
})
} }
func TestInvalidateSession(t *testing.T) { func TestInvalidateSession(t *testing.T) {