Skip to content

Commit

Permalink
Additional test to make test various duration tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
pkukielka committed Jan 31, 2025
1 parent beb9d5f commit d325716
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
21 changes: 21 additions & 0 deletions lib/shared/src/misc/observable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1190,4 +1190,25 @@ describe('retry', () => {

expect(results).toEqual(['value', 'completed'])
})

test('should retry tasks with various duration', async () => {
vi.useFakeTimers()

let count = 0
const source = new Observable(observer => {
count++
const randomTimeout = Math.floor(Math.random() * 10) + 1
setTimeout(() => observer.error(new Error(`Test Error ${count}`)), randomTimeout)
})

const errors: Error[] = []
source.pipe(retry(100)).subscribe({
error: err => errors.push(err),
})

await vi.advanceTimersByTimeAsync(1000)

expect(errors.length).toBe(1)
expect(errors[0].message).toBe('Test Error 101')
})
})
19 changes: 10 additions & 9 deletions lib/shared/src/misc/observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1365,16 +1365,15 @@ export function retry<T>(count: number): (source: ObservableLike<T>) => Observab
function subscribe() {
subscription = source.subscribe({
next(value) {
observer.next(value)
retries = 0
if (subscription) {
observer.next(value)
retries = 0
}
},
error(err) {
if (retries < count && subscription) {
retries++
if (subscription) {
unsubscribe(subscription)
subscription = null
}
unsuscribeThis()
subscribe()
} else {
observer.error(err)
Expand All @@ -1388,13 +1387,15 @@ export function retry<T>(count: number): (source: ObservableLike<T>) => Observab
})
}

subscribe()

return () => {
function unsuscribeThis() {
if (subscription) {
unsubscribe(subscription)
subscription = undefined
}
}

subscribe()

return () => unsuscribeThis()
})
}

0 comments on commit d325716

Please sign in to comment.