-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathtests.js
132 lines (106 loc) · 3.34 KB
/
tests.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
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
"use strict";
var util = require("util");
var dns = require("dns");
var wait = require("./waitfor");
// ----------------------
// DNS TESTS -------------
// ----------------------
function showReverse(addr){
console.log("reverse for " + addr + ": " + JSON.stringify(wait.for(dns.reverse,addr)));
}
function sequential_resolve_parallel_reverse(hostname){
console.log(dns.resolve4);
console.log('dns.resolve4 ',hostname);
var addresses = wait.for(dns.resolve4,hostname);
console.log("addresses: ",JSON.stringify(addresses));
for (var i = 0; i < addresses.length; i++) {
wait.launchFiber(showReverse, addresses[i]);
};
}
function sequential_resolve(hostname){
console.log('dns.resolve4 ',hostname);
var addresses = wait.for(dns.resolve4,hostname);
console.log("addresses: ",JSON.stringify(addresses));
for (var i = 0; i < addresses.length; i++) {
showReverse(addresses[i]);
};
}
// ----------------------
// OBJECT TESTS ---------
// ----------------------
function Constructor(value,pong){
this.value = value;
this.pong = pong;
}
Constructor.prototype.think=function(callback){
if (!this) {
var errMsg='ERR: this is null, at: Constructor.prototype.think';
console.log(errMsg);
callback(new Error(errMsg));
}
else {
console.log('thinking...');
var self=this;
// callback after 1.5 secs
setTimeout(function(){
callback(null, 'the answer is: '+self.value)}
,1500);
}
};
Constructor.prototype.pingPong=function(ping,callback){
// callback before return
callback(null, ping+'...'+this.pong);
}
var theAnswer = new Constructor(42,'tomeito');
var theWrongAnswer = new Constructor('a thousand','tomatito');
// -------------------
// test callback called more than one time
// -------------------
function callItTwice(callback){
setTimeout(callback,200);
setTimeout(callback,300);
setTimeout(callback,400);
setTimeout(callback,500);
};
// -------------------
// RUN TESTS (Fiber)--
// -------------------
function runTests(hostname){
console.log('--------------------------------');
console.log('resolve, then sequential reverse');
sequential_resolve(hostname);
console.log('--------------------------------');
console.log('--------------------------------');
console.log('resolve, then parallel reverse');
sequential_resolve_parallel_reverse(hostname);
console.log('--------------------------------');
//METHOD TEST (passing 'this' to the function)
console.log(wait.forMethod(theAnswer,'think'));
console.log(wait.forMethod(theWrongAnswer,'think'));
console.log('--------------------------------');
console.log('launch wait.parallel tests');
var pt = require('./parallel-tests')
pt.runTests(hostname);
console.log(wait.forMethod(theAnswer,'pingPong','tomato'));
console.log(wait.forMethod(theWrongAnswer,'pingPong','pera'));
wait.for(callItTwice);
/*
}
catch(e){
}
*/
console.log('end of tests');
}
// MAIN
process.on('uncaughtException', function(e) {
if (e.message.indexOf('called twice')!=-1) console.log('OK: wait.for(callItTwice) throw an expected error')
else console.log(e);
});
try{
wait.launchFiber(runTests,"google.com");
console.log('after wait.launchFiber');
}
catch(e){
console.log('catched!');
console.log(e);
};