@@ -28,6 +28,9 @@ describe(EndpointCache.name, () => {
28
28
Expires : now + CachePeriodInMinutes * 60 * 1000 ,
29
29
} ) ) ;
30
30
31
+ const getMaxCachePeriodInMins = ( endpoints : Endpoint [ ] ) =>
32
+ Math . max ( ...endpoints . map ( ( endpoint ) => endpoint . CachePeriodInMinutes ) ) ;
33
+
31
34
beforeEach ( ( ) => {
32
35
( ( LRUCache as unknown ) as jest . Mock ) . mockReturnValueOnce ( {
33
36
set,
@@ -57,41 +60,71 @@ describe(EndpointCache.name, () => {
57
60
jest . spyOn ( Date , "now" ) . mockImplementation ( ( ) => now ) ;
58
61
} ) ;
59
62
60
- describe ( "returns undefined" , ( ) => {
61
- it ( "if cache doesn't have key" , ( ) => {
62
- has . mockReturnValueOnce ( false ) ;
63
- expect ( endpointCache . get ( key ) ) . toBeUndefined ( ) ;
64
- expect ( has ) . toHaveBeenCalledWith ( key ) ;
65
- expect ( peek ) . not . toHaveBeenCalled ( ) ;
66
- expect ( get ) . not . toHaveBeenCalled ( ) ;
67
- expect ( set ) . not . toHaveBeenCalled ( ) ;
68
- } ) ;
63
+ const verifyHasAndGetCalls = ( ) => {
64
+ expect ( has ) . toHaveBeenCalledTimes ( 1 ) ;
65
+ expect ( has ) . toHaveBeenCalledWith ( key ) ;
66
+ expect ( get ) . toHaveBeenCalledTimes ( 1 ) ;
67
+ expect ( get ) . toHaveBeenCalledWith ( key ) ;
68
+ } ;
69
69
70
- it ( "if cache has empty array" , ( ) => {
71
- peek . mockReturnValueOnce ( [ ] ) ;
72
- expect ( endpointCache . get ( key ) ) . toBeUndefined ( ) ;
73
- expect ( has ) . toHaveBeenCalledWith ( key ) ;
74
- expect ( peek ) . toHaveBeenCalledWith ( key ) ;
75
- expect ( get ) . not . toHaveBeenCalled ( ) ;
70
+ it ( "returns undefined if cache doesn't have key" , ( ) => {
71
+ has . mockReturnValueOnce ( false ) ;
72
+ expect ( endpointCache . get ( key ) ) . toBeUndefined ( ) ;
73
+ expect ( has ) . toHaveBeenCalledTimes ( 1 ) ;
74
+ expect ( has ) . toHaveBeenCalledWith ( key ) ;
75
+ expect ( peek ) . not . toHaveBeenCalled ( ) ;
76
+ expect ( get ) . not . toHaveBeenCalled ( ) ;
77
+ } ) ;
78
+
79
+ it ( "returns undefined if cache has empty array" , ( ) => {
80
+ has . mockReturnValueOnce ( true ) ;
81
+ peek . mockReturnValueOnce ( [ ] ) ;
82
+ expect ( endpointCache . get ( key ) ) . toBeUndefined ( ) ;
83
+ expect ( has ) . toHaveBeenCalledTimes ( 1 ) ;
84
+ expect ( has ) . toHaveBeenCalledWith ( key ) ;
85
+ expect ( peek ) . toHaveBeenCalledTimes ( 1 ) ;
86
+ expect ( peek ) . toHaveBeenCalledWith ( key ) ;
87
+ expect ( get ) . not . toHaveBeenCalled ( ) ;
88
+ } ) ;
89
+
90
+ it ( "returns undefined if cache returns undefined for key" , ( ) => {
91
+ get . mockReturnValueOnce ( undefined ) ;
92
+ expect ( endpointCache . get ( key ) ) . toBeUndefined ( ) ;
93
+ verifyHasAndGetCalls ( ) ;
94
+ expect ( set ) . not . toHaveBeenCalled ( ) ;
95
+ } ) ;
96
+
97
+ it ( "returns undefined if endpoints have expired" , ( ) => {
98
+ const maxCachePeriod = getMaxCachePeriodInMins ( mockEndpoints ) ;
99
+ jest . spyOn ( Date , "now" ) . mockImplementation ( ( ) => now + ( maxCachePeriod + 1 ) * 60 * 1000 ) ;
100
+ expect ( endpointCache . get ( key ) ) . toBeUndefined ( ) ;
101
+ verifyHasAndGetCalls ( ) ;
102
+ expect ( set ) . toHaveBeenCalledTimes ( 1 ) ;
103
+ expect ( set ) . toHaveBeenCalledWith ( key , [ ] ) ;
104
+ } ) ;
105
+
106
+ describe ( "getEndpoint" , ( ) => {
107
+ it ( "returns one of the un-expired endpoints" , ( ) => {
108
+ expect ( mockEndpoints . map ( ( endpoint ) => endpoint . Address ) ) . toContain ( endpointCache . getEndpoint ( key ) ) ;
109
+ verifyHasAndGetCalls ( ) ;
76
110
expect ( set ) . not . toHaveBeenCalled ( ) ;
77
111
} ) ;
78
112
79
- it ( "if cache returns undefined for key" , ( ) => {
80
- get . mockReturnValueOnce ( undefined ) ;
81
- expect ( endpointCache . get ( key ) ) . toBeUndefined ( ) ;
82
- expect ( has ) . toHaveBeenCalledWith ( key ) ;
83
- expect ( peek ) . toHaveBeenCalledWith ( key ) ;
84
- expect ( get ) . toHaveBeenCalledWith ( key ) ;
113
+ it ( "returns un-expired endpoint" , ( ) => {
114
+ jest . spyOn ( Date , "now" ) . mockImplementation ( ( ) => now + 90 * 1000 ) ;
115
+ expect ( endpointCache . getEndpoint ( key ) ) . toEqual ( mockEndpoints [ 1 ] . Address ) ;
116
+ verifyHasAndGetCalls ( ) ;
85
117
expect ( set ) . not . toHaveBeenCalled ( ) ;
86
118
} ) ;
87
- } ) ;
88
119
89
- it ( "returns endpoints if available" , ( ) => {
90
- expect ( endpointCache . get ( key ) ) . toEqual ( getEndpointsWithExpiry ( mockEndpoints ) ) ;
91
- expect ( has ) . toHaveBeenCalledWith ( key ) ;
92
- expect ( peek ) . toHaveBeenCalledWith ( key ) ;
93
- expect ( get ) . toHaveBeenCalledWith ( key ) ;
94
- expect ( set ) . not . toHaveBeenCalled ( ) ;
120
+ [ 0 , 1 ] . forEach ( ( index ) => {
121
+ it ( `returns un-expired endpoint at index ${ index } ` , ( ) => {
122
+ jest . spyOn ( Math , "floor" ) . mockImplementation ( ( ) => index ) ;
123
+ expect ( mockEndpoints . map ( ( endpoint ) => endpoint . Address ) ) . toContain ( endpointCache . getEndpoint ( key ) ) ;
124
+ verifyHasAndGetCalls ( ) ;
125
+ expect ( set ) . not . toHaveBeenCalled ( ) ;
126
+ } ) ;
127
+ } ) ;
95
128
} ) ;
96
129
} ) ;
97
130
0 commit comments