Skip to content

Commit 97e740e

Browse files
committed
runtime: replace TestFutexsleep with TestTimediv
TestFutexsleep was originally created in CL 7876043 as a regression test for buggy division logic in futexsleep. Several months later CL 11575044 moved this logic to timediv (called by futexsleep). This test calls runtime.Futexsleep, which temporarily disables asynchronous preemption. Unfortunately, TestFutexSleep calls this from multiple goroutines, creating a race condition that may result in asynchronous preemption remaining disabled for the remainder of the process lifetime. We could fix this by moving the async preemption disable to the main test function, however this test has had a history of flakiness. As an alternative, this CL replaces the test wholesale with a new test for timediv, covering the overflow logic without the difficulty of dealing with futex. Fixes #50749. Change-Id: If9e1dac63ef1535adb49f9a9ffcaff99b9135895 Reviewed-on: https://go-review.googlesource.com/c/go/+/380058 Trust: Michael Pratt <[email protected]> Run-TryBot: Michael Pratt <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Cherry Mui <[email protected]> Reviewed-by: Michael Knyszek <[email protected]>
1 parent f88c3b9 commit 97e740e

File tree

4 files changed

+78
-106
lines changed

4 files changed

+78
-106
lines changed

src/runtime/export_futex_test.go

-19
This file was deleted.

src/runtime/export_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -1328,3 +1328,5 @@ func Acquirem() {
13281328
func Releasem() {
13291329
releasem(getg().m)
13301330
}
1331+
1332+
var Timediv = timediv

src/runtime/futex_test.go

-87
This file was deleted.

src/runtime/runtime_test.go

+76
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package runtime_test
66

77
import (
88
"flag"
9+
"fmt"
910
"io"
1011
. "runtime"
1112
"runtime/debug"
@@ -362,3 +363,78 @@ func TestVersion(t *testing.T) {
362363
t.Fatalf("cr/nl in version: %q", vers)
363364
}
364365
}
366+
367+
func TestTimediv(t *testing.T) {
368+
for _, tc := range []struct {
369+
num int64
370+
div int32
371+
ret int32
372+
rem int32
373+
}{
374+
{
375+
num: 8,
376+
div: 2,
377+
ret: 4,
378+
rem: 0,
379+
},
380+
{
381+
num: 9,
382+
div: 2,
383+
ret: 4,
384+
rem: 1,
385+
},
386+
{
387+
// Used by runtime.check.
388+
num: 12345*1000000000 + 54321,
389+
div: 1000000000,
390+
ret: 12345,
391+
rem: 54321,
392+
},
393+
{
394+
num: 1<<32 - 1,
395+
div: 2,
396+
ret: 1<<31 - 1, // no overflow.
397+
rem: 1,
398+
},
399+
{
400+
num: 1 << 32,
401+
div: 2,
402+
ret: 1<<31 - 1, // overflow.
403+
rem: 0,
404+
},
405+
{
406+
num: 1 << 40,
407+
div: 2,
408+
ret: 1<<31 - 1, // overflow.
409+
rem: 0,
410+
},
411+
{
412+
num: 1<<40 + 1,
413+
div: 1 << 10,
414+
ret: 1 << 30,
415+
rem: 1,
416+
},
417+
} {
418+
name := fmt.Sprintf("%d div %d", tc.num, tc.div)
419+
t.Run(name, func(t *testing.T) {
420+
// Double check that the inputs make sense using
421+
// standard 64-bit division.
422+
ret64 := tc.num / int64(tc.div)
423+
rem64 := tc.num % int64(tc.div)
424+
if ret64 != int64(int32(ret64)) {
425+
// Simulate timediv overflow value.
426+
ret64 = 1<<31 - 1
427+
rem64 = 0
428+
}
429+
if ret64 != int64(tc.ret) {
430+
t.Errorf("%d / %d got ret %d rem %d want ret %d rem %d", tc.num, tc.div, ret64, rem64, tc.ret, tc.rem)
431+
}
432+
433+
var rem int32
434+
ret := Timediv(tc.num, tc.div, &rem)
435+
if ret != tc.ret || rem != tc.rem {
436+
t.Errorf("timediv %d / %d got ret %d rem %d want ret %d rem %d", tc.num, tc.div, ret, rem, tc.ret, tc.rem)
437+
}
438+
})
439+
}
440+
}

0 commit comments

Comments
 (0)