@@ -2,84 +2,84 @@ import { BehaviorSubject, EmptyError, Subject, firstValueFrom, lastValueFrom, ta
2
2
import { InvalidStringError } from '@cardano-sdk/util' ;
3
3
import { RetryBackoffConfig , retryBackoff } from 'backoff-rxjs' ;
4
4
import { TestLogger , createLogger } from '@cardano-sdk/util-dev' ;
5
- import { coldObservableProvider } from '../src' ;
5
+ import { poll } from '../src' ;
6
6
7
7
// There might be a more elegant way to mock with original implementation (spy)
8
8
jest . mock ( 'backoff-rxjs' , ( ) => ( {
9
9
retryBackoff : jest . fn ( ) . mockImplementation ( ( ...args ) => jest . requireActual ( 'backoff-rxjs' ) . retryBackoff ( ...args ) )
10
10
} ) ) ;
11
11
12
- describe ( 'coldObservableProvider ' , ( ) => {
12
+ describe ( 'poll ' , ( ) => {
13
13
let logger : TestLogger ;
14
14
const testErrorStr = 'Test error' ;
15
15
16
16
beforeEach ( ( ) => {
17
17
logger = createLogger ( { record : true } ) ;
18
18
} ) ;
19
19
20
- it ( 'returns an observable that calls underlying provider on each subscription and uses retryBackoff' , async ( ) => {
21
- const underlyingProvider = jest . fn ( ) . mockResolvedValue ( true ) ;
20
+ it ( 'returns an observable that calls sample on each subscription and uses retryBackoff' , async ( ) => {
21
+ const sample = jest . fn ( ) . mockResolvedValue ( true ) ;
22
22
const backoffConfig : RetryBackoffConfig = { initialInterval : 1 } ;
23
- const provider $ = coldObservableProvider ( {
23
+ const values $ = poll ( {
24
24
logger,
25
- provider : underlyingProvider ,
26
- retryBackoffConfig : backoffConfig
25
+ retryBackoffConfig : backoffConfig ,
26
+ sample
27
27
} ) ;
28
- expect ( await firstValueFrom ( provider $) ) . toBe ( true ) ;
29
- expect ( await firstValueFrom ( provider $) ) . toBe ( true ) ;
30
- expect ( underlyingProvider ) . toBeCalledTimes ( 2 ) ;
28
+ expect ( await firstValueFrom ( values $) ) . toBe ( true ) ;
29
+ expect ( await firstValueFrom ( values $) ) . toBe ( true ) ;
30
+ expect ( sample ) . toBeCalledTimes ( 2 ) ;
31
31
expect ( retryBackoff ) . toBeCalledTimes ( 2 ) ;
32
32
} ) ;
33
33
34
- it ( 'provider is unsubscribed on cancel emit' , async ( ) => {
35
- const fakeProviderSubject = new Subject ( ) ;
36
- const underlyingProvider = ( ) => firstValueFrom ( fakeProviderSubject ) ;
34
+ it ( 'completes on cancel emit' , async ( ) => {
35
+ const fakeSampleSubject = new Subject ( ) ;
36
+ const sample = ( ) => firstValueFrom ( fakeSampleSubject ) ;
37
37
const backoffConfig : RetryBackoffConfig = { initialInterval : 1 } ;
38
38
const cancel$ = new BehaviorSubject < boolean > ( true ) ;
39
- const provider $ = coldObservableProvider ( {
39
+ const values $ = poll ( {
40
40
cancel$,
41
41
logger,
42
- provider : underlyingProvider ,
43
- retryBackoffConfig : backoffConfig
42
+ retryBackoffConfig : backoffConfig ,
43
+ sample
44
44
} ) ;
45
45
46
46
try {
47
- await firstValueFrom ( provider $) ;
47
+ await firstValueFrom ( values $) ;
48
48
} catch ( error ) {
49
49
expect ( error ) . toBeInstanceOf ( EmptyError ) ;
50
50
}
51
51
expect . assertions ( 1 ) ;
52
52
} ) ;
53
53
54
- it ( 'retries using retryBackoff, when underlying provider rejects' , async ( ) => {
55
- const underlyingProvider = jest . fn ( ) . mockRejectedValueOnce ( false ) . mockResolvedValue ( true ) ;
54
+ it ( 'retries using retryBackoff, when sample rejects' , async ( ) => {
55
+ const sample = jest . fn ( ) . mockRejectedValueOnce ( false ) . mockResolvedValue ( true ) ;
56
56
const retryBackoffConfig : RetryBackoffConfig = { initialInterval : 1 } ;
57
- const provider $ = coldObservableProvider ( { logger, provider : underlyingProvider , retryBackoffConfig } ) ;
58
- const resolvedValue = await firstValueFrom ( provider $) ;
59
- expect ( underlyingProvider ) . toBeCalledTimes ( 2 ) ;
57
+ const values $ = poll ( { logger, retryBackoffConfig , sample } ) ;
58
+ const resolvedValue = await firstValueFrom ( values $) ;
59
+ expect ( sample ) . toBeCalledTimes ( 2 ) ;
60
60
expect ( resolvedValue ) . toBeTruthy ( ) ;
61
61
} ) ;
62
62
63
- it ( 'does not retry, when underlying provider rejects with InvalidStringError' , async ( ) => {
63
+ it ( 'does not retry, when sample rejects with InvalidStringError' , async ( ) => {
64
64
const testValue = { test : 'value' } ;
65
65
const testError = new InvalidStringError ( 'Test invalid string error' ) ;
66
- const underlyingProvider = jest
66
+ const sample = jest
67
67
. fn ( )
68
68
. mockRejectedValueOnce ( new Error ( testErrorStr ) )
69
69
. mockResolvedValueOnce ( testValue )
70
70
. mockRejectedValueOnce ( testError )
71
71
. mockResolvedValueOnce ( testValue ) ;
72
72
const onFatalError = jest . fn ( ) ;
73
73
const retryBackoffConfig : RetryBackoffConfig = { initialInterval : 1 , shouldRetry : ( ) => true } ;
74
- const provider $ = coldObservableProvider ( {
74
+ const values $ = poll ( {
75
75
logger,
76
76
onFatalError,
77
- provider : underlyingProvider ,
78
- retryBackoffConfig
77
+ retryBackoffConfig ,
78
+ sample
79
79
} ) ;
80
- await expect ( firstValueFrom ( provider $) ) . resolves . toBe ( testValue ) ;
81
- await expect ( firstValueFrom ( provider $) ) . rejects . toThrow ( EmptyError ) ;
82
- expect ( underlyingProvider ) . toBeCalledTimes ( 3 ) ;
80
+ await expect ( firstValueFrom ( values $) ) . resolves . toBe ( testValue ) ;
81
+ await expect ( firstValueFrom ( values $) ) . rejects . toThrow ( EmptyError ) ;
82
+ expect ( sample ) . toBeCalledTimes ( 3 ) ;
83
83
expect ( onFatalError ) . toBeCalledWith ( testError ) ;
84
84
expect ( logger . messages ) . toStrictEqual ( [
85
85
{ level : 'error' , message : [ new Error ( testErrorStr ) ] } ,
@@ -89,45 +89,45 @@ describe('coldObservableProvider', () => {
89
89
] ) ;
90
90
} ) ;
91
91
92
- it ( 'polls the provider until the pollUntil condition is satisfied' , async ( ) => {
93
- const underlyingProvider = jest
92
+ it ( 'polls sample until the pollUntil condition is satisfied' , async ( ) => {
93
+ const sample = jest
94
94
. fn ( )
95
95
. mockResolvedValueOnce ( 'a' )
96
96
. mockResolvedValueOnce ( 'b' )
97
97
. mockResolvedValueOnce ( 'c' )
98
98
. mockResolvedValue ( 'Never reached' ) ;
99
99
const backoffConfig : RetryBackoffConfig = { initialInterval : 1 } ;
100
100
101
- const provider $ = coldObservableProvider ( {
101
+ const values $ = poll ( {
102
102
logger,
103
103
pollUntil : ( v ) => v === 'c' ,
104
- provider : underlyingProvider ,
105
- retryBackoffConfig : backoffConfig
104
+ retryBackoffConfig : backoffConfig ,
105
+ sample
106
106
} ) ;
107
107
108
- const providerValues : unknown [ ] = [ ] ;
109
- await lastValueFrom ( provider $. pipe ( tap ( ( v ) => providerValues . push ( v ) ) ) ) ;
110
- expect ( providerValues ) . toEqual ( [ 'a' , 'b' , 'c' ] ) ;
111
- expect ( underlyingProvider ) . toBeCalledTimes ( 3 ) ;
108
+ const sampleValues : unknown [ ] = [ ] ;
109
+ await lastValueFrom ( values $. pipe ( tap ( ( v ) => sampleValues . push ( v ) ) ) ) ;
110
+ expect ( sampleValues ) . toEqual ( [ 'a' , 'b' , 'c' ] ) ;
111
+ expect ( sample ) . toBeCalledTimes ( 3 ) ;
112
112
} ) ;
113
113
114
114
it ( 'stops retrying after maxRetries attempts and handles the error in catchError' , async ( ) => {
115
115
const testError = new Error ( testErrorStr ) ;
116
- const underlyingProvider = jest . fn ( ) . mockRejectedValue ( testError ) ;
116
+ const sample = jest . fn ( ) . mockRejectedValue ( testError ) ;
117
117
const maxRetries = 3 ;
118
118
const retryBackoffConfig : RetryBackoffConfig = { initialInterval : 1 , maxRetries } ;
119
119
const onFatalError = jest . fn ( ) ;
120
120
121
- const provider $ = coldObservableProvider ( {
121
+ const values $ = poll ( {
122
122
logger,
123
123
onFatalError,
124
- provider : underlyingProvider ,
125
- retryBackoffConfig
124
+ retryBackoffConfig ,
125
+ sample
126
126
} ) ;
127
127
128
- await expect ( firstValueFrom ( provider $) ) . rejects . toThrow ( testError ) ;
128
+ await expect ( firstValueFrom ( values $) ) . rejects . toThrow ( testError ) ;
129
129
130
- expect ( underlyingProvider ) . toBeCalledTimes ( maxRetries + 1 ) ;
130
+ expect ( sample ) . toBeCalledTimes ( maxRetries + 1 ) ;
131
131
expect ( onFatalError ) . toBeCalledWith ( expect . any ( Error ) ) ;
132
132
expect ( logger . messages ) . toStrictEqual ( [
133
133
{ level : 'error' , message : [ testError ] } ,
0 commit comments