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) {
s1.createSessionTimer("foo", ttl) // create the timer and make verify it was created
if s1.sessionTimers.Get("foo") == nil { s1.createSessionTimer("foo", ttl)
t.Fatalf("missing timer") if s1.sessionTimers.Get("foo") == nil {
} r.Fatalf("missing timer")
// wait until it is "expired" but at this point
// the session still exists.
time.Sleep(ttl)
if s1.sessionTimers.Get("foo") == nil {
t.Fatal("missing timer")
}
// renew the session which will reset the TTL to 2*ttl
// since that is the current SessionTTLMultiplier
s1.createSessionTimer("foo", ttl)
// Watch for invalidation
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")
} }
// timer still exists // wait until it is "expired" but still exists
if s1.sessionTimers.Get("foo") != nil { // the session will exist until 2*ttl
time.Sleep(time.Millisecond) time.Sleep(ttl)
continue if s1.sessionTimers.Get("foo") == nil {
r.Fatal("missing timer")
} }
})
// timer gone retry.Run(t, func(r *retry.R) {
if now.Sub(renew) < ttl { // renew the session which will reset the TTL to 2*ttl
t.Fatalf("early invalidate") // since that is the current SessionTTLMultiplier
s1.createSessionTimer("foo", ttl)
if s1.sessionTimers.Get("foo") == nil {
r.Fatal("missing timer")
} }
break renew := time.Now()
}
// Ensure invalidation happens after ttl
for {
// if timer still exists, sleep and continue
if s1.sessionTimers.Get("foo") != nil {
time.Sleep(time.Millisecond)
continue
}
// fail if timer gone before ttl passes
now := time.Now()
if now.Sub(renew) < ttl {
r.Fatalf("early invalidate")
}
break
}
})
} }
func TestInvalidateSession(t *testing.T) { func TestInvalidateSession(t *testing.T) {