by rdtsc · 10y ago · 0 comments
* Didn't think about time at all.
In most cases most don't think about time until it becomes a problem. It often ends up with a system which behaves in surprising ways, and is the equivalent to the developer believing any of those things in the article. But there is a difference in them believing those falsehoods, and simply not being a aware of them at all.
* Time moves forward
This is probably the biggest gotcha of all. Just imagine how many times int the past you did dt = now() - t0, then did something with dt like sleep(dt). Well time will move backwards sometimes and dt is negative. I bet this will make most people here worried and they'll think about code they wrote like that (sure did). I have seen systems freeze for example because they end up sleeping for a huge amount of time (negative number was cast to an unsigned huge number).
A small advice on how to improve errors related to time is to abstract it away from algorithms and code as much as possible. Instead of always calling gettimeofday() / time() / now() function to get the current time, in a piece of lower level code, try to pass it in as an argument. That will make code testable.
I have seen fun failing test cases. Something like encoding the current time the test was written and a dt to current time. The test was working for years then started failing eventually because of the hard coded time.
Any code that uses time based scheduling should use a virtual implementation of time/scheduler to allow testing - this way you can say "sleep 10 hours" and then "move virtual time by 10 hours" and actually verify that the timeout is hit without the tests actually taking 10 hours. It also lets you test out logical edge cases such as `<=`, `>=` vs `<`, `>` which is surprisingly tricky with timeouts.
Now all we need to do is get rid of the filesystem and all other databases...
It doesn't support this use case. Yet [2].
I've pitched this before (during a code review for something) and was laughed at by the team under some guise of YAGNI / developer gold plating. They honestly thought I was joking. Once I explained the issues mentioned in the article, they still thought it was a waste of time.
It takes just a few seconds to implement and makes the code so much easier to test / maintain.
Any class that needs to know the time has the following object injected in.
public interface IDateTime
{
DateTime UtcNow { get; }
DateTime Today { get; }
}
StructureMap is configured to pass in the following concrete implementation. public class DefaultDateTime : IDateTime
{
public DateTime UtcNow
{
get { return DateTime.UtcNow; }
}
public DateTime Today
{
get { return DateTime.Today; }
}
}
Obviously you can expand this to include local time, we haven't in order to prevent accidental use of Now when you mean UtcNow.All of our tests inherit from a base test that does various set up stuff (clear down the db in integration tests for example). One of the things it does is swap out DefaultDateTime with this...
public class TestDateTime : IDateTime
{
private DateTime _testDateTime;
public TestDateTime(DateTime testDateTime)
{
_testDateTime = testDateTime;
}
public void ChangeTimeTo(DateTime testDateTime)
{
_testDateTime = testDateTime;
}
public DateTime UtcNow
{
get { return _testDateTime.ToUniversalTime(); }
public DateTime Today
{
get { return _testDateTime.Date; }
}
}
Where _testDateTime is set to be sometime in August (always a good idea if you're developing in the UK!). This has the added advantage of making test runs deterministic (at least in this respect) even if you're not doing a time related test.[1] https://msdn.microsoft.com/en-us/library/system.concurrency....
Slightly off-topic pet-peeve of mine: never used unsigned, unless you have an extremely good reason for it. Saving one bit is not one of them. You never know when you will need to do arithmetic on your unsigned values. Time going backwards is a good example of this.
I agree. Smart library developers seem to agree, too. Example from Python docs:
https://docs.python.org/3/library/time.html#time.monotonic
The only (cough) problem is drilling this knowledge into developers' heads - and as you said, awareness that there even is a problem!
Can you please explain this one and hopefully link to an in-depth resource? I imagine you're talking about leap seconds, and/or the possibility that the system time is changed (whether manually or due to an ntpd update that corrects backwards). Are there other sources of this phenomenon?