|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var test = require('tape'); |
| 4 | +var HistoricalScheduler = require('../scheduler/historicalscheduler'); |
| 5 | +var reactiveAssert = require('../testing/reactiveassert'); |
| 6 | + |
| 7 | +function time(days) { |
| 8 | + var d = new Date(1979,10,31,4,30,15); |
| 9 | + d.setUTCDate(d.getDate() + days); |
| 10 | + return d.getTime(); |
| 11 | +} |
| 12 | + |
| 13 | +function fromDays(days) { |
| 14 | + return 86400000 * days; |
| 15 | +} |
| 16 | + |
| 17 | +function Timestamped (value, timestamp) { |
| 18 | + this.value = value; |
| 19 | + this.timestamp = timestamp; |
| 20 | +} |
| 21 | + |
| 22 | +Timestamped.prototype.equals = function (other) { |
| 23 | + if (other == null) { return false; } |
| 24 | + return other.value === this.value && other.timestamp === this.timestamp; |
| 25 | +}; |
| 26 | + |
| 27 | +test('HistoricalScheduler constructor', function (t) { |
| 28 | + var s = new HistoricalScheduler(); |
| 29 | + |
| 30 | + t.equal(0, s.clock); |
| 31 | + t.equal(false, s.isEnabled); |
| 32 | + |
| 33 | + t.end(); |
| 34 | +}); |
| 35 | + |
| 36 | +test('HistoricalScheduler start and stop', function (t) { |
| 37 | + var s = new HistoricalScheduler(); |
| 38 | + |
| 39 | + var list = []; |
| 40 | + |
| 41 | + s.scheduleAbsolute(null, time(0), function () { list.push(new Timestamped(1, s.now())); }); |
| 42 | + s.scheduleAbsolute(null, time(1), function () { list.push(new Timestamped(2, s.now())); }); |
| 43 | + s.scheduleAbsolute(null, time(2), function () { s.stop(); }); |
| 44 | + s.scheduleAbsolute(null, time(3), function () { list.push(new Timestamped(3, s.now())); }); |
| 45 | + s.scheduleAbsolute(null, time(4), function () { s.stop(); }); |
| 46 | + s.scheduleAbsolute(null, time(5), function () { s.start(); }); |
| 47 | + s.scheduleAbsolute(null, time(6), function () { list.push(new Timestamped(4, s.now())); }); |
| 48 | + |
| 49 | + s.start(); |
| 50 | + |
| 51 | + t.equal(time(2), s.now()); |
| 52 | + t.equal(time(2), s.clock); |
| 53 | + |
| 54 | + s.start(); |
| 55 | + |
| 56 | + t.equal(time(4), s.now()); |
| 57 | + t.equal(time(4), s.clock); |
| 58 | + |
| 59 | + s.start(); |
| 60 | + |
| 61 | + t.equal(time(6), s.now()); |
| 62 | + t.equal(time(6), s.clock); |
| 63 | + |
| 64 | + s.start(); |
| 65 | + |
| 66 | + t.equal(time(6), s.now()); |
| 67 | + t.equal(time(6), s.clock); |
| 68 | + |
| 69 | + reactiveAssert(t, list, [ |
| 70 | + new Timestamped(1, time(0)), |
| 71 | + new Timestamped(2, time(1)), |
| 72 | + new Timestamped(3, time(3)), |
| 73 | + new Timestamped(4, time(6)) |
| 74 | + ]); |
| 75 | + |
| 76 | + t.end(); |
| 77 | +}); |
| 78 | + |
| 79 | +test('HistoricalScheduler order', function (t) { |
| 80 | + var s = new HistoricalScheduler(); |
| 81 | + |
| 82 | + var list = []; |
| 83 | + |
| 84 | + s.scheduleAbsolute(null, time(2), function () { list.push(new Timestamped(2, s.now())); }); |
| 85 | + |
| 86 | + s.scheduleAbsolute(null, time(3), function () { list.push(new Timestamped(3, s.now())); }); |
| 87 | + |
| 88 | + s.scheduleAbsolute(null, time(1), function () { list.push(new Timestamped(0, s.now())); }); |
| 89 | + s.scheduleAbsolute(null, time(1), function () { list.push(new Timestamped(1, s.now())); }); |
| 90 | + |
| 91 | + s.start(); |
| 92 | + |
| 93 | + reactiveAssert(t, list, [ |
| 94 | + new Timestamped(0, time(1)), |
| 95 | + new Timestamped(1, time(1)), |
| 96 | + new Timestamped(2, time(2)), |
| 97 | + new Timestamped(3, time(3)) |
| 98 | + ]); |
| 99 | + |
| 100 | + t.end(); |
| 101 | +}); |
| 102 | + |
| 103 | +test('HistoricalScheduler cancellation', function (t) { |
| 104 | + var s = new HistoricalScheduler(); |
| 105 | + |
| 106 | + var list = []; |
| 107 | + |
| 108 | + var d = s.scheduleAbsolute(null, time(2), function () { list.push(new Timestamped(2, s.now())); }); |
| 109 | + |
| 110 | + s.scheduleAbsolute(null, time(1), function () { |
| 111 | + list.push(new Timestamped(0, s.now())); |
| 112 | + d.dispose(); |
| 113 | + }); |
| 114 | + |
| 115 | + s.start(); |
| 116 | + |
| 117 | + reactiveAssert(t, list, [ |
| 118 | + new Timestamped(0, time(1)) |
| 119 | + ]); |
| 120 | + |
| 121 | + t.end(); |
| 122 | +}); |
| 123 | + |
| 124 | +test('HistoricalScheduler advance to', function (t) { |
| 125 | + var s = new HistoricalScheduler(); |
| 126 | + |
| 127 | + var list = []; |
| 128 | + |
| 129 | + s.scheduleAbsolute(null, time(0), function () { list.push(new Timestamped(0, s.now())); }); |
| 130 | + s.scheduleAbsolute(null, time(1), function () { list.push(new Timestamped(1, s.now())); }); |
| 131 | + s.scheduleAbsolute(null, time(2), function () { list.push(new Timestamped(2, s.now())); }); |
| 132 | + s.scheduleAbsolute(null, time(10), function () { list.push(new Timestamped(10, s.now())); }); |
| 133 | + s.scheduleAbsolute(null, time(11), function () { list.push(new Timestamped(11, s.now())); }); |
| 134 | + |
| 135 | + s.advanceTo(time(8)); |
| 136 | + |
| 137 | + t.equal(time(8), s.now()); |
| 138 | + t.equal(time(8), s.clock); |
| 139 | + |
| 140 | + reactiveAssert(t, list, [ |
| 141 | + new Timestamped(0, time(0)), |
| 142 | + new Timestamped(1, time(1)), |
| 143 | + new Timestamped(2, time(2)) |
| 144 | + ]); |
| 145 | + |
| 146 | + s.advanceTo(time(8)); |
| 147 | + |
| 148 | + t.equal(time(8), s.now()); |
| 149 | + t.equal(time(8), s.clock); |
| 150 | + |
| 151 | + reactiveAssert(t, list, [ |
| 152 | + new Timestamped(0, time(0)), |
| 153 | + new Timestamped(1, time(1)), |
| 154 | + new Timestamped(2, time(2)) |
| 155 | + ]); |
| 156 | + |
| 157 | + s.scheduleAbsolute(null, time(7), function () { list.push(new Timestamped(7, s.now())); }); |
| 158 | + s.scheduleAbsolute(null, time(8), function () { list.push(new Timestamped(8, s.now())); }); |
| 159 | + |
| 160 | + t.equal(time(8), s.now()); |
| 161 | + t.equal(time(8), s.clock); |
| 162 | + |
| 163 | + reactiveAssert(t, list, [ |
| 164 | + new Timestamped(0, time(0)), |
| 165 | + new Timestamped(1, time(1)), |
| 166 | + new Timestamped(2, time(2)) |
| 167 | + ]); |
| 168 | + |
| 169 | + s.advanceTo(time(10)); |
| 170 | + |
| 171 | + t.equal(time(10), s.now()); |
| 172 | + t.equal(time(10), s.clock); |
| 173 | + |
| 174 | + reactiveAssert(t, list, [ |
| 175 | + new Timestamped(0, time(0)), |
| 176 | + new Timestamped(1, time(1)), |
| 177 | + new Timestamped(2, time(2)), |
| 178 | + new Timestamped(7, time(8)), |
| 179 | + new Timestamped(8, time(8)), |
| 180 | + new Timestamped(10, time(10)) |
| 181 | + ]); |
| 182 | + |
| 183 | + s.advanceTo(time(100)); |
| 184 | + |
| 185 | + t.equal(time(100), s.now()); |
| 186 | + t.equal(time(100), s.clock); |
| 187 | + |
| 188 | + reactiveAssert(t, list, [ |
| 189 | + new Timestamped(0, time(0)), |
| 190 | + new Timestamped(1, time(1)), |
| 191 | + new Timestamped(2, time(2)), |
| 192 | + new Timestamped(7, time(8)), |
| 193 | + new Timestamped(8, time(8)), |
| 194 | + new Timestamped(10, time(10)), |
| 195 | + new Timestamped(11, time(11)) |
| 196 | + ]); |
| 197 | + |
| 198 | + t.end(); |
| 199 | +}); |
| 200 | + |
| 201 | +test('HistoricalScheduler advance by', function (t) { |
| 202 | + var s = new HistoricalScheduler(); |
| 203 | + |
| 204 | + var list = []; |
| 205 | + |
| 206 | + s.scheduleAbsolute(null, time(0), function () { list.push(new Timestamped(0, s.now())); }); |
| 207 | + s.scheduleAbsolute(null, time(1), function () { list.push(new Timestamped(1, s.now())); }); |
| 208 | + s.scheduleAbsolute(null, time(2), function () { list.push(new Timestamped(2, s.now())); }); |
| 209 | + s.scheduleAbsolute(null, time(10), function () { list.push(new Timestamped(10, s.now())); }); |
| 210 | + s.scheduleAbsolute(null, time(11), function () { list.push(new Timestamped(11, s.now())); }); |
| 211 | + |
| 212 | + s.advanceBy(time(8) - s.now()); |
| 213 | + |
| 214 | + t.equal(time(8), s.now()); |
| 215 | + t.equal(time(8), s.clock); |
| 216 | + |
| 217 | + reactiveAssert(t, list, [ |
| 218 | + new Timestamped(0, time(0)), |
| 219 | + new Timestamped(1, time(1)), |
| 220 | + new Timestamped(2, time(2)) |
| 221 | + ]); |
| 222 | + |
| 223 | + s.scheduleAbsolute(null, time(7), function () { list.push(new Timestamped(7, s.now())); }); |
| 224 | + s.scheduleAbsolute(null, time(8), function () { list.push(new Timestamped(8, s.now())); }); |
| 225 | + |
| 226 | + t.equal(time(8), s.now()); |
| 227 | + t.equal(time(8), s.clock); |
| 228 | + |
| 229 | + reactiveAssert(t, list, [ |
| 230 | + new Timestamped(0, time(0)), |
| 231 | + new Timestamped(1, time(1)), |
| 232 | + new Timestamped(2, time(2)) |
| 233 | + ]); |
| 234 | + |
| 235 | + s.advanceBy(0); |
| 236 | + |
| 237 | + t.equal(time(8), s.now()); |
| 238 | + t.equal(time(8), s.clock); |
| 239 | + |
| 240 | + reactiveAssert(t, list, [ |
| 241 | + new Timestamped(0, time(0)), |
| 242 | + new Timestamped(1, time(1)), |
| 243 | + new Timestamped(2, time(2)) |
| 244 | + ]); |
| 245 | + |
| 246 | + s.advanceBy(fromDays(2)); |
| 247 | + |
| 248 | + t.equal(time(10), s.now()); |
| 249 | + t.equal(time(10), s.clock); |
| 250 | + |
| 251 | + reactiveAssert(t, list, [ |
| 252 | + new Timestamped(0, time(0)), |
| 253 | + new Timestamped(1, time(1)), |
| 254 | + new Timestamped(2, time(2)), |
| 255 | + new Timestamped(7, time(8)), |
| 256 | + new Timestamped(8, time(8)), |
| 257 | + new Timestamped(10, time(10)) |
| 258 | + ]); |
| 259 | + |
| 260 | + s.advanceBy(fromDays(90)); |
| 261 | + |
| 262 | + t.equal(time(100), s.now()); |
| 263 | + t.equal(time(100), s.clock); |
| 264 | + |
| 265 | + reactiveAssert(t, list, [ |
| 266 | + new Timestamped(0, time(0)), |
| 267 | + new Timestamped(1, time(1)), |
| 268 | + new Timestamped(2, time(2)), |
| 269 | + new Timestamped(7, time(8)), |
| 270 | + new Timestamped(8, time(8)), |
| 271 | + new Timestamped(10, time(10)), |
| 272 | + new Timestamped(11, time(11)) |
| 273 | + ]); |
| 274 | + |
| 275 | + t.end(); |
| 276 | +}); |
| 277 | + |
| 278 | +test('HistoricalScheduler is enabled', function (t) { |
| 279 | + var s = new HistoricalScheduler(); |
| 280 | + |
| 281 | + t.equal(false, s.isEnabled); |
| 282 | + |
| 283 | + s.schedule(s, function (s) { |
| 284 | + t.equal(true, s.isEnabled); |
| 285 | + s.stop(); |
| 286 | + t.equal(false, s.isEnabled); |
| 287 | + }); |
| 288 | + |
| 289 | + t.equal(false, s.isEnabled); |
| 290 | + |
| 291 | + s.start(); |
| 292 | + |
| 293 | + t.equal(false, s.isEnabled); |
| 294 | + |
| 295 | + t.end(); |
| 296 | +}); |
| 297 | + |
| 298 | +test('HistoricalScheduler Sleep 1', function (t) { |
| 299 | + var now = new Date(1983, 2, 11, 12, 0, 0).getTime(); |
| 300 | + |
| 301 | + var s = new HistoricalScheduler(now); |
| 302 | + |
| 303 | + s.sleep(fromDays(1)); |
| 304 | + |
| 305 | + t.equal(now + fromDays(1), s.clock); |
| 306 | + |
| 307 | + t.end(); |
| 308 | +}); |
| 309 | + |
| 310 | +test('HistoricalScheduler sleep 2', function (t) { |
| 311 | + var s = new HistoricalScheduler(); |
| 312 | + |
| 313 | + var n = 0; |
| 314 | + |
| 315 | + s.scheduleRecursiveFuture(null, new Date(s.now() + 6000), function (_, rec) { |
| 316 | + s.sleep(3 * 6000); |
| 317 | + n++; |
| 318 | + |
| 319 | + rec(null, new Date(s.now() + 6000)); |
| 320 | + }); |
| 321 | + |
| 322 | + s.advanceTo(s.now() + (5 * 6000)); |
| 323 | + |
| 324 | + t.equal(2, n); |
| 325 | + |
| 326 | + t.end(); |
| 327 | +}); |
| 328 | + |
| 329 | +function reverseComparer (x, y) { |
| 330 | + return y - x; |
| 331 | +} |
| 332 | + |
| 333 | +test('HistoricalScheduler with comparer', function (t) { |
| 334 | + var now = new Date(); |
| 335 | + |
| 336 | + var s = new HistoricalScheduler(now, reverseComparer); |
| 337 | + |
| 338 | + var list = []; |
| 339 | + |
| 340 | + s.scheduleAbsolute(null, now - 1000, function () { list.push(1); }); |
| 341 | + s.scheduleAbsolute(null, now - 2000, function () { list.push(2); }); |
| 342 | + |
| 343 | + s.start(); |
| 344 | + |
| 345 | + reactiveAssert(t, list, [ |
| 346 | + 1, |
| 347 | + 2 |
| 348 | + ]); |
| 349 | + |
| 350 | + t.end(); |
| 351 | +}); |
0 commit comments