@@ -46,27 +46,31 @@ describe("getSigningKey", () => {
46
46
} ) ;
47
47
48
48
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 ) ;
53
57
} ) ;
54
58
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 ) ;
57
61
// fill the cache
58
62
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 ) ;
60
64
}
61
65
62
66
// 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 ) ;
64
68
65
69
// 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 ] ) ;
67
71
68
72
// 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 ] ) ;
70
74
} ) ;
71
75
} ) ;
72
76
} ) ;
0 commit comments