@@ -10,78 +10,88 @@ const latencyKey = `${prefix}.telemetry.latencies`;
1010const initKey = `${ prefix } .telemetry.init` ;
1111const fieldVersionablePrefix = `${ metadata . s } /${ metadata . n } /${ metadata . i } ` ;
1212
13- test ( 'TELEMETRY CACHE IN REDIS' , async ( ) => {
13+ describe ( 'TELEMETRY CACHE IN REDIS' , ( ) => {
14+ let connection : RedisAdapter ;
15+ let cache : TelemetryCacheInRedis ;
16+ let keysBuilder : KeyBuilderSS ;
1417
15- const keysBuilder = new KeyBuilderSS ( prefix , metadata ) ;
16- const connection = new RedisAdapter ( loggerMock ) ;
17- const cache = new TelemetryCacheInRedis ( loggerMock , keysBuilder , connection ) ;
18+ beforeEach ( async ( ) => {
19+ keysBuilder = new KeyBuilderSS ( prefix , metadata ) ;
20+ connection = new RedisAdapter ( loggerMock ) ;
21+ cache = new TelemetryCacheInRedis ( loggerMock , keysBuilder , connection ) ;
1822
19- // recordException
20- expect ( await cache . recordException ( 'tr' ) ) . toBe ( 1 ) ;
21- expect ( await cache . recordException ( 'tr' ) ) . toBe ( 2 ) ;
22- expect ( await cache . recordException ( 'tcfs' ) ) . toBe ( 1 ) ;
23+ await connection . del ( exceptionKey ) ;
24+ await connection . del ( latencyKey ) ;
25+ await connection . del ( initKey ) ;
26+ } ) ;
2327
24- expect ( await connection . hget ( exceptionKey , fieldVersionablePrefix + '/track' ) ) . toBe ( '2' ) ;
25- expect ( await connection . hget ( exceptionKey , fieldVersionablePrefix + '/treatment' ) ) . toBe ( null ) ;
26- expect ( await connection . hget ( exceptionKey , fieldVersionablePrefix + '/treatmentsWithConfigByFlagSets' ) ) . toBe ( '1' ) ;
28+ test ( 'TELEMETRY CACHE IN REDIS' , async ( ) => {
2729
28- // recordLatency
29- expect ( await cache . recordLatency ( 'tr' , 1.6 ) ) . toBe ( 1 ) ;
30- expect ( await cache . recordLatency ( 'tr' , 1.6 ) ) . toBe ( 2 ) ;
31- expect ( await cache . recordLatency ( 'tfs' , 1.6 ) ) . toBe ( 1 ) ;
30+ // recordException
31+ expect ( await cache . recordException ( 'tr' ) ) . toBe ( 1 ) ;
32+ expect ( await cache . recordException ( 'tr' ) ) . toBe ( 2 ) ;
33+ expect ( await cache . recordException ( 'tcfs' ) ) . toBe ( 1 ) ;
3234
33- expect ( await connection . hget ( latencyKey , fieldVersionablePrefix + '/track/2 ' ) ) . toBe ( '2' ) ;
34- expect ( await connection . hget ( latencyKey , fieldVersionablePrefix + '/treatment/2 ' ) ) . toBe ( null ) ;
35- expect ( await connection . hget ( latencyKey , fieldVersionablePrefix + '/treatmentsByFlagSets/2 ' ) ) . toBe ( '1' ) ;
35+ expect ( await connection . hget ( exceptionKey , fieldVersionablePrefix + '/track' ) ) . toBe ( '2' ) ;
36+ expect ( await connection . hget ( exceptionKey , fieldVersionablePrefix + '/treatment' ) ) . toBe ( null ) ;
37+ expect ( await connection . hget ( exceptionKey , fieldVersionablePrefix + '/treatmentsWithConfigByFlagSets ' ) ) . toBe ( '1' ) ;
3638
37- // recordConfig
38- expect ( await cache . recordConfig ( ) ) . toBe ( 1 ) ;
39- expect ( JSON . parse ( await connection . hget ( initKey , fieldVersionablePrefix ) as string ) ) . toEqual ( {
40- oM : 1 ,
41- st : 'redis' ,
42- aF : 0 ,
43- rF : 0
44- } ) ;
39+ // recordLatency
40+ expect ( await cache . recordLatency ( 'tr' , 1.6 ) ) . toBe ( 1 ) ;
41+ expect ( await cache . recordLatency ( 'tr' , 1.6 ) ) . toBe ( 2 ) ;
42+ expect ( await cache . recordLatency ( 'tfs' , 1.6 ) ) . toBe ( 1 ) ;
4543
46- // popLatencies
47- const latencies = await cache . popLatencies ( ) ;
48- latencies . forEach ( ( latency , m ) => {
49- expect ( JSON . parse ( m ) ) . toEqual ( metadata ) ;
50- expect ( latency ) . toEqual ( {
51- tfs : [ 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] ,
52- tr : [ 0 , 0 , 2 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] ,
53- } ) ;
54- } ) ;
55- expect ( await connection . hget ( latencyKey , fieldVersionablePrefix + '/track/2' ) ) . toBe ( null ) ;
56-
57- // popExceptions
58- const exceptions = await cache . popExceptions ( ) ;
59- exceptions . forEach ( ( exception , m ) => {
60- expect ( JSON . parse ( m ) ) . toEqual ( metadata ) ;
61- expect ( exception ) . toEqual ( {
62- tcfs : 1 ,
63- tr : 2 ,
64- } ) ;
65- } ) ;
66- expect ( await connection . hget ( exceptionKey , fieldVersionablePrefix + '/track' ) ) . toBe ( null ) ;
44+ expect ( await connection . hget ( latencyKey , fieldVersionablePrefix + '/track/2' ) ) . toBe ( '2' ) ;
45+ expect ( await connection . hget ( latencyKey , fieldVersionablePrefix + '/treatment/2' ) ) . toBe ( null ) ;
46+ expect ( await connection . hget ( latencyKey , fieldVersionablePrefix + '/treatmentsByFlagSets/2' ) ) . toBe ( '1' ) ;
6747
68- // popConfig
69- const configs = await cache . popConfigs ( ) ;
70- configs . forEach ( ( config , m ) => {
71- expect ( JSON . parse ( m ) ) . toEqual ( metadata ) ;
72- expect ( config ) . toEqual ( {
48+ // recordConfig
49+ expect ( await cache . recordConfig ( ) ) . toBe ( 1 ) ;
50+ expect ( JSON . parse ( await connection . hget ( initKey , fieldVersionablePrefix ) as string ) ) . toEqual ( {
7351 oM : 1 ,
7452 st : 'redis' ,
7553 aF : 0 ,
7654 rF : 0
7755 } ) ;
78- } ) ;
79- expect ( await connection . hget ( initKey , fieldVersionablePrefix ) ) . toBe ( null ) ;
8056
81- // pops when there is no data
82- expect ( ( await cache . popLatencies ( ) ) . size ) . toBe ( 0 ) ;
83- expect ( ( await cache . popExceptions ( ) ) . size ) . toBe ( 0 ) ;
84- expect ( ( await cache . popConfigs ( ) ) . size ) . toBe ( 0 ) ;
57+ // popLatencies
58+ const latencies = await cache . popLatencies ( ) ;
59+ latencies . forEach ( ( latency , m ) => {
60+ expect ( JSON . parse ( m ) ) . toEqual ( metadata ) ;
61+ expect ( latency ) . toEqual ( {
62+ tfs : [ 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] ,
63+ tr : [ 0 , 0 , 2 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] ,
64+ } ) ;
65+ } ) ;
66+ expect ( await connection . hget ( latencyKey , fieldVersionablePrefix + '/track/2' ) ) . toBe ( null ) ;
67+
68+ // popExceptions
69+ const exceptions = await cache . popExceptions ( ) ;
70+ exceptions . forEach ( ( exception , m ) => {
71+ expect ( JSON . parse ( m ) ) . toEqual ( metadata ) ;
72+ expect ( exception ) . toEqual ( {
73+ tcfs : 1 ,
74+ tr : 2 ,
75+ } ) ;
76+ } ) ;
77+ expect ( await connection . hget ( exceptionKey , fieldVersionablePrefix + '/track' ) ) . toBe ( null ) ;
8578
86- await connection . disconnect ( ) ;
79+ // popConfig
80+ const configs = await cache . popConfigs ( ) ;
81+ configs . forEach ( ( config , m ) => {
82+ expect ( JSON . parse ( m ) ) . toEqual ( metadata ) ;
83+ expect ( config ) . toEqual ( {
84+ oM : 1 ,
85+ st : 'redis' ,
86+ aF : 0 ,
87+ rF : 0
88+ } ) ;
89+ } ) ;
90+ expect ( await connection . hget ( initKey , fieldVersionablePrefix ) ) . toBe ( null ) ;
91+
92+ // pops when there is no data
93+ expect ( ( await cache . popLatencies ( ) ) . size ) . toBe ( 0 ) ;
94+ expect ( ( await cache . popExceptions ( ) ) . size ) . toBe ( 0 ) ;
95+ expect ( ( await cache . popConfigs ( ) ) . size ) . toBe ( 0 ) ;
96+ } ) ;
8797} ) ;
0 commit comments