aboutsummaryrefslogtreecommitdiffstats
path: root/searchcore/src/tests/proton/reprocessing/reprocessing_runner/reprocessing_runner_test.cpp
blob: c3acc99a3b24fc1d379bb4b9a378242024149337 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/log/log.h>
LOG_SETUP("reprocessing_runner_test");

#include <vespa/searchcore/proton/reprocessing/i_reprocessing_task.h>
#include <vespa/searchcore/proton/reprocessing/reprocessingrunner.h>
#include <vespa/vespalib/testkit/testapp.h>

using namespace proton;

struct Fixture
{
    ReprocessingRunner _runner;
    Fixture()
        : _runner()
    {
    }
};

using TaskList = ReprocessingRunner::ReprocessingTasks;

struct MyTask : public IReprocessingTask
{
    ReprocessingRunner &_runner;
    double _initProgress;
    double _middleProgress;
    double _finalProgress;
    double _myProgress;
    double _weight;

    MyTask(ReprocessingRunner &runner,
           double initProgress,
           double middleProgress,
           double finalProgress,
           double weight) noexcept
        : _runner(runner),
          _initProgress(initProgress),
          _middleProgress(middleProgress),
          _finalProgress(finalProgress),
          _myProgress(0.0),
          _weight(weight)
    {
    }

    void run() override {
        ASSERT_EQUAL(_initProgress, _runner.getProgress());
        _myProgress = 0.5;
        ASSERT_EQUAL(_middleProgress, _runner.getProgress());
        _myProgress = 1.0;
        ASSERT_EQUAL(_finalProgress, _runner.getProgress());
    }

    Progress getProgress() const override {
        return Progress(_myProgress, _weight);
    }

    static std::shared_ptr<MyTask>
    create(ReprocessingRunner &runner,
           double initProgress,
           double middleProgress,
           double finalProgress,
           double weight)
    {
        return std::make_shared<MyTask>(runner, initProgress, middleProgress, finalProgress, weight);
    }
};

TEST_F("require that progress is calculated when tasks are executed", Fixture)
{
    TaskList tasks;
    EXPECT_EQUAL(0.0, f._runner.getProgress());
    tasks.push_back(MyTask::create(f._runner, 0.0, 0.1, 0.2, 1.0));
    tasks.push_back(MyTask::create(f._runner, 0.2, 0.6, 1.0, 4.0));
    f._runner.addTasks(tasks);
    tasks.clear();
    EXPECT_EQUAL(0.0, f._runner.getProgress());
    f._runner.run();
    EXPECT_EQUAL(1.0, f._runner.getProgress());
}


TEST_F("require that runner can be reset", Fixture)
{
    TaskList tasks;
    EXPECT_EQUAL(0.0, f._runner.getProgress());
    tasks.push_back(MyTask::create(f._runner, 0.0, 0.5, 1.0, 1.0));
    f._runner.addTasks(tasks);
    tasks.clear();
    EXPECT_EQUAL(0.0, f._runner.getProgress());
    f._runner.run();
    EXPECT_EQUAL(1.0, f._runner.getProgress());
    f._runner.reset();
    EXPECT_EQUAL(0.0, f._runner.getProgress());
    tasks.push_back(MyTask::create(f._runner, 0.0, 0.5, 1.0, 1.0));
    f._runner.addTasks(tasks);
    tasks.clear();
    EXPECT_EQUAL(0.0, f._runner.getProgress());
    f._runner.reset();
    EXPECT_EQUAL(0.0, f._runner.getProgress());
    tasks.push_back(MyTask::create(f._runner, 0.0, 0.5, 1.0, 4.0));
    f._runner.addTasks(tasks);
    tasks.clear();
    EXPECT_EQUAL(0.0, f._runner.getProgress());
    f._runner.run();
    EXPECT_EQUAL(1.0, f._runner.getProgress());
}


TEST_MAIN()
{
    TEST_RUN_ALL();
}