UNPKG

7.2 kBJavaScriptView Raw
1
2/**
3 * Tests for Function.prototype.future
4 */
5
6var Sync = require('..'),
7 assert = require('assert');
8
9// Simple asynchronous function
10function asyncFunction(a, b, callback) {
11 process.nextTick(function(){
12 callback(null, a + b);
13 })
14}
15
16// asynchronous with timeout
17function asyncFunctionTimeout(t, callback) {
18 setTimeout(function(){
19 callback(null, 'result');
20 }, t)
21}
22
23// synchronous with timeout
24function syncFunctionTimeout(t) {
25 var fiber = Fiber.current;
26 setTimeout(function(){
27 fiber.run('result');
28 }, t)
29 return yield();
30}
31
32// Simple asynchronous function which throws an exception
33function asyncFunctionThrowsException(a, b, callback) {
34 process.nextTick(function(){
35 callback('something went wrong');
36 })
37}
38
39// Wrong asynchronous which calls callback twice
40function asyncFunctionCallbackTwice(a, b, callback) {
41 process.nextTick(function(){
42 callback(null, a + b);
43 callback(null, a - b);
44 })
45}
46
47// test object
48var testObject = {
49
50 property : 2,
51
52 asyncMethod : function someAsyncMethod(b, callback) {
53 var self = this;
54 process.nextTick(function(){
55 callback(null, self.property + b);
56 })
57 },
58
59 asyncMethodThrowsException : function someAsyncMethodThrowsException(b, callback) {
60 process.nextTick(function(){
61 callback('something went wrong');
62 })
63 }
64}
65
66var runTest = module.exports = function(callback)
67{
68 Sync(function(){
69
70 // test on returning value
71 var future = asyncFunction.future(null, 2, 3);
72 // check future function
73 assert.ok(future instanceof Sync.Future);
74 assert.ok(future instanceof Function);
75 // check future result
76 assert.equal(future.result, 2 + 3);
77 // check error
78 assert.strictEqual(future.error, null);
79
80 // test on returning value
81 var future = asyncFunction.future(null, 2, 3);
82 // check future result
83 assert.equal(future.yield(), 2 + 3);
84 // check error
85 assert.strictEqual(future.error, null);
86
87 // check yield on error getter
88 var future = asyncFunction.future(null, 2, 3);
89 // check error
90 assert.strictEqual(future.error, null);
91 // check future result
92 assert.equal(future.result, 2 + 3);
93
94 // test on throws exception
95 var future = asyncFunctionThrowsException.future(null, 2, 3);
96 assert.throws(function(){
97 future.result;
98 }, 'something went wrong');
99 // check error
100 assert.ok(future.error);
101
102 // test asynchronous which calls callback twice (should not be called twice)
103 var future = asyncFunctionCallbackTwice.future(null, 2, 3);
104 assert.equal(future.result, 2 + 3);
105
106 // test on returning value with object context
107 var future = testObject.asyncMethod.future(testObject, 3);
108 assert.equal(future.result, testObject.property + 3);
109
110 // test on throws exception with object context
111 var future = testObject.asyncMethodThrowsException.future(testObject, 2);
112 assert.throws(function(){
113 future.result;
114 }, 'something went wrong');
115
116 // test straight Sync.Future usage
117 asyncFunction(2, 3, future = new Sync.Future());
118 // check error
119 assert.strictEqual(future.error, null);
120 // check future result
121 assert.equal(future.result, 2 + 3);
122
123 // test two futures goes in parallel
124 var start = new Date();
125 var future1 = asyncFunctionTimeout.future(null, 100);
126 var future2 = asyncFunctionTimeout.future(null, 100);
127 assert.ok(future1.result);
128 assert.ok(future2.result);
129 var duration = new Date - start;
130 assert.ok(duration < 110);
131
132 // test two async() futures goes in parallel
133 var start = new Date();
134 var future1 = syncFunctionTimeout.async().future(null, 100);
135 var future2 = syncFunctionTimeout.async().future(null, 100);
136 assert.ok(future1.result);
137 assert.ok(future2.result);
138 var duration = new Date - start;
139 assert.ok(duration < 110);
140
141 // Test futures are automatically resolved when Fiber ends
142 var futures = [];
143 Sync(function(){
144 futures.push(asyncFunction.future(null, 2, 3));
145 futures.push(asyncFunction.future(null, 2, 3));
146 }, function(err){
147 if (err) return console.error(err);
148 try {
149 while (futures.length) assert.ok(futures.shift().resolved);
150 }
151 catch (e) {
152 console.error(e);
153 }
154 })
155
156 // Test timeout
157 var future = asyncFunctionTimeout.future(null, 100);
158 future.timeout = 200;
159 // check future result
160 assert.equal(future.result, 'result');
161 // check error
162 assert.strictEqual(future.error, null);
163
164 // Test timeout error
165 var future = asyncFunctionTimeout.future(null, 100);
166 future.timeout = 50;
167
168 assert.throws(function(){
169 future.result;
170 }, 'future should throw timeout exception')
171
172 // check error
173 assert.ok(future.error instanceof Error);
174 assert.ok(~future.error.stack.indexOf(__filename));
175
176 // test straight Sync.Future timeout usage
177 asyncFunctionTimeout(100, future = new Sync.Future(200));
178 // check error
179 assert.strictEqual(future.error, null);
180 // check future result
181 assert.equal(future.result, 'result');
182
183 // test straight Sync.Future timeout error
184 asyncFunctionTimeout(100, future = new Sync.Future(50));
185 assert.throws(function(){
186 future.result;
187 }, 'future should throw timeout exception')
188
189 // check error
190 assert.ok(future.error instanceof Error);
191 assert.ok(~future.error.stack.indexOf(__filename));
192
193 // TODO: test multiple future calls with errors
194 return;
195
196 var foo = function(a, b, callback)
197 {
198 process.nextTick(function(){
199 callback('error');
200 })
201 }
202
203 var fn = function()
204 {
205 var future = foo.future(null, 2, 3);
206 var future2 = foo.future(null, 2, 3);
207 console.log('x');
208 var a = future.result;
209 console.log('y');
210 var b = future2.result;
211
212 }.async()
213
214 Sync(function(){
215
216 try {
217 fn.sync();
218 }
219 catch (e) {
220 console.log('catched', e.stack);
221 }
222
223 }, function(err){
224 if (err) console.error('hehe', err);
225 })
226
227 }, function(e){
228 if (e) {
229 console.error(e.stack);
230 }
231 if (callback) {
232 callback(e);
233 }
234 })
235}
236
237if (!module.parent) {
238 runTest(function(){
239 console.log('%s done', __filename);
240 });
241}