aboutsummaryrefslogtreecommitdiffstats
path: root/staging_vespalib/src/tests/timer/timer_test.cpp
blob: 309ee873b44df46cbc89e511646b2d7e6d84e31a (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/vespalib/testkit/testapp.h>
#include <vespa/vespalib/util/timer.h>
#include <vespa/vespalib/util/executor.h>

using namespace vespalib;
using vespalib::Executor;
typedef Executor::Task Task;

class Test : public TestApp
{
public:
    int Main() override;
    void testScheduling();
    void testReset();
};

class TestTask : public Task {
private:
    vespalib::CountDownLatch &_latch;
public:
    TestTask(vespalib::CountDownLatch & latch) : _latch(latch) { }
    void run() override { _latch.countDown(); }
};

int
Test::Main()
{
    TEST_INIT("timer_test");
    testScheduling();
    testReset();
    TEST_DONE();
}

void Test::testScheduling()
{
    vespalib::CountDownLatch latch1(3);
    vespalib::CountDownLatch latch2(2);
    Timer timer;
    timer.scheduleAtFixedRate(Task::UP(new TestTask(latch1)), 0.1, 0.2);
    timer.scheduleAtFixedRate(Task::UP(new TestTask(latch2)), 0.5, 0.5);
    EXPECT_TRUE(latch1.await(60000));
    EXPECT_TRUE(latch2.await(60000));
}

void Test::testReset()
{
    vespalib::CountDownLatch latch1(2);
    Timer timer;
    timer.scheduleAtFixedRate(Task::UP(new TestTask(latch1)), 2.0, 3.0);
    timer.reset();
    EXPECT_TRUE(!latch1.await(3000));
    timer.scheduleAtFixedRate(Task::UP(new TestTask(latch1)), 0.2, 0.3);
    EXPECT_TRUE(latch1.await(60000));
}

TEST_APPHOOK(Test)