In honor of the Leap Day of February 29th, let me tell a story about an unusual date problem I encountered while unit-testing.
One project I worked on had made a small effort at writing unit tests, using Java and jUnit. Unfortunately, the team culture was not to run the test suite frequently while developing. Rather, developers were sort of willing to run their own tests, but let the nightly job run the full suite.
When a test would fail, someone would track down and lean on whoever had broken it.
Once in a blue moon, a certain couple of Unit tests would fail. This is the story of why they were breaking, why it was hard to duplicate the issue, and how I fixed it once I found it.
These tests failed one night in the spring, and again one night in the summer.
But next morning, no one could reproduce the failure. So they were chalked up to disturbances in the Force or cosmic rays, one of "those things" that happen "sometimes" and nothing was done.
They failed again in late summer. Following that fail, I took it upon myself to solve the mystery. Let's take a peak at why these tests would fail.
Here is one of the occasionally-failing tests:
@Test
public void testGetAge_OneFutureMonth_ExpectedSingularString()
{
int monthsDiff = 1;
Calendar date = Calendar.getInstance();
DBRecord rec = makeEmptyDBRecord();
rec.setDateYear(date.get(Calendar.YEAR));
rec.setDateMonth(1 + date.get(Calendar.MONTH) + monthsDiff);
rec.setDateDay(date.get(Calendar.DAY_OF_MONTH));
assertEquals("" + monthsDiff + " month future", DateUtilities.getAge(rec));
}
Sure enough, as with previous times, it passed when run the next day. So let's code-review it and ask: What exactly is this test doing?
It is testing a getAge() method, which takes a DBRecord data structure object as input and returns a string that describes the difference between today's date and the date in the data structure.
The data structure stores the date with day, month and year separated and stored as integers. The month value is incremented by 1 because this application uses 1-based months and Calendar uses 0-based months.
Of course, this means that invalid dates are possible, e.g. January 99, or a negative year such as -2016.
Since it's a Date-related test, maybe the fact that yesterday was a different date than today is a hint?
The current date when I did this review was September 1st. That meant that the test failed during the nightly run on August 31st.
Thinking it through, it means that the test run on the 1st sets the DBRecord object to October 1st, one month in the future. But the test run the previous night, Aug 31st, set the future date to an invalid September 31st. The DBRecord structure permits that.
But the Calendar class does not.
Here's part of the static method that our test is testing:
public static String getAge(DBRecord record)
{
// skip some lines...
int year = record.getDateYear();
int month = record.getDateMonth();
int day = record.getDateDay();
Calendar today = Calendar.getInstance();
Calendar date = Calendar.getInstance();
if (year != 0)
date.set(Calendar.YEAR, year);
if (month != 0)
date.set(Calendar.MONTH, month - 1);
if (day != 0)
date.set(Calendar.DAY_OF_MONTH, day);
// skip some more lines...
When we get to the getAge() method's lines that set the year, month and day, Calendar normalizes the date from Sept 31st to Oct 1st.
So the call to getMonthsDifference() returns 2 months between August (the "current date" when the failed test ran) and October (the future date).
Since the test is only expecting a difference of 1 month, it fails.
Aha! The mystery of why it fails occasionally is solved!
Now how to fix it so that it runs correctly, regardless of the date the test runs? Lots of possible fixes come to mind.
For one, this would have been discovered much sooner had the team - myself included - followed the best-practice of running the entire test suite before committing our changes. Had we been doing that, this test would have failed on all of our work-stations all day on the 31st and not produced the overnight-fail mystery.
Another very good fix would be to introduce some kind of Date Provider or other class, something that will provide the current date in Production, but that our tests can manipulate to return a test-controlled date, rather than a calendar-controlled date.
I like that solution, but I did not choose that fix back in September. It's been a while but my thought process was probably that the bug was in the test code, not the production code, and that creating and introducing some form of Date Provider would require changes to the production code.
Instead, I tweaked the tests to fix the bug in them. I created a helper method to ensure a valid date in the DBRecord, one that does not allow the day of the month to be greater than 28.
But..
private DBRecord getDateOffsetBy(int monthsDiff, int yearsDiff)
{
Calendar date = Calendar.getInstance();
if (date.get(Calendar.DAY_OF_MONTH) > 28)
date.set(Calendar.DAY_OF_MONTH, 28);
date.set(Calendar.MONTH, date.get(Calendar.MONTH) + monthsDiff);
date.set(Calendar.YEAR, date.get(Calendar.YEAR) + yearsDiff);
DBRecord rec = makeEmptyDBRecord();
rec.setDateYear(date.get(Calendar.YEAR));
rec.setDateMonth(1 + date.get(Calendar.MONTH) + monthsDiff);
rec.setDateDay(date.get(Calendar.DAY_OF_MONTH));
return rec;
}
I then changed the failing tests to variations of:
@Test
public void testGetAge_OneFutureMonth_ExpectedSingularString()
{
int monthsDiff = 1;
DBRecord rec = getDateOffsetBy(monthsDiff, 0);
assertEquals("" + monthsDiff + " month future", DateUtilities.getAge(rec));
}
One project I worked on had made a small effort at writing unit tests, using Java and jUnit. Unfortunately, the team culture was not to run the test suite frequently while developing. Rather, developers were sort of willing to run their own tests, but let the nightly job run the full suite.
When a test would fail, someone would track down and lean on whoever had broken it.
Once in a blue moon, a certain couple of Unit tests would fail. This is the story of why they were breaking, why it was hard to duplicate the issue, and how I fixed it once I found it.
These tests failed one night in the spring, and again one night in the summer.
But next morning, no one could reproduce the failure. So they were chalked up to disturbances in the Force or cosmic rays, one of "those things" that happen "sometimes" and nothing was done.
They failed again in late summer. Following that fail, I took it upon myself to solve the mystery. Let's take a peak at why these tests would fail.
Here is one of the occasionally-failing tests:
@Test
public void testGetAge_OneFutureMonth_ExpectedSingularString()
{
int monthsDiff = 1;
Calendar date = Calendar.getInstance();
DBRecord rec = makeEmptyDBRecord();
rec.setDateYear(date.get(Calendar.YEAR));
rec.setDateMonth(1 + date.get(Calendar.MONTH) + monthsDiff);
rec.setDateDay(date.get(Calendar.DAY_OF_MONTH));
assertEquals("" + monthsDiff + " month future", DateUtilities.getAge(rec));
}
Sure enough, as with previous times, it passed when run the next day. So let's code-review it and ask: What exactly is this test doing?
It is testing a getAge() method, which takes a DBRecord data structure object as input and returns a string that describes the difference between today's date and the date in the data structure.
The data structure stores the date with day, month and year separated and stored as integers. The month value is incremented by 1 because this application uses 1-based months and Calendar uses 0-based months.
Of course, this means that invalid dates are possible, e.g. January 99, or a negative year such as -2016.
Since it's a Date-related test, maybe the fact that yesterday was a different date than today is a hint?
The current date when I did this review was September 1st. That meant that the test failed during the nightly run on August 31st.
Thinking it through, it means that the test run on the 1st sets the DBRecord object to October 1st, one month in the future. But the test run the previous night, Aug 31st, set the future date to an invalid September 31st. The DBRecord structure permits that.
But the Calendar class does not.
Here's part of the static method that our test is testing:
public static String getAge(DBRecord record)
{
// skip some lines...
int year = record.getDateYear();
int month = record.getDateMonth();
int day = record.getDateDay();
Calendar today = Calendar.getInstance();
Calendar date = Calendar.getInstance();
if (year != 0)
date.set(Calendar.YEAR, year);
if (month != 0)
date.set(Calendar.MONTH, month - 1);
if (day != 0)
date.set(Calendar.DAY_OF_MONTH, day);
// skip some more lines...
When we get to the getAge() method's lines that set the year, month and day, Calendar normalizes the date from Sept 31st to Oct 1st.
So the call to getMonthsDifference() returns 2 months between August (the "current date" when the failed test ran) and October (the future date).
Since the test is only expecting a difference of 1 month, it fails.
Aha! The mystery of why it fails occasionally is solved!
Now how to fix it so that it runs correctly, regardless of the date the test runs? Lots of possible fixes come to mind.
For one, this would have been discovered much sooner had the team - myself included - followed the best-practice of running the entire test suite before committing our changes. Had we been doing that, this test would have failed on all of our work-stations all day on the 31st and not produced the overnight-fail mystery.
Another very good fix would be to introduce some kind of Date Provider or other class, something that will provide the current date in Production, but that our tests can manipulate to return a test-controlled date, rather than a calendar-controlled date.
I like that solution, but I did not choose that fix back in September. It's been a while but my thought process was probably that the bug was in the test code, not the production code, and that creating and introducing some form of Date Provider would require changes to the production code.
Instead, I tweaked the tests to fix the bug in them. I created a helper method to ensure a valid date in the DBRecord, one that does not allow the day of the month to be greater than 28.
But..
private DBRecord getDateOffsetBy(int monthsDiff, int yearsDiff)
{
Calendar date = Calendar.getInstance();
if (date.get(Calendar.DAY_OF_MONTH) > 28)
date.set(Calendar.DAY_OF_MONTH, 28);
date.set(Calendar.MONTH, date.get(Calendar.MONTH) + monthsDiff);
date.set(Calendar.YEAR, date.get(Calendar.YEAR) + yearsDiff);
DBRecord rec = makeEmptyDBRecord();
rec.setDateYear(date.get(Calendar.YEAR));
rec.setDateMonth(1 + date.get(Calendar.MONTH) + monthsDiff);
rec.setDateDay(date.get(Calendar.DAY_OF_MONTH));
return rec;
}
I then changed the failing tests to variations of:
@Test
public void testGetAge_OneFutureMonth_ExpectedSingularString()
{
int monthsDiff = 1;
DBRecord rec = getDateOffsetBy(monthsDiff, 0);
assertEquals("" + monthsDiff + " month future", DateUtilities.getAge(rec));
}