forked from appium/appium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
warp.js
98 lines (91 loc) · 2.98 KB
/
warp.js
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
"use strict";
var logger = require('./logger').get('appium')
, inTimeWarp = false
, timeWarpDoneCb = function() {}
, warpFactor = 1
, exec = require('child_process').exec;
var pad0 = function(x) {
if (x.toString().length == 1) {
x = '0' + x;
}
return x;
};
var getDateStr = function(dateObj) {
return [pad0(dateObj.getHours()),
pad0(dateObj.getMinutes()),
'.',
pad0(dateObj.getSeconds())].join('');
};
exports.checkWarpDrive = function(cb) {
var dateStr = getDateStr(new Date());
exec('echo "\n" | sudo -S /bin/date ' + dateStr, function(err) {
if (err) {
logger.error("You can't access the warp drive; make sure you're the " +
"Captain or Scotty");
cb(false);
} else {
cb(true);
}
});
};
exports.timeWarp = function(period, warp, resetTime) {
resetTime = resetTime === "undefined" ? false : resetTime;
var warpStartTime = Date.now();
if (!inTimeWarp) {
var numHops = 0;
logger.info("Starting time warp");
period = typeof period === "undefined" ? 50 : period;
warp = typeof warp === "undefined" ? 1000 : warp;
warpFactor = (warp + period) / period;
var makeJump = function() {
var curMs, newDate, dateStr, realTime, fakeTime, info;
if (inTimeWarp) {
curMs = Date.now();
newDate = new Date(curMs + warp);
dateStr = getDateStr(newDate);
exec('sudo -S /bin/date ' + dateStr, function(err, stdout, stderr) {
if (err) {
logger.error("Could not set date, here's stderr:");
logger.error(stderr);
throw new Error("time warp failed, not enough power to make jump");
} else {
numHops++;
setTimeout(makeJump, period);
}
});
} else {
warpFactor = 1;
realTime = period * numHops / 1000;
fakeTime = (warp * numHops / 1000) + realTime;
info = "Moved forward " + fakeTime + " secs in " + realTime + " actual seconds. That's a warp factor of " + (fakeTime / realTime).toFixed(1) + "!";
logger.info("Stopping time warp: " + info);
if (resetTime) {
newDate = new Date(warpStartTime + ((period + 8) * numHops));
var newDateStr = getDateStr(newDate);
exec('sudo /bin/date ' + newDateStr, function() {
logger.info("Attempted to reset system time to something " +
"reasonable. Hope you enjoyed your trip to the future");
timeWarpDoneCb(period * numHops);
});
} else {
timeWarpDoneCb(period * numHops);
}
}
};
inTimeWarp = true;
makeJump();
} else {
logger.info("You asked for a time warp but we're already in the middle " +
"of a series of jumps...");
}
};
exports.stopTimeWarp = function(cb) {
inTimeWarp = false;
if (typeof cb === "function") {
timeWarpDoneCb = cb;
}
};
exports.setWarpAwareTimeout = function(fn, ms) {
ms *= warpFactor;
setTimeout(fn, ms);
};