aboutsummaryrefslogtreecommitdiffstats
path: root/slobrok/src/tests/backoff/testbackoff.cpp
blob: 23193cc2d9827e16b3b8ad660c498fb37b1c7b56 (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/vespalib/testkit/testapp.h>
#include <vespa/slobrok/backoff.h>

#include <vespa/log/log.h>
LOG_SETUP("backoff_test");

using slobrok::api::BackOff;

TEST_SETUP(Test);

//-----------------------------------------------------------------------------

int
Test::Main()
{
    TEST_INIT("backoff_test");

    BackOff one;
    EXPECT_FALSE(one.shouldWarn());
    EXPECT_EQUAL(0.500, one.get());
    EXPECT_FALSE(one.shouldWarn());
    EXPECT_EQUAL(1.000, one.get());
    EXPECT_FALSE(one.shouldWarn());
    EXPECT_EQUAL(1.500, one.get());
    EXPECT_TRUE(one.shouldWarn());
    for (int i = 4; i < 41; i++) {
        EXPECT_EQUAL(0.5 * i, one.get());
    }
    for (int i = 1; i < 1000; i++) {
        EXPECT_EQUAL(20.0, one.get());
    }
    TEST_FLUSH();

    BackOff two;
    EXPECT_FALSE(two.shouldWarn());
    for (int i = 1; i < 50; i++) {
        double expect = 0.5 * i;
        if (expect > 20.0) expect = 20.0;
        EXPECT_EQUAL(expect, two.get());
        if (i == 3 || i == 8 || i == 18) {
            EXPECT_TRUE(two.shouldWarn());
        } else {
            EXPECT_FALSE(two.shouldWarn());
        }
    }
    two.reset();
    EXPECT_FALSE(two.shouldWarn());
    for (int i = 1; i < 50; i++) {
        double expect = 0.5 * i;
        if (expect > 20.0) expect = 20.0;
        EXPECT_EQUAL(expect, two.get());
        if (i == 3 || i == 8 || i == 18) {
            EXPECT_TRUE(two.shouldWarn());
        } else {
            EXPECT_FALSE(two.shouldWarn());
        }
    }
    for (int i = 0; i < 50000; i++) {
        EXPECT_EQUAL(20.0, two.get());
        if ((i % 180) == 5) {
            EXPECT_TRUE(two.shouldWarn());
        } else {
            EXPECT_FALSE(two.shouldWarn());
        }
    }
    TEST_DONE();
}