aboutsummaryrefslogtreecommitdiffstats
path: root/searchcore/src/tests/proton/initializer/task_runner_test.cpp
blob: 064c481727ae2d7e57bd2b07fb847ee7be48d00e (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/log/log.h>
LOG_SETUP("task_runner_test");
#include <vespa/vespalib/testkit/testapp.h>
#include <vespa/searchcore/proton/initializer/initializer_task.h>
#include <vespa/searchcore/proton/initializer/task_runner.h>
#include <vespa/vespalib/util/threadstackexecutor.h>
#include <vespa/vespalib/stllike/string.h>
#include <mutex>

using proton::initializer::InitializerTask;
using proton::initializer::TaskRunner;

struct TestLog
{
    std::mutex _lock;
    vespalib::string _log;
    using UP = std::unique_ptr<TestLog>;

    TestLog()
        : _lock(),
          _log()
    {
    }

    void append(vespalib::string str) {
        std::lock_guard<std::mutex> guard(_lock);
        _log += str;
    }

    vespalib::string result() const { return _log; }
};

class NamedTask : public InitializerTask
{
protected:
    vespalib::string  _name;
    TestLog          &_log;
public:
    NamedTask(const vespalib::string &name, TestLog &log)
        : _name(name),
          _log(log)
    {
    }

    virtual void run() override { _log.append(_name); }
};


struct TestJob {
    TestLog::UP _log;
    InitializerTask::SP _root;

    TestJob(TestLog::UP log, InitializerTask::SP root);
    TestJob(TestJob &&) = default;
    ~TestJob();

    static TestJob setupCDependsOnAandB()
    {
        TestLog::UP log = std::make_unique<TestLog>();
        InitializerTask::SP A(std::make_shared<NamedTask>("A", *log));
        InitializerTask::SP B(std::make_shared<NamedTask>("B", *log));
        InitializerTask::SP C(std::make_shared<NamedTask>("C", *log));
        C->addDependency(A);
        C->addDependency(B);
        return TestJob(std::move(log), std::move(C));
    }

    static TestJob setupDiamond()
    {
        TestLog::UP log = std::make_unique<TestLog>();
        InitializerTask::SP A(std::make_shared<NamedTask>("A", *log));
        InitializerTask::SP B(std::make_shared<NamedTask>("B", *log));
        InitializerTask::SP C(std::make_shared<NamedTask>("C", *log));
        InitializerTask::SP D(std::make_shared<NamedTask>("D", *log));
        C->addDependency(A);
        C->addDependency(B);
        A->addDependency(D);
        B->addDependency(D);
        return TestJob(std::move(log), std::move(C));
    }
};

TestJob::TestJob(TestLog::UP log, InitializerTask::SP root)
    : _log(std::move(log)),
      _root(std::move(root))
{ }
TestJob::~TestJob() {}


struct Fixture
{
    vespalib::ThreadStackExecutor _executor;
    TaskRunner _taskRunner;

    Fixture(uint32_t numThreads = 1)
        : _executor(numThreads, 128 * 1024),
          _taskRunner(_executor)
    {
    }

    void run(const InitializerTask::SP &task) { _taskRunner.runTask(task); }
};


TEST_F("1 thread, 2 dependees, 1 depender", Fixture(1))
{
    TestJob job = TestJob::setupCDependsOnAandB();
    f.run(job._root);
    EXPECT_EQUAL("ABC", job._log->result());
}

TEST_F("1 thread, dag graph", Fixture(1))
{
    for (int iter = 0; iter < 1000; ++iter) {
        TestJob job = TestJob::setupDiamond();
        f.run(job._root);
        EXPECT_EQUAL("DABC", job._log->result());
    }
}

TEST_F("multiple threads, dag graph", Fixture(10))
{
    int dabc_count = 0;
    int dbac_count = 0;
    for (int iter = 0; iter < 1000; ++iter) {
        TestJob job = TestJob::setupDiamond();
        f.run(job._root);
        vespalib::string result = job._log->result();
        EXPECT_TRUE("DABC" == result || "DBAC" == result);
        if ("DABC" == result) {
            ++dabc_count;
        }
        if ("DBAC" == result) {
            ++dbac_count;
        }
    }
    LOG(info, "dabc=%d, dbac=%d", dabc_count, dbac_count);
}

TEST_MAIN()
{
    TEST_RUN_ALL();
}