aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/vespa/model/utils/DurationTest.java
blob: 8ec0624980a5e6ff8863c6cd17d8f8e056a9350f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.utils;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

public class DurationTest {
    @Test
    void testDurationUnits() {
        assertEquals(1000, new Duration("1").getMilliSeconds());
        assertEquals(2.0, new Duration("2").getSeconds(), 0.0001);
        assertEquals(1, new Duration("1ms").getMilliSeconds());
        assertEquals(2000, new Duration("2s").getMilliSeconds());
        assertEquals(5 * 60 * 1000, new Duration("5m").getMilliSeconds());
        assertEquals(3 * 60 * 60 * 1000, new Duration("3h").getMilliSeconds());
        assertEquals(24 * 60 * 60 * 1000, new Duration("1d").getMilliSeconds());

        assertEquals(1400, new Duration("1.4s").getMilliSeconds());
        assertEquals(1400, new Duration("1.4 s").getMilliSeconds());
    }

    private void assertException(String str) {
        try {
            new Duration(str);
            fail("Exception not thrown for string: " + str);
        } catch (Exception e) {
        }
    }

    @Test
    void testParseError() {
        assertException("bjarne");
        assertException("");
        assertException("1 foo");
        assertException("1.5 bar");
        assertException("-5");
    }
}