1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | var Sync = require('..'),
|
7 | assert = require('assert');
|
8 |
|
9 |
|
10 | function asyncFunction(a, b, callback) {
|
11 | process.nextTick(function(){
|
12 | callback(null, a + b);
|
13 | })
|
14 | }
|
15 |
|
16 |
|
17 | function asyncFunctionTimeout(t, callback) {
|
18 | setTimeout(function(){
|
19 | callback(null, 'result');
|
20 | }, t)
|
21 | }
|
22 |
|
23 |
|
24 | function syncFunctionTimeout(t) {
|
25 | var fiber = Fiber.current;
|
26 | setTimeout(function(){
|
27 | fiber.run('result');
|
28 | }, t)
|
29 | return yield();
|
30 | }
|
31 |
|
32 |
|
33 | function asyncFunctionThrowsException(a, b, callback) {
|
34 | process.nextTick(function(){
|
35 | callback('something went wrong');
|
36 | })
|
37 | }
|
38 |
|
39 |
|
40 | function asyncFunctionCallbackTwice(a, b, callback) {
|
41 | process.nextTick(function(){
|
42 | callback(null, a + b);
|
43 | callback(null, a - b);
|
44 | })
|
45 | }
|
46 |
|
47 |
|
48 | var 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 |
|
66 | var runTest = module.exports = function(callback)
|
67 | {
|
68 | Sync(function(){
|
69 |
|
70 |
|
71 | var future = asyncFunction.future(null, 2, 3);
|
72 |
|
73 | assert.ok(future instanceof Sync.Future);
|
74 | assert.ok(future instanceof Function);
|
75 |
|
76 | assert.equal(future.result, 2 + 3);
|
77 |
|
78 | assert.strictEqual(future.error, null);
|
79 |
|
80 |
|
81 | var future = asyncFunction.future(null, 2, 3);
|
82 |
|
83 | assert.equal(future.yield(), 2 + 3);
|
84 |
|
85 | assert.strictEqual(future.error, null);
|
86 |
|
87 |
|
88 | var future = asyncFunction.future(null, 2, 3);
|
89 |
|
90 | assert.strictEqual(future.error, null);
|
91 |
|
92 | assert.equal(future.result, 2 + 3);
|
93 |
|
94 |
|
95 | var future = asyncFunctionThrowsException.future(null, 2, 3);
|
96 | assert.throws(function(){
|
97 | future.result;
|
98 | }, 'something went wrong');
|
99 |
|
100 | assert.ok(future.error);
|
101 |
|
102 |
|
103 | var future = asyncFunctionCallbackTwice.future(null, 2, 3);
|
104 | assert.equal(future.result, 2 + 3);
|
105 |
|
106 |
|
107 | var future = testObject.asyncMethod.future(testObject, 3);
|
108 | assert.equal(future.result, testObject.property + 3);
|
109 |
|
110 |
|
111 | var future = testObject.asyncMethodThrowsException.future(testObject, 2);
|
112 | assert.throws(function(){
|
113 | future.result;
|
114 | }, 'something went wrong');
|
115 |
|
116 |
|
117 | asyncFunction(2, 3, future = new Sync.Future());
|
118 |
|
119 | assert.strictEqual(future.error, null);
|
120 |
|
121 | assert.equal(future.result, 2 + 3);
|
122 |
|
123 |
|
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 |
|
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 |
|
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 |
|
157 | var future = asyncFunctionTimeout.future(null, 100);
|
158 | future.timeout = 200;
|
159 |
|
160 | assert.equal(future.result, 'result');
|
161 |
|
162 | assert.strictEqual(future.error, null);
|
163 |
|
164 |
|
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 |
|
173 | assert.ok(future.error instanceof Error);
|
174 | assert.ok(~future.error.stack.indexOf(__filename));
|
175 |
|
176 |
|
177 | asyncFunctionTimeout(100, future = new Sync.Future(200));
|
178 |
|
179 | assert.strictEqual(future.error, null);
|
180 |
|
181 | assert.equal(future.result, 'result');
|
182 |
|
183 |
|
184 | asyncFunctionTimeout(100, future = new Sync.Future(50));
|
185 | assert.throws(function(){
|
186 | future.result;
|
187 | }, 'future should throw timeout exception')
|
188 |
|
189 |
|
190 | assert.ok(future.error instanceof Error);
|
191 | assert.ok(~future.error.stack.indexOf(__filename));
|
192 |
|
193 |
|
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 |
|
237 | if (!module.parent) {
|
238 | runTest(function(){
|
239 | console.log('%s done', __filename);
|
240 | });
|
241 | }
|