This repository is currently being migrated. It's locked while the migration is in progress.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolling.js
70 lines (60 loc) · 2.51 KB
/
polling.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
/*
This is a very crude wrapper around some simple logic to count the number of
requests done. All attempts to abstract the whole polling process failed due
to postman seemingly not being able to call setNextRequest in library code.
This means there will still be quite a bit of code duplication in the tests,
but at least they won't keep polling indefinitely.
The main entry point to the polling will be a call to polling.incrementCcount()
- this will return a boolean which semantically should be understood as a
"should I keep polling?" hint. If you want to handle the timeout case, you can
do that, but be warned that there's not much you can do. Postman doesn't seem
to be able to abort tests prematurely, so your next request will most likely
fail anyways.
In the case of sucessful polling, it's probably a good idea to call
polling.resetCount() again after your assertions.
Usage:
* install the script as we install Postman BDD, i.e. download and save in
global variable, and eval it in every test where it's used.
* postman is very leaky when it comes to variables, especially when
terminating tests prematurely - so it might be a good idea to start with a
call to poling.resetCount()
Example:
eval(globals.polling);
if (response.body.is_ended === false && response.body.is_erroneous === false) {
if (polling.incrementCount()) {
postman.setNextRequest('Poll task: account_no_pin.send_pin');
new Promise(resolve => setTimeout(resolve, 1000));
}
} else {
describe('should be ended', () => {
response.body.is_ended.should.be.true;
response.body.is_erroneous.should.be.false;
}
polling.resetCount();
}
*/
polling = {
MAX_POLL_COUNT: 20,
getCount: function() {
var count = postman.getEnvironmentVariable('__poll_count');
return (count === undefined) ? 0 : JSON.parse(count);
},
setCount: function(value) {
postman.setEnvironmentVariable('__poll_count', JSON.stringify(value));
},
resetCount: function() {
postman.clearEnvironmentVariable('__poll_count');
},
incrementCount: function() {
polling.setCount(polling.getCount() + 1);
var keepPolling = polling.getCount() < polling.MAX_POLL_COUNT;
if (!keepPolling) polling.resetCount();
return keepPolling
},
isDone: function(response) {
return (response.body.is_ended ||
response.body.is_erroneous ||
response.body.is_waiting_for_pin ||
response.body.is_waiting_for_response)
}
}