285 points · by luu · 11y ago · 112 comments
Then I was coding on the train home one night and in the tube my hotspot lost connection. Then I happened to notice... with wifi off the tests ran 10x quicker. Pull out netstat.... I see http requests to akamai essentially as each test setUp() is run. It's SSL so I only see a hostname, not the full path, and they just look like this, but the hostname is dynamic: a23-34-132-181.deploy.static.akamaitechnologies.com
I spent 30 mins reviewing our code, grepping my code and packages directories, and stepping thru in a debugger. No luck.
The worst was having a Map keyed on URLs in Java -- turns out URL.equals() and URL.hashCode() do DNS lookups... Easiest 50% test speedup ever!
The other suggestions -- using a SSL proxy -- has been my plan of attack.
- start a test from the IDE,
- wait a second or so,
- manually pause all threads,
- look at the stacks of the threads.
Might take two or three attempts, but likely should learn you a lot. Alternatively, use a real profiler to find the call(s) that take lots of time. strace -e trace=network ./run_my_unit_testsUse java.net.Uri, or e.g. the equivalent from Jersey or whatever your local framework/HTTP client/... brings with it.
Joshua Bloch used this as an example in one of the brilliant Java Puzzler video series with the net advise of don't depend on non-local state for equality.
[0]: https://mitmproxy.org/, https://mitmproxy.org/doc/ssl.html
[0]: http://nedbatchelder.com/blog/201503/finding_temp_file_creat...
https://jimshaver.net/2015/02/11/decrypting-tls-browser-traf...
http://www.haiku-os.org/legacy-docs/benewsletter/Issue4-22.h...
A Testing Fairy Tale
Two test engineers were in a crunch. The floppy drive they were currently testing would work all day while they ran a variety of stress tests, but the exact same tests would run for only eight hours at night. After a few days of double-checking the hardware, the testing procedure, and the recording devices, they decided to stay the night and watch what happened. For eight hours they stared at the floppy drive and drank espresso. The long dark night slowly turned into day and the sun shone in the window. The angled sunlight triggered the write-protection mechanism, which caused a write failure. A new casing was designed and the problem was solved. Who knew?
The cleaner's vacuum cleaner would occasionally pull out the PS2 cord.
The cool bit, at least to me, is that the mnemonic "spring forward" still works. Likely, this is only cool to me because the whole thing is unusual.
The other odd bit is that some TZs sit on the "wrong" side of the international date line, and thus get interesting offsets such as +13 or +14h from UTC. I think there's some that are -13h too, so the total "length" of day (that is, how long you could remain on a calendar date) is >48h. Thus, at any given time, people in their local time are in as many as three different calendar dates.
There's also a few TZs with half-hour offsets. (e.g., they're something like 4.5 hours off UTC.) There's a 3-hour TZ jump at one point on China's border, I think, because all of China is on a single timezone, and the nation is quite wide, east-to-west.
We've not even gotten to leap seconds, or that 2100 won't be a leap year.
And governments regularly muck with this stuff. The set of timezones and their offsets and DST preferences are constantly changing.
To get anything done, I feel like the general programmer[1] is allowed to assume, at a bare minimum, a Proleptic Gregorian calendar. Anything else just makes handling early (pre-Gregorian) dates madness. (Various geographical locations adopted the calendar at different times, so you end up needing to know where you are to compute the "local" date. Even further back in history the rules get even more odd, and unknown. [2], if I read it correctly, includes a table of when scholars _think_ people inserted leap years.)
Once you go with "Proleptic Gregorian calendar", I think points such as
> Months have either 28, 29, 30, or 31 days.
…are actually always true. Of course, two bullets down is,
> There is only one calendar system in use at one time.
Which is essentially what we're using as axiom here. It's true because we've defined it as such. (Cheating, I admit, but if you're building the next great mobile app, do you care?)
I do wish that site included fold-out sections to de-mystify some of the points.
> Non leap years will never contain a leap day.
Your definition of leap year isn't "year with a leap day"?
> Unix time is the number of seconds since Jan 1st 1970.
> Unix time […] is […] defined as the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970, not counting leap seconds.[3]
Again I wish for pop-out explanations, because I'm left to wonder whether POSIX's lack of leap seconds is what the author is hinting at.
I'm bored though, so perhaps I should write a gist showing which of these I believe to hold (and why) and which do not (and why).
[1]: I think anyone breaking this rule would know that their area of interest requires them to break it, and thus know that they need attention.
[2]: https://en.wikipedia.org/wiki/Julian_calendar#Leap_year_erro...
http://en.wikipedia.org/wiki/Eucla,_Western_Australia#Time_z...
The standard says: "The implementation of ECMAScript should not try to determine whether the exact time was subject to daylight saving time, but just whether daylight saving time would have been in effect if the current daylight saving time algorithm had been used at the time. This avoids complications such as taking into account the years that the locale observed daylight saving time year round."
In Spidermonkey, at least, it mapped current years onto prior years where the current day falls on the same day of the week.
edit: Spidermonkey bug I filed a long time ago: https://bugzilla.mozilla.org/show_bug.cgi?id=1029923
On a side note, the title reminds me of a similarly interesting bug in OpenOffice.org: https://bugs.launchpad.net/ubuntu/+source/file/+bug/248619
Except for you know, when it's still light at 7.30pm.
I like light evenings in the summer and am prepared to accept a little complexity as a tradeoff.
And didn't Australia do some last minute DST nonsense like a Latam country, versus planning far ahead?
It's rather like redefining the metre so that everyone is 2 metres tall - you're adding a lot of complexity for not a lot of consistency.
It's not about going out, it's just nice still having a few hours of daylight in the early evenings after I've finished work.
Quite a few years ago I was working in WA on a system that frequently aggregated data from servers located in different states when they decided to try daylight savings. From memory it was decided by the WA state government that the state would adopt daylight savings on a trial basis, try it for a year or two, then consider if we wanted to keep it.
For programmers it was pretty much the worst possible solution. Have it or don't have it, don't force us to make sure everything supported having daylight savings in WA and also require the ability to possibly turn it off again in the relatively near future.
Getting annoyed just thinking about it...
Also remember at one point NZ was having a potential power shortage caused by a drought[1]. Some senior government officials were suggesting bringing forward the start of daylight saving by a month with around 2 weeks notice.[2]
[1] - NZ gets most of it's power from Hydro
[2] - The jury is still out on if daylight savings saves power, especially since different places have low/high aircon usage.
At best it's a wash. People all think that domestic lighting is a big power user, but in reality it's things like commercial shopping centres and office buildings that really suck down the juice. And typically those are run on a timer, so the pattern doesn't change (even if the timer is adjusted, the lights,ac and services are on for a fixed period of time).
Any savings for people having their lights off for 60 minutes less is swamped by people switching their AC on when they get home an hour earlier.
I still support DST as a quality of life thing, though.
WA Govt was very keen on us voting for it, and assumed that with a trial we would get used to it and it would pass. As it turns out, that time it was rejected by the biggest margin so far.
We eventually realized that the unittest will only fail after work hours, at which usually no one is around to start tests. It turns out the test will fail during 6PM - midnight because of an UTC issue.
Now that is the sort of thing that drives programmers nuts.
I have an app where users are running it while crossing time zones. :-(
Every time we switched to or from DST there were a couple of outliers that got caught. We'd update the tables to handle the case correctly next time, but would miss something else, if the change didn't break something else completely.
Why do you have unit tests hitting outside services? If you have developed service clients that need to be unit tested, you should have mock services for them to run against locally so that your unit tests aren't going down every time there's a remote service failure. They don't need to be full blown services, but they should receive the request and respond with the expected result the service would give when hit by the client.
If your clients are generated by the remote service [i.e. by WSDL], then you shouldn't theoretically need to test those as they're generated by the third party - so you should be unit testing against mock clients.
I'd argue that integration tests with real world 3rd party services should be handled by some reporting system on your build pipeline somewhere and your ops team should be investigating that failure, you shouldn't need to be running them locally on your dev machine.
Hard coded dates not taking into account timezone and daylight saving? Been there, done that. This week the time was AEST-DS (Daylight saving) and is now AEST (finished daylight saving) [0]
[0] For example: Melbourne AEST: Australian Eastern Standard Time == GMT +10Hrs
DS AEST-1 hour when daylight saving is declared. When Daylight saving finishes, DS is AEST+1 hour or AEST again.
Test was that at $1/day, now+1 month would be $31. Wrote the test on the 30th of March, so found out about it the next day.
If it had been written on the 1st of March, wouldn't have found out about it for a month.