Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems testing w/ seconds at runtime

Status
Not open for further replies.

clemcrock

Programmer
Dec 17, 2006
63
0
0
US
Hello,

I constantly run into this problem when trying to test assertions
against time w/ seconds. Since there can be a tiny lapse time between
when a test is fired off and the value is tested your test can be off by
a second, therefor causing it to fail.

For instance. I have an assert select test that will randomly fail
because the dynamic time being tested against will have lapsed a second.

Code:
assert_select "pubDate", 6.days.from_now.gmtime.strftime("%a, %d %b %Y
%I:%M:%S GMT")

Now, about 50% of the time, this test will fail and give you a result
like such:

<"Sun, 11 Nov 2007 21:15:48 GMT"> expected but was
<"Sun, 11 Nov 2007 21:15:47 GMT">.

Any ideas on how I can make this test less brittle?

Thanks,
Eric

 
Maybe you could convert the returned time to seconds since the epoch (or some other standardised numeric value), then check that it was less than (now + 6 days + 5 seconds) and more than (now + 6 days - 5 seconds). This would add a bit of tolerance to your test...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top