@@ -4,10 +4,13 @@ const test = require('tap').test
4
4
5
5
const axios = require ( 'axios' )
6
6
const fastify = require ( 'fastify' )
7
- const BPromise = require ( 'bluebird' )
8
7
9
8
const plugin = require ( './index.js' )
10
9
10
+ function delay ( ms ) {
11
+ return new Promise ( ( resolve ) => setTimeout ( resolve , ms ) )
12
+ }
13
+
11
14
test ( 'should decorate cache to fastify instance' , ( t ) => {
12
15
t . plan ( 3 )
13
16
const instance = fastify ( )
@@ -30,7 +33,7 @@ test('should cache the cacheable request', (t) => {
30
33
instance . server . unref ( )
31
34
const portNum = instance . server . address ( ) . port
32
35
const address = `http://127.0.0.1:${ portNum } /cache`
33
- const [ response1 , response2 ] = await BPromise . all ( [
36
+ const [ response1 , response2 ] = await Promise . all ( [
34
37
axios . get ( address ) ,
35
38
axios . get ( address ) ,
36
39
] )
@@ -55,7 +58,7 @@ test('should not cache the uncacheable request', (t) => {
55
58
instance . server . unref ( )
56
59
const portNum = instance . server . address ( ) . port
57
60
const address = `http://127.0.0.1:${ portNum } /no-cache`
58
- const [ response1 , response2 ] = await BPromise . all ( [
61
+ const [ response1 , response2 ] = await Promise . all ( [
59
62
axios . post ( address , { } ) ,
60
63
axios . post ( address , { } ) ,
61
64
] )
@@ -80,11 +83,11 @@ test('should apply ttl config', (t) => {
80
83
instance . server . unref ( )
81
84
const portNum = instance . server . address ( ) . port
82
85
const address = `http://127.0.0.1:${ portNum } /ttl`
83
- const [ response1 , response2 ] = await BPromise . all ( [
86
+ const [ response1 , response2 ] = await Promise . all ( [
84
87
axios . get ( address ) ,
85
88
axios . get ( address ) ,
86
89
] )
87
- await BPromise . delay ( 3000 )
90
+ await delay ( 3000 )
88
91
const response3 = await axios . get ( address )
89
92
t . is ( response1 . status , 200 )
90
93
t . is ( response2 . status , 200 )
@@ -114,7 +117,7 @@ test('should apply additionalCondition config', (t) => {
114
117
instance . server . unref ( )
115
118
const portNum = instance . server . address ( ) . port
116
119
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 ( [
118
121
axios . get ( address , {
119
122
headers : { 'x-should-applied' : 'yes' } ,
120
123
} ) ,
@@ -146,15 +149,15 @@ test('should waiting for cache if multiple same request come in', (t) => {
146
149
const instance = fastify ( )
147
150
instance . register ( plugin , { ttl : 5000 } )
148
151
instance . get ( '/waiting' , async ( req , res ) => {
149
- await BPromise . delay ( 3000 )
152
+ await delay ( 3000 )
150
153
res . send ( { hello : 'world' } )
151
154
} )
152
155
instance . listen ( 0 , async ( err ) => {
153
156
if ( err ) t . threw ( err )
154
157
instance . server . unref ( )
155
158
const portNum = instance . server . address ( ) . port
156
159
const address = `http://127.0.0.1:${ portNum } /waiting`
157
- const [ response1 , response2 ] = await BPromise . all ( [
160
+ const [ response1 , response2 ] = await Promise . all ( [
158
161
axios . get ( address ) ,
159
162
axios . get ( address ) ,
160
163
] )
@@ -172,15 +175,15 @@ test('should not waiting for cache due to timeout', (t) => {
172
175
const instance = fastify ( )
173
176
instance . register ( plugin )
174
177
instance . get ( '/abort' , async ( req , res ) => {
175
- await BPromise . delay ( 2000 )
178
+ await delay ( 2000 )
176
179
res . send ( { hello : 'world' } )
177
180
} )
178
181
instance . listen ( 0 , async ( err ) => {
179
182
if ( err ) t . threw ( err )
180
183
instance . server . unref ( )
181
184
const portNum = instance . server . address ( ) . port
182
185
const address = `http://127.0.0.1:${ portNum } /abort`
183
- const [ response1 , response2 ] = await BPromise . all ( [
186
+ const [ response1 , response2 ] = await Promise . all ( [
184
187
axios . get ( address ) ,
185
188
axios . get ( address ) ,
186
189
] )
0 commit comments