This repository was archived by the owner on Oct 13, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +57
-0
lines changed
Expand file tree Collapse file tree 3 files changed +57
-0
lines changed Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ export default function redisClient() { // eslint-disable-line no-unused-vars
88 const redisOptions = Object . assign ( { } , this . get ( 'redis' ) , {
99 retry_strategy : function ( options ) { // eslint-disable-line camelcase
1010 app . set ( 'redisClient' , undefined ) ;
11+ /* istanbul ignore next */
1112 if ( cacheOptions . env !== 'test' ) {
1213 console . log ( `${ chalk . yellow ( '[redis]' ) } not connected` ) ;
1314 }
@@ -16,8 +17,11 @@ export default function redisClient() { // eslint-disable-line no-unused-vars
1617 } ) ;
1718 const client = redis . createClient ( redisOptions ) ;
1819
20+ app . set ( 'redisClient' , client ) ;
21+
1922 client . on ( 'ready' , ( ) => {
2023 app . set ( 'redisClient' , client ) ;
24+ /* istanbul ignore next */
2125 if ( cacheOptions . env !== 'test' ) {
2226 console . log ( `${ chalk . green ( '[redis]' ) } connected` ) ;
2327 }
Original file line number Diff line number Diff line change @@ -666,6 +666,41 @@ describe('Redis After Hook', () => {
666666 } ) ;
667667 } ) ;
668668
669+ it ( 'does not save anything in cache if redisClient offline' , ( ) => {
670+ const hook = a ( ) ;
671+ const clientDummy = {
672+ keys : [ ] ,
673+ set : function ( key , val ) {
674+ this . keys . push ( key ) ;
675+ } ,
676+ expire : function ( ) { } ,
677+ rpush : function ( ) { } ,
678+ } ;
679+ const mock = {
680+ params : { query : '' } ,
681+ path : 'dummy' ,
682+ id : 'do-not-save' ,
683+ result : {
684+ cache : { }
685+ } ,
686+ app : {
687+ get : ( what ) => {
688+ // comment in to emulate redisClient online (will cause test fail)
689+ // if (what === 'redisClient') {
690+ // return clientDummy;
691+ // }
692+ }
693+ }
694+ } ;
695+ const prevKeys = clientDummy . keys . join ( ) ;
696+
697+ return hook ( mock ) . then ( result => {
698+ const currKeys = clientDummy . keys . join ( ) ;
699+
700+ expect ( currKeys ) . to . equal ( prevKeys ) ;
701+ } ) ;
702+ } ) ;
703+
669704 // after(() => {
670705 // client.del('parent');
671706 // client.del('parent?full=true');
Original file line number Diff line number Diff line change @@ -240,6 +240,24 @@ describe('Redis Before Hook', () => {
240240 } ) ;
241241 } ) ;
242242
243+ it ( 'does not return any result if redisClient offline' , ( ) => {
244+ const hook = b ( ) ;
245+ const mock = {
246+ params : { query : '' } ,
247+ path : '' ,
248+ id : '' ,
249+ app : {
250+ get : ( what ) => { }
251+ }
252+ } ;
253+
254+ return hook ( mock ) . then ( result => {
255+ const data = result . result ;
256+
257+ expect ( data ) . to . be . undefined ;
258+ } ) ;
259+ } ) ;
260+
243261 after ( ( ) => {
244262 client . del ( 'before-test-route' ) ;
245263 client . del ( 'before-test-route?full=true' ) ;
You can’t perform that action at this time.
0 commit comments