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 syncFunction(a, b) {
|
18 | return asyncFunction.sync(null, a, b);
|
19 | }
|
20 |
|
21 |
|
22 | function syncFunctionThrowsException(a, b) {
|
23 | throw 'something went wrong';
|
24 | }
|
25 |
|
26 |
|
27 | var 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 |
|
40 | var runTest = module.exports = function(callback)
|
41 | {
|
42 | var e;
|
43 |
|
44 | try {
|
45 |
|
46 |
|
47 | var syncFunctionAsync = syncFunction.async();
|
48 | syncFunctionAsync(2, 3, function(err, result){
|
49 | assert.equal(result, 2 + 3);
|
50 | })
|
51 |
|
52 |
|
53 | var syncFunctionThrowsExceptionAsync = syncFunctionThrowsException.async();
|
54 | syncFunctionThrowsExceptionAsync(2, 3, function(err, result){
|
55 | assert.equal(err, 'something went wrong');
|
56 | })
|
57 |
|
58 |
|
59 | |
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | Fiber(function(){
|
66 | var result = syncFunctionAsync(2, 3);
|
67 | assert.equal(result, 5);
|
68 | }).run()
|
69 |
|
70 |
|
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 |
|
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 |
|
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 |
|
98 | testObject.syncMethodAuto = testObject.syncMethod.async();
|
99 | testObject.syncMethodAuto(3, function(err, result){
|
100 | assert.equal(result, testObject.property + 3);
|
101 | })
|
102 |
|
103 |
|
104 | var syncMethodThrowsExceptionAsync = testObject.syncMethodThrowsException.async(testObject);
|
105 | syncMethodThrowsExceptionAsync(3, function(err, result){
|
106 | assert.equal(err, 'something went wrong');
|
107 | })
|
108 |
|
109 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
215 | if (!module.parent) {
|
216 | runTest(function(){
|
217 | console.log('%s done', __filename);
|
218 | });
|
219 | }
|