summaryrefslogtreecommitdiff
path: root/school/node_modules/node-forge/tests/tasks.js
blob: dd3ffde97f7b1ee3e778c971fb244726dd3bd18d (plain)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/**
 * Forge Tasks Test
 *
 * @author David I. Lehn <dlehn@digitalbazaar.com>
 *
 * Copyright (c) 2009-2010 Digital Bazaar, Inc. All rights reserved.
 */
jQuery(function($)
{
   var cat = 'forge.tests.tasks';

   var tests = [];
   var passed = 0;
   var failed = 0;
   
   var init = function() {
      passed = failed = 0;
      $('.ready,.testing,.pass,.fail')
         .removeClass('ready testing pass fail');
      $('#status')
         .text('Ready.')
         .addClass('ready');
      $('#total').text(tests.length);
      $('#pass').text(passed);
      $('#fail').text(failed);
      $('.expect').empty();
      $('.result').empty();
      $('.time').empty();
      $('#start').removeAttr('disabled');
   };

   var start = function()
   {
      $('#start').attr('disabled', 'disabled');
      // meta! use tasks to run the task tests
      forge.task.start({
         type: 'test',
         run: function(task) {
            task.next('starting', function(task) {
               forge.log.debug(cat, 'start');
               $('#status')
                  .text('Testing...')
                  .addClass('testing')
                  .removeClass('idle');
            });
            $.each(tests, function(i, test) {
               task.next('test', function(task) {
                  var title = $('li:first', test.container);
                  if($('#scroll:checked').length === 1)
                  {
                     $('html,body').animate({scrollTop: title.offset().top});
                  }
                  title.addClass('testing');
                  test.run(task, test);
               });
               task.next('test', function(task) {
                  $('li:first', test.container).removeClass('testing');
               });
            });
            task.next('success', function(task) {
               forge.log.debug(cat, 'done');
               if(failed === 0) {
                  $('#status')
                     .text('PASS')
                     .addClass('pass')
                     .removeClass('testing');
               } else {
                  // FIXME: should just be hitting failure() below
                  $('#status')
                     .text('FAIL')
                     .addClass('fail')
                     .removeClass('testing');
               }
            });
         },
         failure: function() {
            $('#status')
               .text('FAIL')
               .addClass('fail')
               .removeClass('testing');
         }
      });
   };

   $('#start').click(function() {
      start();
   });
   
   $('#reset').click(function() {
      init();
   });
   
   var addTest = function(name, run)
   {
      var container = $('<ul><li>Test ' + name + '</li><ul/></ul>');
      var expect = $('<li>Expect: <span class="expect"/></li>');
      var result = $('<li>Result: <span class="result"/></li>');
      var time = $('<li>Time: <span class="time"/></li>');
      $('ul', container).append(expect).append(result).append(time);
      $('#tests').append(container);
      var test = {
         container: container,
         startTime: null,
         run: function(task, test) {
            test.startTime = new Date();
            run(task, test);
         },
         expect: $('span', expect),
         result: $('span', result),
         check: function() {
            var e = test.expect.text();
            var r = test.result.text();
            (e == r) ? test.pass() : test.fail();
         },
         pass: function() {
            passed += 1;
            $('#pass').text(passed);
            $('li:first', container).addClass('pass');
            var dt = new Date() - test.startTime;
            $('span.time', container).html(dt);
         },
         fail: function() {
            failed += 1;
            $('#fail').text(failed);
            $('li:first', container).addClass('fail');
            var dt = new Date() - test.startTime;
            $('span.time', container).html(dt);
         }
      };
      tests.push(test);
   };

   addTest('pass', function(task, test) {
      test.pass();
   });

   addTest('check', function(task, test) {
      test.check();
   });

   addTest('task 1', function(task, test) {
      task.next(function(task) {
         test.pass();
      });
   });

   addTest('task check()', function(task, test) {
      test.expect.append('check');
      task.next(function(task) {
         test.result.append('check');
      });
      task.next(function(task) {
         test.check();
      });
   });

   addTest('serial 20', function(task, test) {
      // total
      var n = 20;
      // counter used in the tasks
      var taskn = 0;
      for(var i = 0; i < n; ++i) {
         test.expect.append(i + ' ');
         task.next(function(task) {
            test.result.append(taskn++ + ' ');
         });
      }
      task.next(function(task) {
         test.check();
      });
   });

   addTest('ajax block', function(task, test) {
      test.expect.append('.');
      task.next(function(task) {
         task.parent.block();
         $.ajax({
            type: 'GET',
            url: 'tasks.html',
            success: function() {
               test.result.append('.');
               task.parent.unblock();
            }
         });
      });
      task.next(function(task) {
         test.check();
      });
   });

   addTest('serial ajax', function(task, test) {
      var n = 10;
      for(var i = 0; i < n; ++i)
      {
         test.expect.append(i + ' ');
      }
      task.next(function(task) {
         // create parallel functions
         task.parent.block(n);
         for(var i = 0; i < n; ++i)
         {
            // pass value into closure
            (function(i)
            {
               // serial tasks
               task.next(function(ajaxTask)
               {
                  $.ajax({
                     type: 'GET',
                     url: 'tasks.html',
                     success: function() {
                        // results use top level task
                        test.result.append(i + ' ');
                        task.parent.unblock();
                     }
                  });
               });
            })(i);
         }
      });
      task.next(function(task) {
         test.check();
      });
   });

   addTest('parallel ajax', function(task, test) {
      task.next(function(task) {
         var n = 10;
         // create parallel functions
         var tasks = [];
         for(var i = 0; i < n; ++i)
         {
            // pass value into closure
            (function(i)
            {
               tasks.push(function(ajaxTask)
               {
                  $.ajax({
                     type: 'GET',
                     url: 'tasks.html',
                     success: function() {
                        // results use top level task
                        test.result.append(i + ' ');
                     }
                  });
               });
            })(i);
         }
         // launch in parallel
         task.parallel(tasks);
      });
      task.next(function(task) {
         test.pass();
      });
   });

   addTest('linear empty tasks rate', function(task, test) {
      test.expect.append('-');
      // total
      var n = 100;
      var start = new Date();
      for(var i = 0; i < n; ++i) {
         // empty task
         task.next(function(task) {});
      }
      task.next(function(task) {
         var dt = (new Date() - start) / 1000;
         var res = $('<ul/>')
            .append('<li>Tasks: ' + n + '</li>')
            .append('<li>Time: ' + dt + 's</li>')
            .append('<li>Rate: ' + n/dt + ' tasks/s</li>')
            .append('<li>Task Time: ' + 1000*dt/n + ' ms/tasks</li>');
         test.result.html(res);
         test.pass();
      });
   });

   addTest('sleep', function(task, test) {
      test.expect.append('-');
      var st = 1000;
      var start = new Date();
      task.next(function(task) {
         task.sleep(st);
      });
      task.next(function(task) {
         var dt = new Date() - start;
         var res = $('<ul/>')
            .append('<li>Sleep Time : ' + st + 'ms</li>')
            .append('<li>Real Time: ' + dt + 'ms</li>')
            .append('<li>Diff: ' + (dt-st) + 'ms</li>');
         test.result.html(res);
         test.pass();
      });
   });
   
   addTest('serial 20 + sleep', function(task, test) {
      // total
      var n = 20;
      // counter used in the tasks
      var taskn = 0;
      for(var i = 0; i < n; ++i) {
         test.expect.append(i + ' ');
         task.next(function(task) {
            task.sleep(20);
            test.result.append(taskn++ + ' ');
         });
      }
      task.next(function(task) {
         test.check();
      });
   });
   
   addTest('concurrent tasks', function(task, test)
   {
      var colors = [
         'red',
         'green',
         'blue',
         'black',
         'purple',
         'goldenrod',
         'maroon',
         'gray',
         'teal',
         'magenta'
      ];
      var count = colors.length;
      task.next(function(task)
      {
         var main = task;
         task.block(count);
         
         var tasks = [];
         for(var i = 0; i < count; ++i)
         {
            var makefunction = function(index)
            {
               return function(task)
               {
                  // total
                  var n = 20;
                  // counter used in the tasks
                  var taskn = 0;
                  for(var j = 0; j < n; j++)
                  {
                     task.next(function(task)
                     {
                        test.result.append(
                           '<span style=\"color:' + colors[index] + ';\">' +
                           taskn++ + '</span> ');
                     });
                  }
                  task.next(function(task)
                  {
                     main.unblock();
                  });
               };
            };
            tasks.push(
            {
               type: 'concurrent' + i,
               run: makefunction(i)
            });
         }
         
         for(var i = 0; i < count; ++i)
         {
            forge.task.start(tasks[i]);
         }
      });
      
      task.next(function(task) {
         test.pass();
      });
   });

   init();
});