aboutsummaryrefslogtreecommitdiffstats
path: root/storage/src/tests/storageframework/thread/taskthreadtest.cpp
blob: 32bc6e8a8fb4b238bf86b847b1f7bb2bcf766fd7 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/storageframework/generic/thread/taskthread.h>
#include <vespa/vespalib/gtest/gtest.h>

namespace storage::framework {

namespace {

struct Task {
    std::string _name;
    uint8_t _priority;

    Task(const std::string& name, uint8_t priority)
        : _name(name), _priority(priority) {}

    bool operator<(const Task& other) const {
        return (_priority > other._priority);
    }
    uint8_t getPriority() const { return _priority; }
};

struct MyThread : public TaskThread<Task> {
    MyThread(ThreadLock& lock) : TaskThread<Task>(lock) {}
    ThreadWaitInfo doNonCriticalTick(ThreadIndex) override {
        return ThreadWaitInfo::NO_MORE_CRITICAL_WORK_KNOWN;
    }
};

}

TEST(TaskThreadTest, test_normal_usage)
{
    TickingThreadPool::UP pool(TickingThreadPool::createDefault("testApp", 100ms));

    MyThread t(*pool);
    t.addTask(Task("a", 6));
    t.addTask(Task("b", 3));
    t.addTask(Task("c", 8));
    t.addTask(Task("d", 4));
    EXPECT_TRUE(t.empty()); // Still empty before critical tick has run
    dynamic_cast<TickingThread&>(t).doCriticalTick(0);
    ASSERT_TRUE(!t.empty());
    EXPECT_EQ(3, t.peek().getPriority());
    std::ostringstream ost;
    while (!t.empty()) {
        Task task(t.peek());
        ost << task._name << '(' << ((int) task.getPriority()) << ") ";
        t.pop();
    }
    EXPECT_EQ(std::string("b(3) d(4) a(6) c(8) "), ost.str());
}

}