7
7
import { identity , noop } from 'lodash' ;
8
8
import sinon from 'sinon' ;
9
9
import expect from 'expect.js' ;
10
+ import { Collector } from '../collector' ;
10
11
import { CollectorSet } from '../collector_set' ;
11
12
12
13
const DEBUG_LOG = [ 'debug' , 'monitoring-ui' , 'kibana-monitoring' ] ;
@@ -17,52 +18,74 @@ const CHECK_DELAY = 100; // can be lower than COLLECTOR_INTERVAL because the col
17
18
18
19
describe ( 'CollectorSet' , ( ) => {
19
20
describe ( 'registers a collector set and runs lifecycle events' , ( ) => {
20
- let log ;
21
+ let server ;
21
22
let init ;
22
23
let cleanup ;
23
24
let fetch ;
24
25
beforeEach ( ( ) => {
25
- log = sinon . spy ( ) ;
26
+ server = {
27
+ log : sinon . spy ( )
28
+ } ;
26
29
init = noop ;
27
30
cleanup = noop ;
28
31
fetch = noop ;
29
32
} ) ;
30
33
34
+ it ( 'should throw an error if non-Collector type of object is registered' , ( ) => {
35
+ const collectors = new CollectorSet ( server , {
36
+ interval : COLLECTOR_INTERVAL ,
37
+ combineTypes : identity ,
38
+ onPayload : identity
39
+ } ) ;
40
+
41
+ const registerPojo = ( ) => {
42
+ collectors . register ( {
43
+ type : 'type_collector_test' ,
44
+ fetchAfterInit : true ,
45
+ init,
46
+ fetch,
47
+ cleanup
48
+ } ) ;
49
+ } ;
50
+
51
+ expect ( registerPojo ) . to . throwException ( ( { message } ) => {
52
+ expect ( message ) . to . be ( 'CollectorSet can only have Collector instances registered' ) ;
53
+ } ) ;
54
+ } ) ;
55
+
31
56
it ( 'should skip bulk upload if payload is empty' , ( done ) => {
32
- const collectors = new CollectorSet ( {
57
+ const collectors = new CollectorSet ( server , {
33
58
interval : COLLECTOR_INTERVAL ,
34
- logger : log ,
35
59
combineTypes : identity ,
36
60
onPayload : identity
37
61
} ) ;
38
62
39
- collectors . register ( {
63
+ collectors . register ( new Collector ( server , {
40
64
type : 'type_collector_test' ,
41
65
fetchAfterInit : true ,
42
66
init,
43
67
fetch,
44
68
cleanup
45
- } ) ;
69
+ } ) ) ;
46
70
47
71
collectors . start ( ) ;
48
72
49
73
// allow interval to tick a few times
50
74
setTimeout ( ( ) => {
51
75
collectors . cleanup ( ) ;
52
76
53
- expect ( log . calledWith ( INFO_LOG , 'Starting all stats collectors' ) ) . to . be ( true ) ;
54
- expect ( log . calledWith ( DEBUG_LOG , 'Initializing type_collector_test collector' ) ) . to . be ( true ) ;
55
- expect ( log . calledWith ( DEBUG_LOG , 'Fetching data from type_collector_test collector' ) ) . to . be ( true ) ;
56
- expect ( log . calledWith ( DEBUG_LOG , 'Skipping bulk uploading of an empty stats payload' ) ) . to . be ( true ) ; // proof of skip
57
- expect ( log . calledWith ( INFO_LOG , 'Stopping all stats collectors' ) ) . to . be ( true ) ;
58
- expect ( log . calledWith ( DEBUG_LOG , 'Running type_collector_test cleanup' ) ) . to . be ( true ) ;
77
+ expect ( server . log . calledWith ( INFO_LOG , 'Starting all stats collectors' ) ) . to . be ( true ) ;
78
+ expect ( server . log . calledWith ( DEBUG_LOG , 'Initializing type_collector_test collector' ) ) . to . be ( true ) ;
79
+ expect ( server . log . calledWith ( DEBUG_LOG , 'Fetching data from type_collector_test collector' ) ) . to . be ( true ) ;
80
+ expect ( server . log . calledWith ( DEBUG_LOG , 'Skipping bulk uploading of an empty stats payload' ) ) . to . be ( true ) ; // proof of skip
81
+ expect ( server . log . calledWith ( INFO_LOG , 'Stopping all stats collectors' ) ) . to . be ( true ) ;
82
+ expect ( server . log . calledWith ( DEBUG_LOG , 'Running type_collector_test cleanup' ) ) . to . be ( true ) ;
59
83
60
84
done ( ) ; // for async exit
61
85
} , CHECK_DELAY ) ;
62
86
} ) ;
63
87
64
88
it ( 'should run the bulk upload handler' , ( done ) => {
65
- const log = sinon . spy ( ) ;
66
89
const combineTypes = sinon . spy ( data => {
67
90
return [
68
91
data [ 0 ] [ 0 ] ,
@@ -71,34 +94,33 @@ describe('CollectorSet', () => {
71
94
} ) ;
72
95
const onPayload = sinon . spy ( ) ;
73
96
74
- const collectors = new CollectorSet ( {
97
+ const collectors = new CollectorSet ( server , {
75
98
interval : COLLECTOR_INTERVAL ,
76
- logger : log ,
77
99
combineTypes,
78
100
onPayload
79
101
} ) ;
80
102
81
103
fetch = ( ) => ( { testFetch : true } ) ;
82
- collectors . register ( {
104
+ collectors . register ( new Collector ( server , {
83
105
type : 'type_collector_test' ,
84
106
fetchAfterInit : true ,
85
107
init,
86
108
fetch,
87
109
cleanup
88
- } ) ;
110
+ } ) ) ;
89
111
90
112
collectors . start ( ) ;
91
113
92
114
// allow interval to tick a few times
93
115
setTimeout ( ( ) => {
94
116
collectors . cleanup ( ) ;
95
117
96
- expect ( log . calledWith ( INFO_LOG , 'Starting all stats collectors' ) ) . to . be ( true ) ;
97
- expect ( log . calledWith ( DEBUG_LOG , 'Initializing type_collector_test collector' ) ) . to . be ( true ) ;
98
- expect ( log . calledWith ( DEBUG_LOG , 'Fetching data from type_collector_test collector' ) ) . to . be ( true ) ;
99
- expect ( log . calledWith ( DEBUG_LOG , 'Uploading bulk stats payload to the local cluster' ) ) . to . be ( true ) ;
100
- expect ( log . calledWith ( INFO_LOG , 'Stopping all stats collectors' ) ) . to . be ( true ) ;
101
- expect ( log . calledWith ( DEBUG_LOG , 'Running type_collector_test cleanup' ) ) . to . be ( true ) ;
118
+ expect ( server . log . calledWith ( INFO_LOG , 'Starting all stats collectors' ) ) . to . be ( true ) ;
119
+ expect ( server . log . calledWith ( DEBUG_LOG , 'Initializing type_collector_test collector' ) ) . to . be ( true ) ;
120
+ expect ( server . log . calledWith ( DEBUG_LOG , 'Fetching data from type_collector_test collector' ) ) . to . be ( true ) ;
121
+ expect ( server . log . calledWith ( DEBUG_LOG , 'Uploading bulk stats payload to the local cluster' ) ) . to . be ( true ) ;
122
+ expect ( server . log . calledWith ( INFO_LOG , 'Stopping all stats collectors' ) ) . to . be ( true ) ;
123
+ expect ( server . log . calledWith ( DEBUG_LOG , 'Running type_collector_test cleanup' ) ) . to . be ( true ) ;
102
124
103
125
// un-flattened
104
126
expect ( combineTypes . getCall ( 0 ) . args [ 0 ] ) . to . eql (
@@ -115,29 +137,28 @@ describe('CollectorSet', () => {
115
137
} ) ;
116
138
117
139
it ( 'should log the info-level status of stopping and restarting' , ( done ) => {
118
- const collectors = new CollectorSet ( {
140
+ const collectors = new CollectorSet ( server , {
119
141
interval : COLLECTOR_INTERVAL ,
120
- logger : log ,
121
142
combineTypes : identity ,
122
143
onPayload : identity
123
144
} ) ;
124
145
125
- collectors . register ( {
146
+ collectors . register ( new Collector ( server , {
126
147
type : 'type_collector_test' ,
127
148
fetchAfterInit : true ,
128
149
init,
129
150
fetch,
130
151
cleanup
131
- } ) ;
152
+ } ) ) ;
132
153
133
154
collectors . start ( ) ;
134
- expect ( log . calledWith ( INFO_LOG , 'Starting all stats collectors' ) ) . to . be ( true ) ;
155
+ expect ( server . log . calledWith ( INFO_LOG , 'Starting all stats collectors' ) ) . to . be ( true ) ;
135
156
136
157
collectors . cleanup ( ) ;
137
- expect ( log . calledWith ( INFO_LOG , 'Stopping all stats collectors' ) ) . to . be ( true ) ;
158
+ expect ( server . log . calledWith ( INFO_LOG , 'Stopping all stats collectors' ) ) . to . be ( true ) ;
138
159
139
160
collectors . start ( ) ;
140
- expect ( log . calledWith ( INFO_LOG , 'Starting all stats collectors' ) ) . to . be ( true ) ;
161
+ expect ( server . log . calledWith ( INFO_LOG , 'Starting all stats collectors' ) ) . to . be ( true ) ;
141
162
142
163
// exit
143
164
collectors . cleanup ( ) ;
0 commit comments