tests: make test more tolerant to windows low precision timer default interval

pull/8345/head
Delyan Angelov 2021-01-25 20:04:29 +02:00
parent e0ed224a8d
commit ea11488339
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
1 changed files with 14 additions and 8 deletions

View File

@ -21,7 +21,9 @@ fn f1(ch1 chan int, ch2 chan St, ch3 chan int, ch4 chan int, ch5 chan int, sem s
ch3 <- 5 { ch3 <- 5 {
a = 1 a = 1
} }
ch2 <- St{a: 37} { ch2 <- St{
a: 37
} {
a = 2 a = 2
} }
ch4 <- (6 + 7 * 9) { ch4 <- (6 + 7 * 9) {
@ -56,7 +58,7 @@ fn f2(ch1 chan St, ch2 chan int, sem sync.Semaphore) {
} }
} }
sem.post() sem.post()
} }
fn test_select_blocks() { fn test_select_blocks() {
ch1 := chan int{cap: 1} ch1 := chan int{cap: 1}
@ -67,7 +69,7 @@ fn test_select_blocks() {
sem := sync.new_semaphore() sem := sync.new_semaphore()
mut r := false mut r := false
t := select { t := select {
b := <- ch1 { b := <-ch1 {
println(b) println(b)
} }
else { else {
@ -80,20 +82,26 @@ fn test_select_blocks() {
go f2(ch2, ch3, sem) go f2(ch2, ch3, sem)
n := <-ch3 n := <-ch3
assert n == 23 assert n == 23
ch2 <- St{a: 13} ch2 <- St{
a: 13
}
sem.wait() sem.wait()
stopwatch := time.new_stopwatch({}) stopwatch := time.new_stopwatch({})
go f1(ch1, ch2, ch3, ch4, ch5, sem) go f1(ch1, ch2, ch3, ch4, ch5, sem)
sem.wait() sem.wait()
elapsed_ms := f64(stopwatch.elapsed()) / time.millisecond elapsed_ms := f64(stopwatch.elapsed()) / time.millisecond
assert elapsed_ms >= 295.0 // https://docs.microsoft.com/en-us/windows-hardware/drivers/kernel/high-resolution-timers
// > For example, for Windows running on an x86 processor, the default interval between
// > system clock ticks is typically about 15 milliseconds, and the minimum interval
// > between system clock ticks is about 1 millisecond.
assert elapsed_ms >= 280.0 // 300 - (15ms + 5ms just in case)
ch1.close() ch1.close()
ch2.close() ch2.close()
mut h := 7 mut h := 7
mut is_open := true mut is_open := true
if select { if select {
b := <- ch2 { _ := <-ch2 {
h = 0 h = 0
} }
ch1 <- h { ch1 <- h {
@ -112,5 +120,3 @@ fn test_select_blocks() {
// since all channels are closed `select` should return `false` // since all channels are closed `select` should return `false`
assert is_open == false assert is_open == false
} }