This repository was archived by the owner on Feb 13, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 6 files changed +733
-457
lines changed Expand file tree Collapse file tree 6 files changed +733
-457
lines changed Original file line number Diff line number Diff line change @@ -5,7 +5,6 @@ workflows:
5
5
- circle-lint
6
6
multi-test :
7
7
jobs :
8
- - test-node8
9
8
- test-node10
10
9
- test-node12
11
10
test_and_publish :
@@ -43,7 +42,6 @@ workflows:
43
42
- master
44
43
- scheduled_e2e_testing
45
44
jobs :
46
- - test-node8
47
45
- test-node10
48
46
- test-node12
49
47
74
72
root : .
75
73
paths : [.]
76
74
77
- test-node8 :
78
- << : *node-base-test
79
- docker :
80
- - image : circleci/node:8-browsers
81
75
test-node10 :
82
76
<< : *node-base-test
83
77
docker :
Original file line number Diff line number Diff line change @@ -45,6 +45,7 @@ class Analytics {
45
45
this . axiosInstance = axiosInstance
46
46
this . timeout = options . timeout || false
47
47
this . flushAt = Math . max ( options . flushAt , 1 ) || 20
48
+ this . maxQueueSize = options . maxQueueSize || 1024 * 500 // defaults to 500kb
48
49
this . flushInterval = options . flushInterval || 10000
49
50
this . flushed = false
50
51
Object . defineProperty ( this , 'enable' , {
@@ -208,7 +209,9 @@ class Analytics {
208
209
return
209
210
}
210
211
211
- if ( this . queue . length >= this . flushAt ) {
212
+ const hasReachedFlushAt = this . queue . length >= this . flushAt
213
+ const hasReachedQueueSize = this . queue . reduce ( ( acc , item ) => acc + JSON . stringify ( item ) . length , 0 ) >= this . maxQueueSize
214
+ if ( hasReachedFlushAt || hasReachedQueueSize ) {
212
215
this . flush ( callback )
213
216
}
214
217
Original file line number Diff line number Diff line change 16
16
"circle-lint" : " .buildscript/circle.sh" ,
17
17
"dependencies" : " yarn" ,
18
18
"test" : " standard && nyc ava --timeout=20s&& .buildscript/e2e.sh" ,
19
+ "coverage" : " nyc npm run test" ,
19
20
"report-coverage" : " nyc report --reporter=lcov > coverage.lcov && codecov" ,
20
21
"np" : " np --no-publish" ,
21
22
"release" : " yarn run np"
46
47
"ava" : " ^0.25.0" ,
47
48
"basic-auth" : " ^2.0.1" ,
48
49
"body-parser" : " ^1.17.1" ,
49
- "codecov" : " ^3.0.0 " ,
50
+ "codecov" : " ^3.8.1 " ,
50
51
"commander" : " ^2.9.0" ,
51
52
"delay" : " ^4.2.0" ,
52
53
"express" : " ^4.15.2" ,
53
54
"husky" : " ^3.0.4" ,
54
- "nyc" : " ^14 .1.1 " ,
55
+ "nyc" : " ^15 .1.0 " ,
55
56
"pify" : " ^4.0.1" ,
56
57
"sinon" : " ^7.3.2" ,
57
58
"snyk" : " ^1.171.1" ,
Original file line number Diff line number Diff line change
1
+ const os = require ( 'os' )
2
+ const uuid = require ( 'uuid' )
3
+ const Analytics = require ( '.' )
4
+ const analytics = new Analytics ( 'xemyw6oe3n' )
5
+
6
+ for ( let i = 0 ; i < 10 ; i ++ ) {
7
+ for ( let j = 0 ; j < 10 ; j ++ ) {
8
+ analytics . track ( {
9
+ anonymousId : uuid . v4 ( ) ,
10
+ userId : os . userInfo ( ) . username ,
11
+ event : 'Node Test' ,
12
+ properties : {
13
+ count : i + j
14
+ }
15
+ } )
16
+ }
17
+ }
18
+
19
+ analytics . flush ( )
Original file line number Diff line number Diff line change @@ -375,6 +375,38 @@ test('flush - skip when client is disabled', async t => {
375
375
t . false ( callback . called )
376
376
} )
377
377
378
+ test ( 'flush - flush when reaches max payload size' , async t => {
379
+ const client = createClient ( { flushAt : 1000 } )
380
+ client . flush = spy ( )
381
+
382
+ // each of these messages when stringified to json has 220-ish bytes
383
+ // to satisfy our default limit of 1024*500 bytes we need less than 2600 of those messages
384
+ const event = {
385
+ userId : 1 ,
386
+ event : 'event'
387
+ }
388
+ for ( let i = 0 ; i < 2600 ; i ++ ) {
389
+ client . track ( event )
390
+ }
391
+
392
+ t . true ( client . flush . called )
393
+ } )
394
+
395
+ test ( 'flush - wont flush when no flush condition has meet' , async t => {
396
+ const client = createClient ( { flushAt : 1000 , maxQueueSize : 1024 * 1000 } )
397
+ client . flush = spy ( )
398
+
399
+ const event = {
400
+ userId : 1 ,
401
+ event : 'event'
402
+ }
403
+ for ( let i = 0 ; i < 150 ; i ++ ) {
404
+ client . track ( event )
405
+ }
406
+
407
+ t . false ( client . flush . called )
408
+ } )
409
+
378
410
test ( 'identify - enqueue a message' , t => {
379
411
const client = createClient ( )
380
412
stub ( client , 'enqueue' )
You can’t perform that action at this time.
0 commit comments