Fix an sporadic failure in ServerCookieEncoderTest

In testEncodingSingleCookieV0():

Let's assume we encoded a cookie with MaxAge=50 when currentTimeMillis
is 10999.

Because the encoder will not encode the millisecond part for Expires,
the timeMillis value of the encoded Expires field will be 60000. (If we
did not dropped the millisecond part, it would be 60999.)

Encoding a cookie will take some time, so currentTimeMillis will
increase slightly, such as to 11001.

  diff = (60000 - 11001) / 1000 = 48999 / 1000 = 48
  maxAge - diff = 50 - 48 = 2

Due to losing millisecond part twice, we end up with the precision
problem illustrated above, and thus we should increase the tolerance
from 1 second to 2 seconds.

/cc @slandelle
This commit is contained in:
Trustin Lee 2015-02-02 16:08:29 +09:00
parent 200c6efc75
commit 3c6cbd40e2

View File

@ -46,8 +46,8 @@ public class ServerCookieEncoderTest {
assertTrue(matcher.find()); assertTrue(matcher.find());
Date expiresDate = HttpHeaderDateFormat.get().parse(matcher.group(1)); Date expiresDate = HttpHeaderDateFormat.get().parse(matcher.group(1));
long diff = (expiresDate.getTime() - System.currentTimeMillis()) / 1000; long diff = (expiresDate.getTime() - System.currentTimeMillis()) / 1000;
// 1 sec should be fine // 2 secs should be fine
assertTrue(Math.abs(diff - maxAge) <= 1); assertTrue(Math.abs(diff - maxAge) <= 2);
} }
@Test @Test