1
1
const express = require ( 'express' ) ;
2
2
const request = require ( 'supertest' ) ;
3
3
const nock = require ( 'nock' ) ;
4
+ const crypto = require ( 'crypto' ) ;
4
5
const { isValidUUIDV4 } = require ( 'is-valid-uuid-v4' ) ;
5
6
const config = require ( '../config' ) ;
6
7
7
8
const middleware = require ( '..' ) ;
8
9
9
- const apiKey = 'OUW3RlI4gUCwWGpO10srIo2ufdWmMhMH' ;
10
+ const apiKey = 'fakeApiKey' ;
11
+ const group = '5afa21b97011c63320226ef3' ;
10
12
11
13
expect . extend ( {
12
14
toHaveLogHeader ( res ) {
@@ -50,8 +52,6 @@ describe('#metrics', () => {
50
52
} ) ;
51
53
52
54
it ( 'should send a request to the metrics server' , async function test ( ) {
53
- const group = '5afa21b97011c63320226ef3' ;
54
-
55
55
const mock = nock ( config . host )
56
56
. post ( '/v1/request' , ( [ body ] ) => {
57
57
expect ( body . group ) . toBe ( group ) ;
@@ -75,8 +75,6 @@ describe('#metrics', () => {
75
75
76
76
describe ( '#bufferLength' , ( ) => {
77
77
it ( 'should send requests when number hits `bufferLength` size' , async function test ( ) {
78
- const group = '5afa21b97011c63320226ef3' ;
79
-
80
78
const mock = nock ( config . host )
81
79
. post ( '/v1/request' , body => {
82
80
expect ( body ) . toHaveLength ( 3 ) ;
@@ -123,6 +121,47 @@ describe('#metrics', () => {
123
121
expect ( mock . isDone ( ) ) . toBe ( true ) ;
124
122
mock . done ( ) ;
125
123
} ) ;
124
+
125
+ it ( 'should clear out the queue when sent' , ( ) => {
126
+ const numberOfLogs = 20 ;
127
+ const numberOfMocks = 4 ;
128
+ const bufferLength = numberOfLogs / numberOfMocks ;
129
+
130
+ const seenLogs = [ ] ;
131
+
132
+ const mocks = [ ...new Array ( numberOfMocks ) . keys ( ) ] . map ( ( ) =>
133
+ nock ( config . host )
134
+ . post ( '/v1/request' , body => {
135
+ expect ( body ) . toHaveLength ( bufferLength ) ;
136
+
137
+ // Ensure that our executed requests and the buffered queue they're in remain unique.
138
+ body . forEach ( req => {
139
+ const requestHash = crypto . createHash ( 'md5' ) . update ( JSON . stringify ( req ) ) . digest ( 'hex' ) ;
140
+ expect ( seenLogs ) . not . toContain ( requestHash ) ;
141
+ seenLogs . push ( requestHash ) ;
142
+ } ) ;
143
+
144
+ return true ;
145
+ } )
146
+ // This is the important part of this test,
147
+ // the delay mimics the latency of a real
148
+ // HTTP request
149
+ . delay ( 1000 )
150
+ . reply ( 200 )
151
+ ) ;
152
+
153
+ const app = express ( ) ;
154
+ app . use ( middleware . metrics ( apiKey , ( ) => group , { bufferLength } ) ) ;
155
+ app . get ( '/test' , ( req , res ) => res . sendStatus ( 200 ) ) ;
156
+
157
+ return Promise . all (
158
+ [ ...new Array ( numberOfLogs ) . keys ( ) ] . map ( i => {
159
+ return request ( app ) . get ( `/test?log=${ i } ` ) . expect ( 200 ) ;
160
+ } )
161
+ ) . then ( ( ) => {
162
+ mocks . map ( mock => mock . done ( ) ) ;
163
+ } ) ;
164
+ } ) ;
126
165
} ) ;
127
166
128
167
describe ( '`res._body`' , ( ) => {
@@ -139,7 +178,7 @@ describe('#metrics', () => {
139
178
it ( 'should buffer up res.write() calls' , async function test ( ) {
140
179
const mock = createMock ( ) ;
141
180
const app = express ( ) ;
142
- app . use ( middleware . metrics ( apiKey , ( ) => '123' ) ) ;
181
+ app . use ( middleware . metrics ( apiKey , ( ) => group ) ) ;
143
182
app . get ( '/test' , ( req , res ) => {
144
183
res . write ( '{"a":1,' ) ;
145
184
res . write ( '"b":2,' ) ;
@@ -155,7 +194,7 @@ describe('#metrics', () => {
155
194
it ( 'should buffer up res.end() calls' , async function test ( ) {
156
195
const mock = createMock ( ) ;
157
196
const app = express ( ) ;
158
- app . use ( middleware . metrics ( apiKey , ( ) => '123' ) ) ;
197
+ app . use ( middleware . metrics ( apiKey , ( ) => group ) ) ;
159
198
app . get ( '/test' , ( req , res ) => res . end ( JSON . stringify ( responseBody ) ) ) ;
160
199
161
200
await request ( app )
@@ -169,7 +208,7 @@ describe('#metrics', () => {
169
208
it ( 'should work for res.send() calls' , async function test ( ) {
170
209
const mock = createMock ( ) ;
171
210
const app = express ( ) ;
172
- app . use ( middleware . metrics ( apiKey , ( ) => '123' ) ) ;
211
+ app . use ( middleware . metrics ( apiKey , ( ) => group ) ) ;
173
212
app . get ( '/test' , ( req , res ) => res . send ( responseBody ) ) ;
174
213
175
214
await request ( app )
0 commit comments