summaryrefslogtreecommitdiff
path: root/tests/test-Tasks.cc
blob: 9184f975c6d47dd43f59a11b505c920dd789ae9d (plain)
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
#include <Main.h>
#include <Task.h>
#include <TaskMan.h>
#include <StacklessTask.h>

using namespace Balau;

class CustomPrinter : public Printer {
};

static CustomPrinter * customPrinter = NULL;

class TestTask : public Task {
  public:
    virtual const char * getName() const { return "TestTask"; }
  private:
    virtual void Do() {
        customPrinter->setLocal();
        Printer::enable(M_ALL);
        Printer::log(M_DEBUG, "In TestTask::Do()");
    }
};

class TestOperation {
  public:
      TestOperation() : m_count(0), m_completed(false) { }
    void Do() {
        if (m_count++ == 0) {
            m_timeout.set(0.2);
            Task::operationYield(&m_timeout, Task::STACKLESS);
        }
        TAssert(m_timeout.gotSignal());
        m_completed = true;
    }
    bool completed() { return m_completed; }
  private:
    int m_count;
    bool m_completed;
    Events::Timeout m_timeout;
};

class TestStackless : public StacklessTask {
  public:
    virtual const char * getName() const { return "TestStackless"; }
  private:
    virtual void Do() {
        ssize_t r;
        StacklessBegin();
        m_operation = new TestOperation();
        StacklessOperation(m_operation->Do());
        TAssert(m_operation->completed());
        delete m_operation;
        StacklessEnd();
    }
    TestOperation * m_operation;
};

static void yieldingFunction() {
    Events::Timeout timeout(0.2);
    Task::operationYield(&timeout);
    TAssert(timeout.gotSignal());
}

void MainTask::Do() {
    customPrinter = new CustomPrinter();
    Printer::log(M_STATUS, "Test::Tasks running.");

    Events::TaskEvent taskEvt;
    Task * testTask = TaskMan::registerTask(new TestTask(), &taskEvt);
    waitFor(&taskEvt);
    TAssert(!taskEvt.gotSignal());
    yield();
    TAssert(taskEvt.gotSignal());
    taskEvt.ack();

    Task * testStackless = TaskMan::registerTask(new TestStackless(), &taskEvt);
    waitFor(&taskEvt);
    TAssert(!taskEvt.gotSignal());
    yield();
    TAssert(taskEvt.gotSignal());
    taskEvt.ack();

    Events::Timeout timeout(0.1);
    waitFor(&timeout);
    TAssert(!timeout.gotSignal());
    yield();
    TAssert(timeout.gotSignal());

    timeout.set(0.1);
    timeout.reset();
    waitFor(&timeout);
    yieldingFunction();
    TAssert(timeout.gotSignal());

    Printer::log(M_STATUS, "Test::Tasks passed.");
    Printer::log(M_DEBUG, "You shouldn't see that message.");
}