A12 shut after lorry crash blocks road
Pierce Brosnan, 73, and wife Keely Shaye Smith, 62, make a rare appearance together as they head out for a lunch date in Beverly Hills
Van and car crash blocks A12 with heavy traffic building
Van and car crash blocks A12 with heavy traffic building
Law firm insisted on one password to rule them all
Physicists Create First Room-Temperature Quantum Material
Read more of this story at Slashdot.
Brentwood residents divided as controversial new Aldi to be built metres from M25
Call police if you see this man wanted for stealing a bag at Halstead pub
British man left with leg 'dangling off' after Thailand motorbike crash - and now facing £22,000 hospital bills
Argentina players hold banner claiming the Falkland Islands after knocking England out of the World Cup
The astonishing underworld of Bicester Village: How the Chinese tourists who come flocking are often buying designer bags and watches destined for a VERY sinister purpose
Homeowner's fury after neighbour builds elevated platform that looks like an 'East German lookout tower' - which overshadows her garden and has killed her plants
Jesy Nelson says 'I feel so proud' as she celebrates a huge win in her SMA campaign and confirms screening test will now be rolled out across the whole of the UK
Timothy Busfield tells grand jury he's been 'canceled' after being charged with child sexual abuse
US woman trapped in Iran since 2024 released by Tehran as a 'gesture of Goodwill'
Aggro on the streets: Armed police arrest England and Argentina fans in Atlanta as fans cause chaos in London following Three Lions' loss
Cruz Beckham's Brazilian girlfriend Jackie Apostel, 30, proves her loyalty to England in a Three Lions shirt as she consoles boyfriend, 21, and his family during World Cup semi-final defeat
CodeSOD: Wait Longer
Karen was maintaining some specification tests that were flaky. Not extremely flaky, but three or four times out of a thousand, the tests would just fail. The tests were complicated, and some of the operations were timing sensitive, so it wasn't precisely surprising- but the problem was that they were actually generous with their timing windows. The unit tests passed consistently, it was only these functional, specification-based tests that failed.
So, for example, there were sections in the tests where they wanted to wait at least 2ms. Since the code and tests were in TypeScript, they used the setTimeout function, which per standard JavaScript documentation warns that it may wait longer. But again, Karen was fine with longer.
Unfortunately for Karen, the documentation for NodeJS is less specific, as it makes no guarantees about when the timeout function gets invoked. This means that it can fire the timeout before the time has elapsed.
After many, many hours of debugging, that was exactly the situation that Karen found herself in. Which is why her very simple wait function went from:
export const wait = (ms:number) => new Promise((complete) => setTimeout(complete, ms));To the much more awkward:
export const wait = (ms: number) { const target = performance.now() + ms; return new Promise((complete) => { const checkReady = () => { if (performance.now() > target) { complete(); } else { setTimeout(checkReady, 1); } } setTimeout(checkReady, 1); }); }This version of the function checks the time every millisecond, and only completes the operation if we've waited at least as long as our target duration. This ensures that the timeout never fires too soon and it fixes the janky tests. But it's also terrible. Terrible that it exists. Terrible that this is the best solution. Terrible that our functional tests need to be so time sensitive. And terrible that the Node runtime actually breaks the one consistent scheduling guarantee that pretty much every other scheduler does: that it'll wait at least as long as you asked, but might wait much longer.
At best, we can say, "at least it's only testing code."
[Advertisement] Picking up NuGet is easy. Getting good at it takes time. Download our guide to learn the best practice of NuGet for the Enterprise.