SprdNils · GitHub

Hi there,

when comparing timestamps of a row with some given value within a specific tolerance sometimes it happens that the assertions fails although the current timestamp is within the tolerance. Problem is caused when the expected value is on the edge of a minute shift like 09:24:58 and the current value is after that value like 09:25:00. When comparing these two values with a tolerance of 10 seconds the corresponding method returns false.

Sample code to reproduce the problem:

public static void main(String[] args) throws IllegalAccessException, InstantiationException, InvocationTargetException {
        // current value: 2019-12-03 09:25:00.345
        Instant instant = Instant.parse("2019-12-03T09:25:00.34Z");
        Timestamp timestamp = Timestamp.from(instant);
        Value currentValue = createValue("blub", timestamp, LetterCase.COLUMN_DEFAULT);
        // expected: 2019-12-03T09:24:58.000000000
        Instant expectedInstant = Instant.parse("2019-12-03T09:24:58.00Z");
        DateTimeValue expectedDateTime = DateTimeValue.from(Timestamp.from(expectedInstant));
        boolean areClose = Values.areClose(currentValue, expectedDateTime, new TimeValue(0, 0, 10));
        System.out.println("Values are close: " + areClose);
    }
 private static Value createValue(String columnName, Object actualObject, LetterCase letterCase)
            throws IllegalAccessException, InvocationTargetException, InstantiationException {
        Class<Value> valueClass = Value.class;
        Constructor<?>[] constructors = valueClass.getDeclaredConstructors();
        Constructor<?> singleConstructor = constructors[0];
        singleConstructor.setAccessible(true);
        return (Value) singleConstructor.newInstance(columnName, actualObject, letterCase);
    }

Read the original on github.com ↗