Fix timing issue in sub-second expires test (#9821)

The `PEXPIRE/PSETEX/PEXPIREAT can set sub-second expires` test is
a very time sensitive test, it used to occasionally fail on MacOS.

It will perform there internal tests in a loop, as long as one
fails, it will try to excute again in the next loop.

oranagra suggested that we can split it into three individual tests,
so that if one fails, we do not need to retry the others. And maybe
it will increase the chances of success dramatically.

Each is executed 500 times, and the number of retries is collected:
```
PSETEX, total: 500, sum: 745, min: 0, max: 13, avg: 1.49

PEXPIRE, total: 500, sum: 575, min: 0, max: 16, avg: 1.15

PEXPIREAT, total: 500, sum: 0, min: 0, max: 0, avg: 0.0

ALL(old_way), total: 500, sum: 8090, min: 0, max: 138, avg: 16.18
```

And we can see the threshold is very low.
Splitting the test also makes the code better to maintain.

Co-authored-by: Oran Agra <oran@redislabs.com>
This commit is contained in:
Binbin 2021-11-22 14:39:27 +08:00 committed by GitHub
parent 183b90a625
commit 698b577413
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -91,20 +91,27 @@ start_server {tags {"expire"}} {
list $a $b
} {somevalue {}}
test {PEXPIRE/PSETEX/PEXPIREAT can set sub-second expires} {
# This test is very likely to do a false positive if the
# server is under pressure, so if it does not work give it a few more
# chances.
test "PSETEX can set sub-second expires" {
# This test is very likely to do a false positive if the server is
# under pressure, so if it does not work give it a few more chances.
for {set j 0} {$j < 50} {incr j} {
r del x
r del y
r del z
r psetex x 100 somevalue
after 80
set a [r get x]
after 120
set b [r get x]
if {$a eq {somevalue} && $b eq {}} break
}
if {$::verbose} { puts "PSETEX sub-second expire test attempts: $j" }
list $a $b
} {somevalue {}}
test "PEXPIRE can set sub-second expires" {
# This test is very likely to do a false positive if the server is
# under pressure, so if it does not work give it a few more chances.
for {set j 0} {$j < 50} {incr j} {
r set x somevalue
r pexpire x 100
after 80
@ -112,6 +119,16 @@ start_server {tags {"expire"}} {
after 120
set d [r get x]
if {$c eq {somevalue} && $d eq {}} break
}
if {$::verbose} { puts "PEXPIRE sub-second expire test attempts: $j" }
list $c $d
} {somevalue {}}
test "PEXPIREAT can set sub-second expires" {
# This test is very likely to do a false positive if the server is
# under pressure, so if it does not work give it a few more chances.
for {set j 0} {$j < 50} {incr j} {
r set x somevalue
set now [r time]
r pexpireat x [expr ([lindex $now 0]*1000)+([lindex $now 1]/1000)+200]
@ -120,15 +137,11 @@ start_server {tags {"expire"}} {
after 220
set f [r get x]
if {$a eq {somevalue} && $b eq {} &&
$c eq {somevalue} && $d eq {} &&
$e eq {somevalue} && $f eq {}} break
if {$e eq {somevalue} && $f eq {}} break
}
if {$::verbose} {
puts "sub-second expire test attempts: $j"
}
list $a $b $c $d $e $f
} {somevalue {} somevalue {} somevalue {}}
if {$::verbose} { puts "PEXPIREAT sub-second expire test attempts: $j" }
list $e $f
} {somevalue {}}
test {TTL returns time to live in seconds} {
r del x