@@ -4,10 +4,13 @@ const test = require('tap').test
44
55const axios = require ( 'axios' )
66const fastify = require ( 'fastify' )
7- const BPromise = require ( 'bluebird' )
87
98const plugin = require ( './index.js' )
109
10+ function delay ( ms ) {
11+ return new Promise ( ( resolve ) => setTimeout ( resolve , ms ) )
12+ }
13+
1114test ( 'should decorate cache to fastify instance' , ( t ) => {
1215 t . plan ( 3 )
1316 const instance = fastify ( )
@@ -30,7 +33,7 @@ test('should cache the cacheable request', (t) => {
3033 instance . server . unref ( )
3134 const portNum = instance . server . address ( ) . port
3235 const address = `http://127.0.0.1:${ portNum } /cache`
33- const [ response1 , response2 ] = await BPromise . all ( [
36+ const [ response1 , response2 ] = await Promise . all ( [
3437 axios . get ( address ) ,
3538 axios . get ( address ) ,
3639 ] )
@@ -55,7 +58,7 @@ test('should not cache the uncacheable request', (t) => {
5558 instance . server . unref ( )
5659 const portNum = instance . server . address ( ) . port
5760 const address = `http://127.0.0.1:${ portNum } /no-cache`
58- const [ response1 , response2 ] = await BPromise . all ( [
61+ const [ response1 , response2 ] = await Promise . all ( [
5962 axios . post ( address , { } ) ,
6063 axios . post ( address , { } ) ,
6164 ] )
@@ -80,11 +83,11 @@ test('should apply ttl config', (t) => {
8083 instance . server . unref ( )
8184 const portNum = instance . server . address ( ) . port
8285 const address = `http://127.0.0.1:${ portNum } /ttl`
83- const [ response1 , response2 ] = await BPromise . all ( [
86+ const [ response1 , response2 ] = await Promise . all ( [
8487 axios . get ( address ) ,
8588 axios . get ( address ) ,
8689 ] )
87- await BPromise . delay ( 3000 )
90+ await delay ( 3000 )
8891 const response3 = await axios . get ( address )
8992 t . is ( response1 . status , 200 )
9093 t . is ( response2 . status , 200 )
@@ -114,7 +117,7 @@ test('should apply additionalCondition config', (t) => {
114117 instance . server . unref ( )
115118 const portNum = instance . server . address ( ) . port
116119 const address = `http://127.0.0.1:${ portNum } /headers`
117- const [ response1 , response2 , response3 , response4 ] = await BPromise . all ( [
120+ const [ response1 , response2 , response3 , response4 ] = await Promise . all ( [
118121 axios . get ( address , {
119122 headers : { 'x-should-applied' : 'yes' } ,
120123 } ) ,
@@ -146,15 +149,15 @@ test('should waiting for cache if multiple same request come in', (t) => {
146149 const instance = fastify ( )
147150 instance . register ( plugin , { ttl : 5000 } )
148151 instance . get ( '/waiting' , async ( req , res ) => {
149- await BPromise . delay ( 3000 )
152+ await delay ( 3000 )
150153 res . send ( { hello : 'world' } )
151154 } )
152155 instance . listen ( 0 , async ( err ) => {
153156 if ( err ) t . threw ( err )
154157 instance . server . unref ( )
155158 const portNum = instance . server . address ( ) . port
156159 const address = `http://127.0.0.1:${ portNum } /waiting`
157- const [ response1 , response2 ] = await BPromise . all ( [
160+ const [ response1 , response2 ] = await Promise . all ( [
158161 axios . get ( address ) ,
159162 axios . get ( address ) ,
160163 ] )
@@ -172,15 +175,15 @@ test('should not waiting for cache due to timeout', (t) => {
172175 const instance = fastify ( )
173176 instance . register ( plugin )
174177 instance . get ( '/abort' , async ( req , res ) => {
175- await BPromise . delay ( 2000 )
178+ await delay ( 2000 )
176179 res . send ( { hello : 'world' } )
177180 } )
178181 instance . listen ( 0 , async ( err ) => {
179182 if ( err ) t . threw ( err )
180183 instance . server . unref ( )
181184 const portNum = instance . server . address ( ) . port
182185 const address = `http://127.0.0.1:${ portNum } /abort`
183- const [ response1 , response2 ] = await BPromise . all ( [
186+ const [ response1 , response2 ] = await Promise . all ( [
184187 axios . get ( address ) ,
185188 axios . get ( address ) ,
186189 ] )
0 commit comments