dc09ceb4324380c36a30557bdfab6c9b03992dc4
[mkws-moved-to-github.git] / test / spec / async.spec.js
1 /* Copyright (c) 2013 IndexData ApS. http://indexdata.com
2  *
3  * async check
4  *
5  */
6 describe("Asynchronous check", function () {
7     it("contains spec with an expectation", function () {
8         expect(true).toBe(true);
9     });
10
11     // Asynchronous part
12     it("simple check", function () {
13         var max_time = 1;
14         var timer = 0;
15
16         function found(time, none) {
17             setTimeout(function () {
18                 timer = time;
19             }, time * 1000);
20             expect(time >= 0).toBeTruthy();
21         }
22
23         runs(function () {
24             // check hit counter after N seconds
25             found(0, true);
26             found(0.2);
27             found(0.4);
28             found(0.5);
29             found(0.7);
30             found(max_time);
31         });
32
33         waitsFor(function () {
34             // console.log("waits for ... " + timer);
35             return timer == max_time ? true : false;
36         }, "The Value should be N seconds", max_time * 1000);
37
38         runs(function () {
39             expect(timer).toEqual(max_time);
40         });
41     });
42
43
44     it("double async check", function () {
45         var max_time = 0.5;
46         var timer = 0;
47
48         function found(time, none) {
49             setTimeout(function () {
50                 timer = time;
51             }, time * 1000);
52             expect(time >= 0).toBeTruthy();
53         }
54
55         runs(function () {
56             found(0);
57             found(0.2);
58             found(max_time - 0.1);
59         });
60
61         waitsFor(function () {
62             return timer == max_time - 0.1 ? true : false;
63         }, "The Value should be N seconds", max_time * 1000);
64
65         runs(function () {
66             expect(timer <= max_time).toBeTruthy();
67         });
68
69         timer = 0;
70         runs(function () {
71             found(0.1);
72             found(max_time);
73         });
74
75         waitsFor(function () {
76             // console.log("waits for ... " + timer);
77             return timer == max_time ? true : false;
78         }, "The Value should be N seconds", max_time * 1000);
79
80         runs(function () {
81             expect(timer <= max_time).toBeTruthy();
82         });
83     });
84
85 });