@@ -46,27 +46,31 @@ describe("getSigningKey", () => {
4646 } ) ;
4747
4848 describe ( "caching" , ( ) => {
49- it ( "should return the same promise when called with the same date, region, service, and credentials" , ( ) => {
50- const promise1 = getSigningKey ( Sha256 , credentials , shortDate , region , service ) ;
51- const promise2 = getSigningKey ( Sha256 , credentials , shortDate , region , service ) ;
52- expect ( promise1 ) . toBe ( promise2 ) ;
49+ it ( "should return the same signing key when called with the same date, region, service, and credentials" , async ( ) => {
50+ const mockSha256Constructor = jest . fn ( ) . mockImplementation ( ( args ) => {
51+ return new Sha256 ( args ) ;
52+ } ) ;
53+ const key1 = await getSigningKey ( mockSha256Constructor , credentials , shortDate , region , service ) ;
54+ const key2 = await getSigningKey ( mockSha256Constructor , credentials , shortDate , region , service ) ;
55+ expect ( key1 ) . toBe ( key2 ) ;
56+ expect ( mockSha256Constructor ) . toHaveBeenCalledTimes ( 6 ) ;
5357 } ) ;
5458
55- it ( "should cache a maximum of 50 entries" , ( ) => {
56- const keyPromises : Array < Promise < Uint8Array > > = new Array ( 50 ) ;
59+ it ( "should cache a maximum of 50 entries" , async ( ) => {
60+ const keys : Array < Uint8Array > = new Array ( 50 ) ;
5761 // fill the cache
5862 for ( let i = 0 ; i < 50 ; i ++ ) {
59- keyPromises [ i ] = getSigningKey ( Sha256 , credentials , shortDate , `us-foo-${ i . toString ( 10 ) } ` , service ) ;
63+ keys [ i ] = await getSigningKey ( Sha256 , credentials , shortDate , `us-foo-${ i . toString ( 10 ) } ` , service ) ;
6064 }
6165
6266 // evict the oldest member from the cache
63- getSigningKey ( Sha256 , credentials , shortDate , `us-foo-50` , service ) ;
67+ await getSigningKey ( Sha256 , credentials , shortDate , `us-foo-50` , service ) ;
6468
6569 // the second oldest member should still be in cache
66- expect ( keyPromises [ 1 ] ) . toBe ( getSigningKey ( Sha256 , credentials , shortDate , `us-foo-1` , service ) ) ;
70+ await expect ( getSigningKey ( Sha256 , credentials , shortDate , `us-foo-1` , service ) ) . resolves . toStrictEqual ( keys [ 1 ] ) ;
6771
6872 // the oldest member should not be in the cache
69- expect ( keyPromises [ 0 ] ) . not . toBe ( getSigningKey ( Sha256 , credentials , shortDate , `us-foo-0` , service ) ) ;
73+ await expect ( getSigningKey ( Sha256 , credentials , shortDate , `us-foo-0` , service ) ) . resolves . not . toBe ( keys [ 0 ] ) ;
7074 } ) ;
7175 } ) ;
7276} ) ;
0 commit comments