scripts/image-fixes/32-duration-wait-rounds-up.stDuration>>asDelay, new test
DurationTest>>testAsDelayDoesNotRoundDownDuration>>asDelay makes a Delay for self asMilliSeconds milliseconds.
asMilliSeconds rounds down, so a wait for 99.98 milliseconds is a Delay of
99 milliseconds, and Duration>>wait can end before the Duration has
passed.
The stock VM hides the defect: it ends each Delay 1 to 6 milliseconds late
on the test machine. Our VM ends a Delay 0.1 to 0.4 milliseconds late.
scripts/cogdiff/delay_accuracy.st gives, for the shortest of 40 waits of
(Duration nanoSeconds: 99980000) wait, minus the Duration:
| VM | Shortest wait minus the Duration |
|---|---|
| our VM | -921 microseconds |
| stock Cog | +4629 microseconds |
The repair rounds up: a part of a millisecond counts as one more millisecond. A negative Duration gives the same Delay as before.
| Test | Defect in | State | Cause |
|---|---|---|---|
DurationTest>>testAsDelayDoesNotRoundDown (new) |
image code | repaired | The Delay of a Duration was shorter than the Duration. |
TkThreadTest>>testPeriodFixedDuration (package PharoMisc, Tasks) |
image code | repaired | Waits that are 1 millisecond short gave a task a fourth run in 300 milliseconds. |
The new test fails on the two VMs before the repair.
Last full sweeps of 2026-11-17: iospharo (sweep14-new14) and stock Cog with the Cog changes (sweepcogp14-new10), with all image fixes. "Before" is the state of the test before the repairs. The Stock Cog column has a value only where the record measured stock Cog: "PASS 3 of 3 (repair run)" is the check of each repair on stock Cog v12.0.6 with the fix entry; "-" is not measured separately.
| Test | Before | State | iospharo VM | Patched Cog | Stock Cog |
|---|---|---|---|---|---|
DurationTest>>testAsDelayDoesNotRoundDown | repaired | PASS | PASS | - | |
TkThreadTest>>testPeriodFixedDuration | repaired | not run | not run | - |
Run the tests in a Pharo 14 image (build 791). Some of these tests are new: the entries file adds them, so file in the entries file first, then run them. These classes are not in the Pharo image but in a package (see the record): TkThreadTest.
"Run in a Playground of a Pharo 14 image (Do it and print each line)." ((DurationTest selector: #testAsDelayDoesNotRoundDown) run) printString. ((TkThreadTest selector: #testPeriodFixedDuration) run) printString.
How to apply an entries file: file in 00-header.st and the entries file as one chunk (the iospharo script scripts/gen-image-fixes.py makes that chunk). Each entry checks for the defect first and prints FIXED, ADDED or SKIP. Download 00-header.st.
"System-Time: a wait for a Duration can end before the Duration has passed (2026-11-17). Found by the package run: PharoMisc, Tasks, TkThreadTest>>testPeriodFixedDuration, which failed on our VM only. Duration>>asDelay makes a Delay for 'self asMilliSeconds' milliseconds, and asMilliSeconds rounds down. A wait for 99.98 milliseconds is a Delay of 99 milliseconds. Duration>>wait uses asDelay. Measured with scripts/cogdiff/delay_accuracy.st, 40 waits of '(Duration nanoSeconds: 99980000) wait', shortest wait minus the Duration: our VM -921 microseconds (the wait ends before its time) stock Cog +4629 microseconds The stock VM ends each Delay 1 to 6 milliseconds late on this machine, and that hides the defect. Our VM ends a Delay 0.1 to 0.4 milliseconds late. The Tasks package waits 'dueTime - now' for each period. With waits that are 1 millisecond short, a task that repeats each 100 milliseconds for 300 milliseconds gets a fourth run before the 300 milliseconds end. The repair rounds up: a part of a millisecond counts as one more millisecond. A negative Duration gives the same Delay as before (an error in Delay>>setDelay:forSemaphore:). The new test fails on the two VMs before the repair." fix value: #Duration value: #asDelay value: [ :source | source includesSubstring: 'Delay forMilliseconds: self asMilliSeconds' ] value: 'asDelay "A wait must not end before the receiver has passed. A Delay counts milliseconds, so a part of a millisecond counts as one more millisecond. (asMilliSeconds rounds down: a wait for 99.98 milliseconds ended after 99 milliseconds.)" | nanoseconds | nanoseconds := self asNanoSeconds. ^ Delay forMilliseconds: (nanoseconds negative ifTrue: [ nanoseconds // 1000000 ] ifFalse: [ nanoseconds + 999999 // 1000000 ])'. add value: #DurationTest value: #testAsDelayDoesNotRoundDown value: 'tests' value: 'testAsDelayDoesNotRoundDown "The Delay of a Duration must not be shorter than the Duration." self assert: (Duration nanoSeconds: 99980000) asDelay millisecondDelayDuration equals: 100. self assert: (Duration nanoSeconds: 100000000) asDelay millisecondDelayDuration equals: 100. self assert: (Duration nanoSeconds: 100000001) asDelay millisecondDelayDuration equals: 101. self assert: (Duration nanoSeconds: 1) asDelay millisecondDelayDuration equals: 1. self assert: Duration zero asDelay millisecondDelayDuration equals: 0'.
State of 2026-11-20. Made from the test records of the iospharo project (a second Pharo VM). The fixes are proposals for review.