summaryrefslogtreecommitdiff
path: root/src/mongo/scripting/deadline_monitor_test.cpp
blob: 71daefbce8f262fd98a9883d84afdf869231538e (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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
 *    Copyright (C) 2013 10gen Inc.
 *
 *    This program is free software: you can redistribute it and/or  modify
 *    it under the terms of the GNU Affero General Public License, version 3,
 *    as published by the Free Software Foundation.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Affero General Public License for more details.
 *
 *    You should have received a copy of the GNU Affero General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the GNU Affero General Public License in all respects
 *    for all of the code used other than as permitted herein. If you modify
 *    file(s) with this exception, you may extend this exception to your
 *    version of the file(s), but you are not obligated to do so. If you do not
 *    wish to do so, delete this exception statement from your version. If you
 *    delete this exception statement from all source files in the program,
 *    then also delete it in the license file.
 */

// DeadlineMonitor unit tests

#include "mongo/platform/basic.h"

#include "mongo/scripting/deadline_monitor.h"


#include "mongo/unittest/unittest.h"

namespace mongo {

using std::shared_ptr;
using std::vector;

class TaskGroup {
public:
    TaskGroup() : _c(), _killCount(0), _targetKillCount(0) {}
    void noteKill() {
        stdx::lock_guard<stdx::mutex> lk(_m);
        ++_killCount;
        if (_killCount >= _targetKillCount)
            _c.notify_one();
    }
    void waitForKillCount(uint64_t target) {
        stdx::unique_lock<stdx::mutex> lk(_m);
        _targetKillCount = target;
        while (_killCount < _targetKillCount)
            _c.wait(lk);
    }

private:
    stdx::mutex _m;
    stdx::condition_variable _c;
    uint64_t _killCount;
    uint64_t _targetKillCount;
};

class Task {
public:
    Task() : _group(NULL), _killed(0) {}
    explicit Task(TaskGroup* group) : _group(group), _killed(0) {}
    void kill() {
        _killed = curTimeMillis64();
        if (_group)
            _group->noteKill();
    }
    void interrupt() {}
    TaskGroup* _group;
    uint64_t _killed;
};

// single task expires before stopping the deadline
TEST(DeadlineMonitor, ExpireThenRemove) {
    DeadlineMonitor<Task> dm;
    TaskGroup group;
    Task task(&group);
    dm.startDeadline(&task, 10);
    group.waitForKillCount(1);
    ASSERT(task._killed);
    ASSERT(!dm.stopDeadline(&task));
}

// single task deadline stopped before the task expires
TEST(DeadlineMonitor, RemoveBeforeExpire) {
    DeadlineMonitor<Task> dm;
    Task task;
    dm.startDeadline(&task, 3600 * 1000);
    ASSERT(dm.stopDeadline(&task));
    ASSERT(!task._killed);
}

// multiple tasks complete before deadline expires (with 10ms window)
TEST(DeadlineMonitor, MultipleTasksCompleteBeforeExpire) {
    DeadlineMonitor<Task> dm;
    vector<shared_ptr<Task>> tasks;

    // start 100 tasks with varying deadlines (1-100 hours)
    for (int i = 1; i <= 100; i++) {
        shared_ptr<Task> task(new Task());
        dm.startDeadline(task.get(), i * 3600 * 1000);
        tasks.push_back(task);
    }

    // verify each deadline is stopped arrival
    for (vector<shared_ptr<Task>>::iterator i = tasks.begin(); i != tasks.end(); ++i) {
        ASSERT(dm.stopDeadline(i->get()));
        ASSERT(!(*i)->_killed);
    }
}

// multiple tasks expire before stopping the deadline
TEST(DeadlineMonitor, MultipleTasksExpire) {
    DeadlineMonitor<Task> dm;
    TaskGroup group;
    vector<shared_ptr<Task>> tasks;

    // start 100 tasks with varying deadlines
    for (int i = 1; i <= 100; i++) {
        shared_ptr<Task> task(new Task(&group));
        dm.startDeadline(task.get(), i);
        tasks.push_back(task);
    }

    group.waitForKillCount(100);

    // verify each deadline has expired
    for (vector<shared_ptr<Task>>::iterator i = tasks.begin(); i != tasks.end(); ++i) {
        ASSERT(!dm.stopDeadline(i->get()));
        ASSERT((*i)->_killed);
    }
}

// mixed expiration and completion
TEST(DeadlineMonitor, MultipleTasksExpireOrComplete) {
    DeadlineMonitor<Task> dm;
    TaskGroup group;
    vector<shared_ptr<Task>> expiredTasks;  // tasks that should expire
    vector<shared_ptr<Task>> stoppedTasks;  // tasks that should not expire

    // start 100 tasks with varying deadlines
    for (int i = 1; i <= 100; i++) {
        shared_ptr<Task> task(new Task(&group));
        if (i % 2 == 0) {
            // stop every other task
            dm.startDeadline(task.get(), i * 3600 * 1000);
            dm.stopDeadline(task.get());
            stoppedTasks.push_back(task);
            continue;
        }
        dm.startDeadline(task.get(), i);
        expiredTasks.push_back(task);
    }

    group.waitForKillCount(50);

    // check tasks which exceed the deadline
    for (vector<shared_ptr<Task>>::iterator i = expiredTasks.begin(); i != expiredTasks.end();
         ++i) {
        ASSERT(!dm.stopDeadline(i->get()));
        ASSERT((*i)->_killed);
    }

    // check tasks with a deadline that was stopped
    for (vector<shared_ptr<Task>>::iterator i = stoppedTasks.begin(); i != stoppedTasks.end();
         ++i) {
        ASSERT(!(*i)->_killed);
    }
}

}  // namespace mongo