1
+ var should = require ( 'should' ) ;
2
+ var async = require ( 'async' ) ;
3
+ var mongoose = require ( '../../bootstrap' ) ;
4
+ var UptimeCalculator = require ( '../../lib/uptimeCalculator' ) ;
5
+ var Check = require ( '../../models/check' ) ;
6
+ var CheckEvent = require ( '../../models/checkEvent' ) ;
7
+ var Ping = require ( '../../models/ping' ) ;
8
+
9
+ var check1 , check2 , now ; // fixtures
10
+
11
+ describe ( 'uptimeCalculator' , function ( ) {
12
+
13
+ before ( function ( done ) {
14
+ async . parallel ( [
15
+ function ( cb ) { Ping . collection . drop ( cb ) } ,
16
+ function ( cb ) { Check . collection . drop ( cb ) } ,
17
+ function ( cb ) { CheckEvent . collection . drop ( cb ) } ,
18
+ ] , done ) ;
19
+ } ) ;
20
+
21
+ before ( function ( ) {
22
+ now = Date . now ( ) ;
23
+ } ) ;
24
+
25
+ before ( function ( done ) {
26
+ check1 = new Check ( ) ;
27
+ check1 . save ( function ( err ) {
28
+ if ( err ) throw ( err ) ;
29
+ async . series ( [
30
+ function ( cb ) { Ping . createForCheck ( false , now - 3000 , 100 , check1 , 'dummy1' , '' , cb ) ; } ,
31
+ function ( cb ) { Ping . createForCheck ( false , now - 2000 , 100 , check1 , 'dummy2' , '' , cb ) ; } ,
32
+ function ( cb ) { Ping . createForCheck ( true , now - 1000 , 100 , check1 , 'dummy3' , '' , cb ) ; } ,
33
+ function ( cb ) { Ping . createForCheck ( true , now , 100 , check1 , 'dummy4' , '' , cb ) ; } ,
34
+ function ( cb ) { Ping . createForCheck ( true , now + 1000 , 100 , check1 , 'dummy5' , '' , cb ) ; } ,
35
+ function ( cb ) { Ping . createForCheck ( false , now + 2000 , 100 , check1 , 'dummy6' , '' , cb ) ; } ,
36
+ function ( cb ) { Ping . createForCheck ( true , now + 3000 , 100 , check1 , 'dummy7' , '' , cb ) ; } ,
37
+ ] , done ) ;
38
+ } ) ;
39
+ } ) ;
40
+
41
+ before ( function ( done ) {
42
+ check2 = new Check ( ) ;
43
+ check2 . save ( done ) ;
44
+ } ) ;
45
+
46
+ describe ( '#getPingBeforeTime' , function ( ) {
47
+
48
+ it ( 'should return nothing for new Checks' , function ( done ) {
49
+ var calculator = new UptimeCalculator ( check2 ) ;
50
+ calculator . getPingBeforeTime ( now , function ( err , ping ) {
51
+ if ( err ) throw ( err ) ;
52
+ should . not . exist ( ping ) ;
53
+ done ( ) ;
54
+ } ) ;
55
+ } ) ;
56
+
57
+ it ( 'should return the latest ping' , function ( done ) {
58
+ var calculator = new UptimeCalculator ( check1 ) ;
59
+ calculator . getPingBeforeTime ( now , function ( err , ping ) {
60
+ if ( err ) throw ( err ) ;
61
+ ping . monitorName . should . eql ( 'dummy4' ) ;
62
+ done ( ) ;
63
+ } ) ;
64
+ } ) ;
65
+
66
+ } ) ;
67
+
68
+ describe ( '#getUptimePeriods' , function ( ) {
69
+
70
+ it ( 'should return empty array when there is no ping' , function ( done ) {
71
+ var calculator = new UptimeCalculator ( check2 ) ;
72
+ calculator . getUptimePeriods ( Date . now ( ) , Date . now ( ) + 1000 , function ( err , periods ) {
73
+ if ( err ) throw ( err ) ;
74
+ periods . should . eql ( [ ] ) ;
75
+ done ( ) ;
76
+ } ) ;
77
+ } ) ;
78
+
79
+ it ( 'should return the correct period for not finished check' , function ( done ) {
80
+ var calculator = new UptimeCalculator ( check1 ) ;
81
+ calculator . getUptimePeriods ( now + 3000 , now + 6000 , function ( err , periods ) {
82
+ if ( err ) throw ( err ) ;
83
+ periods . should . eql ( [ [ now + 3000 , now + 6000 ] ] ) ;
84
+ done ( ) ;
85
+ } ) ;
86
+ } ) ;
87
+
88
+ it ( 'should return the correct period for not started check' , function ( done ) {
89
+ var calculator = new UptimeCalculator ( check1 ) ;
90
+ calculator . getUptimePeriods ( now - 6000 , now - 3000 , function ( err , periods ) {
91
+ if ( err ) throw ( err ) ;
92
+ periods . should . eql ( [ ] ) ;
93
+ done ( ) ;
94
+ } ) ;
95
+ } ) ;
96
+
97
+ it ( 'should return the correct period for crossing checks' , function ( done ) {
98
+ var calculator = new UptimeCalculator ( check1 ) ;
99
+ calculator . getUptimePeriods ( now - 6000 , now , function ( err , periods ) {
100
+ if ( err ) throw ( err ) ;
101
+ periods . should . eql ( [ [ now - 1000 , now ] ] ) ;
102
+ done ( ) ;
103
+ } ) ;
104
+ } ) ;
105
+
106
+ it ( 'should return the correct periods' , function ( done ) {
107
+ var calculator = new UptimeCalculator ( check1 ) ;
108
+ calculator . getUptimePeriods ( now - 3000 , now + 3000 , function ( err , periods ) {
109
+ if ( err ) throw ( err ) ;
110
+ periods . should . eql ( [ [ now - 1000 , now + 2000 ] , [ now + 3000 , now + 3000 ] ] ) ;
111
+ done ( ) ;
112
+ } ) ;
113
+ } ) ;
114
+
115
+ } ) ;
116
+
117
+ } ) ;
0 commit comments