UNPKG

7 kBJavaScriptView Raw
1
2/**
3 * Tests for Function.prototype.async
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// Simple synchronous function
17function syncFunction(a, b) {
18 return asyncFunction.sync(null, a, b);
19}
20
21// Simple synchronous function throws an exception
22function syncFunctionThrowsException(a, b) {
23 throw 'something went wrong';
24}
25
26// test object
27var testObject = {
28
29 property : 2,
30
31 syncMethod : function someAsyncMethod(b) {
32 return asyncFunction.sync(null, this.property, b);
33 },
34
35 syncMethodThrowsException : function someAsyncMethodThrowsException(b) {
36 throw 'something went wrong';
37 }
38}
39
40var runTest = module.exports = function(callback)
41{
42 var e;
43
44 try {
45
46 // test on returning value
47 var syncFunctionAsync = syncFunction.async();
48 syncFunctionAsync(2, 3, function(err, result){
49 assert.equal(result, 2 + 3);
50 })
51
52 // test on throws exception
53 var syncFunctionThrowsExceptionAsync = syncFunctionThrowsException.async();
54 syncFunctionThrowsExceptionAsync(2, 3, function(err, result){
55 assert.equal(err, 'something went wrong');
56 })
57
58 // test on throws exception when call without callback
59 /*var syncFunctionAsync = syncFunction.async();
60 assert.throws(function(){
61 syncFunctionAsync(2, 3);
62 }, 'Missing callback as last argument to async function');*/
63
64 // test on working synchronously within a Fiber
65 Fiber(function(){
66 var result = syncFunctionAsync(2, 3);
67 assert.equal(result, 5);
68 }).run()
69
70 // test on working synchronously within a Fiber with object context
71 Fiber(function(){
72 testObject.syncMethodAuto = testObject.syncMethod.async();
73 var result = testObject.syncMethodAuto(3);
74 assert.equal(result, testObject.property + 3);
75 }).run()
76
77 // test running in a same fiber
78 Fiber(function(){
79 var fiber = Fiber.current;
80 (function(){
81 assert.ok(Fiber.current instanceof Fiber);
82 assert.strictEqual(Fiber.current, fiber);
83 }).async()();
84 }).run()
85
86 // test on returning value with object context
87 var syncMethodAsync = testObject.syncMethod.async(testObject);
88 syncMethodAsync(3, function(err, result){
89 try {
90 assert.equal(result, testObject.property + 3);
91 }
92 catch (e){
93 console.error(e.stack);
94 }
95 })
96
97 // test automatic context assignment
98 testObject.syncMethodAuto = testObject.syncMethod.async();
99 testObject.syncMethodAuto(3, function(err, result){
100 assert.equal(result, testObject.property + 3);
101 })
102
103 // test on throws exception with object context
104 var syncMethodThrowsExceptionAsync = testObject.syncMethodThrowsException.async(testObject);
105 syncMethodThrowsExceptionAsync(3, function(err, result){
106 assert.equal(err, 'something went wrong');
107 })
108
109 // Test async call with .sync()
110 Fiber(function(){
111 var syncFunctionAsync = syncFunction.async();
112 var result = syncFunctionAsync.sync(null, 2, 3);
113 assert.equal(result, 2 + 3);
114 }).run()
115
116 // Test async call with .sync() throwing exception
117 Fiber(function(){
118 var syncFunctionThrowsExceptionAsync = syncFunctionThrowsException.async();
119 assert.throws(function(){
120 syncFunctionThrowsExceptionAsync.sync(null, 2, 3);
121 }, 'something went wrong');
122 }).run()
123
124 // Test async call with .sync() with object context 1
125 Fiber(function(){
126 var syncMethodAsync = testObject.syncMethod.async(testObject);
127 var result = syncMethodAsync.sync(testObject, 3);
128 assert.equal(result, testObject.property + 3);
129 }).run()
130
131 // Test async call with .sync() with object context 2
132 Fiber(function(){
133 var syncMethodAsync = testObject.syncMethod.async(testObject);
134 var result = syncMethodAsync.sync(null, 3);
135 assert.equal(result, testObject.property + 3);
136 }).run()
137
138 // Test async call with .future()
139 Sync(function(){
140 var syncFunctionAsync = syncFunction.async();
141 var future = syncFunctionAsync.future(null, 2, 3);
142 assert.equal(future.result, 2 + 3);
143 }, function(err){
144 if (err) console.error(err);
145 })
146
147 // Test async call with .future() throwing exception
148 Sync(function(){
149 var syncFunctionThrowsExceptionAsync = syncFunctionThrowsException.async();
150 assert.throws(function(){
151 var future = syncFunctionThrowsExceptionAsync.future(null, 2, 3);
152 future.result;
153 }, 'something went wrong');
154 }, function(err){
155 if (err) console.error(err);
156 })
157
158 // Test async call with .future() with object context
159 Sync(function(){
160 var syncMethodAsync = testObject.syncMethod.async(testObject);
161 var future = syncMethodAsync.future(testObject, 3);
162 assert.equal(future.result, testObject.property + 3);
163 }, function(err){
164 if (err) console.error(err);
165 })
166
167 // Test async call with .future() with object context 2
168 Sync(function(){
169 var syncMethodAsync = testObject.syncMethod.async(testObject);
170 var future = syncMethodAsync.future(null, 3);
171 assert.equal(future.result, testObject.property + 3);
172 }, function(err){
173 if (err) console.error(err);
174 })
175
176 // Test async call in the same fiber
177 Sync(function(){
178
179 var result;
180
181 var someSyncFunction = function() {
182
183 result = asyncFunction.sync(null, 2, 3);
184
185 }.async()
186
187 var fiber = Fiber.current;
188
189 process.nextTick(function(){
190 someSyncFunction();
191
192 process.nextTick(function(){
193 fiber.run();
194 })
195 })
196
197 Fiber.yield();
198
199 assert.equal(result, 5);
200
201 }, function(err){
202 if (err) console.error(err);
203 })
204
205 }
206 catch (e) {
207 console.error(e.stack);
208 }
209
210 if (callback) {
211 callback(e);
212 }
213}
214
215if (!module.parent) {
216 runTest(function(){
217 console.log('%s done', __filename);
218 });
219}