@@ -45,6 +45,48 @@ describe_only(() => {
45
45
. then ( done ) ;
46
46
} ) ;
47
47
48
+ it ( 'should not store value for ttl=0' , done => {
49
+ const cache = new RedisCacheAdapter ( null , 5 ) ;
50
+
51
+ cache
52
+ . put ( KEY , VALUE , 0 )
53
+ . then ( ( ) => cache . get ( KEY ) )
54
+ . then ( value => expect ( value ) . toEqual ( null ) )
55
+ . then ( done ) ;
56
+ } ) ;
57
+
58
+ it ( 'should not expire when ttl=Infinity' , done => {
59
+ const cache = new RedisCacheAdapter ( null , 1 ) ;
60
+
61
+ cache
62
+ . put ( KEY , VALUE , Infinity )
63
+ . then ( ( ) => cache . get ( KEY ) )
64
+ . then ( value => expect ( value ) . toEqual ( VALUE ) )
65
+ . then ( wait . bind ( null , 1 ) )
66
+ . then ( ( ) => cache . get ( KEY ) )
67
+ . then ( value => expect ( value ) . toEqual ( VALUE ) )
68
+ . then ( done ) ;
69
+ } ) ;
70
+
71
+ it ( 'should fallback to default ttl' , done => {
72
+ const cache = new RedisCacheAdapter ( null , 1 ) ;
73
+ let promise = Promise . resolve ( ) ;
74
+
75
+ [ - 100 , null , undefined , 'not number' , true ] . forEach ( ttl => {
76
+ promise = promise . then ( ( ) =>
77
+ cache
78
+ . put ( KEY , VALUE , ttl )
79
+ . then ( ( ) => cache . get ( KEY ) )
80
+ . then ( value => expect ( value ) . toEqual ( VALUE ) )
81
+ . then ( wait . bind ( null , 2 ) )
82
+ . then ( ( ) => cache . get ( KEY ) )
83
+ . then ( value => expect ( value ) . toEqual ( null ) )
84
+ ) ;
85
+ } ) ;
86
+
87
+ promise . then ( done ) ;
88
+ } ) ;
89
+
48
90
it ( 'should find un-expired records' , done => {
49
91
const cache = new RedisCacheAdapter ( null , 5 ) ;
50
92
@@ -58,3 +100,66 @@ describe_only(() => {
58
100
. then ( done ) ;
59
101
} ) ;
60
102
} ) ;
103
+
104
+ describe_only ( ( ) => {
105
+ return process . env . PARSE_SERVER_TEST_CACHE === 'redis' ;
106
+ } ) ( 'RedisCacheAdapter/KeyPromiseQueue' , function ( ) {
107
+ const KEY1 = 'key1' ;
108
+ const KEY2 = 'key2' ;
109
+ const VALUE = 'hello' ;
110
+
111
+ // number of chained ops on a single key
112
+ function getQueueCountForKey ( cache , key ) {
113
+ return cache . queue . queue [ key ] [ 0 ] ;
114
+ }
115
+
116
+ // total number of queued keys
117
+ function getQueueCount ( cache ) {
118
+ return Object . keys ( cache . queue . queue ) . length ;
119
+ }
120
+
121
+ it ( 'it should clear completed operations from queue' , done => {
122
+ const cache = new RedisCacheAdapter ( { ttl : NaN } ) ;
123
+
124
+ // execute a bunch of operations in sequence
125
+ let promise = Promise . resolve ( ) ;
126
+ for ( let index = 1 ; index < 100 ; index ++ ) {
127
+ promise = promise . then ( ( ) => {
128
+ const key = `${ index } ` ;
129
+ return cache
130
+ . put ( key , VALUE )
131
+ . then ( ( ) => expect ( getQueueCount ( cache ) ) . toEqual ( 0 ) )
132
+ . then ( ( ) => cache . get ( key ) )
133
+ . then ( ( ) => expect ( getQueueCount ( cache ) ) . toEqual ( 0 ) )
134
+ . then ( ( ) => cache . clear ( ) )
135
+ . then ( ( ) => expect ( getQueueCount ( cache ) ) . toEqual ( 0 ) ) ;
136
+ } ) ;
137
+ }
138
+
139
+ // at the end the queue should be empty
140
+ promise . then ( ( ) => expect ( getQueueCount ( cache ) ) . toEqual ( 0 ) ) . then ( done ) ;
141
+ } ) ;
142
+
143
+ it ( 'it should count per key chained operations correctly' , done => {
144
+ const cache = new RedisCacheAdapter ( { ttl : NaN } ) ;
145
+
146
+ let key1Promise = Promise . resolve ( ) ;
147
+ let key2Promise = Promise . resolve ( ) ;
148
+ for ( let index = 1 ; index < 100 ; index ++ ) {
149
+ key1Promise = cache . put ( KEY1 , VALUE ) ;
150
+ key2Promise = cache . put ( KEY2 , VALUE ) ;
151
+ // per key chain should be equal to index, which is the
152
+ // total number of operations on that key
153
+ expect ( getQueueCountForKey ( cache , KEY1 ) ) . toEqual ( index ) ;
154
+ expect ( getQueueCountForKey ( cache , KEY2 ) ) . toEqual ( index ) ;
155
+ // the total keys counts should be equal to the different keys
156
+ // we have currently being processed.
157
+ expect ( getQueueCount ( cache ) ) . toEqual ( 2 ) ;
158
+ }
159
+
160
+ // at the end the queue should be empty
161
+ Promise . all ( [ key1Promise , key2Promise ] )
162
+ . then ( ( ) => expect ( getQueueCount ( cache ) ) . toEqual ( 0 ) )
163
+ . then ( done ) ;
164
+ } ) ;
165
+ } ) ;
0 commit comments