-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #209.
- Loading branch information
Showing
8 changed files
with
217 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { ExecutedFn } from '../types/executedFn'; | ||
import { Policy } from './policy'; | ||
|
||
export class NopPolicy<ResultType> extends Policy<ResultType> { | ||
protected async policyExecutorImpl(fn: ExecutedFn<ResultType>): Promise<ResultType> { | ||
return fn(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Policy } from './policy'; | ||
|
||
export class PolicyCombination { | ||
public static combine<ResultType>( | ||
policies: [Policy<ResultType>, Policy<ResultType>, ...Array<Policy<ResultType>>], | ||
): Policy<ResultType> { | ||
return policies.reduceRight( | ||
(prev, next): Policy<ResultType> => { | ||
next.wrap(prev); | ||
return next; | ||
}, | ||
); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import { expect } from 'chai'; | ||
import { PolicyCombination } from '../../src/policies/policyCombination'; | ||
import { TimeoutException } from '../../src/policies/proactive/timeoutPolicy/timeoutException'; | ||
import { TimeoutPolicy } from '../../src/policies/proactive/timeoutPolicy/timeoutPolicy'; | ||
import { FallbackPolicy } from '../../src/policies/reactive/fallbackPolicy/fallbackPolicy'; | ||
import { RetryPolicy } from '../../src/policies/reactive/retryPolicy/retryPolicy'; | ||
|
||
describe('PolicyCombination', (): void => { | ||
it('should run the wrapped policy inside the wrapper policy (wrapped with singular wrapping)', async (): Promise< | ||
void | ||
> => { | ||
let onRetryExecuted = 0; | ||
let onFallbackExecuted = 0; | ||
|
||
const fallbackPolicy = new FallbackPolicy<boolean>(); | ||
fallbackPolicy.reactOnResult((r): boolean => r); | ||
fallbackPolicy.fallback((): boolean => { | ||
return false; | ||
}); | ||
fallbackPolicy.onFallback((): void => { | ||
expect(onRetryExecuted).to.equal(1); | ||
onFallbackExecuted++; | ||
}); | ||
|
||
const retryPolicy = new RetryPolicy<boolean>(); | ||
retryPolicy.reactOnResult((r): boolean => r); | ||
retryPolicy.onRetry((): void => { | ||
expect(onFallbackExecuted).to.equal(0); | ||
onRetryExecuted++; | ||
}); | ||
|
||
fallbackPolicy.wrap(retryPolicy); | ||
|
||
await fallbackPolicy.execute((): boolean => { | ||
return true; | ||
}); | ||
|
||
expect(onRetryExecuted).to.equal(1); | ||
expect(onFallbackExecuted).to.equal(1); | ||
}); | ||
|
||
it('should run the wrapped policy inside the wrapper policy (combined with PolicyCombination)', async (): Promise< | ||
void | ||
> => { | ||
let onRetryExecuted = 0; | ||
let onFallbackExecuted = 0; | ||
|
||
const fallbackPolicy = new FallbackPolicy<boolean>(); | ||
fallbackPolicy.reactOnResult((r): boolean => r); | ||
fallbackPolicy.fallback((): boolean => { | ||
return false; | ||
}); | ||
fallbackPolicy.onFallback((): void => { | ||
expect(onRetryExecuted).to.equal(1); | ||
onFallbackExecuted++; | ||
}); | ||
|
||
const retryPolicy = new RetryPolicy<boolean>(); | ||
retryPolicy.reactOnResult((r): boolean => r); | ||
retryPolicy.onRetry((): void => { | ||
expect(onFallbackExecuted).to.equal(0); | ||
onRetryExecuted++; | ||
}); | ||
|
||
const wrappedPolicy = PolicyCombination.combine<boolean>([fallbackPolicy, retryPolicy]); | ||
await wrappedPolicy.execute((): boolean => { | ||
return true; | ||
}); | ||
|
||
expect(onRetryExecuted).to.equal(1); | ||
expect(onFallbackExecuted).to.equal(1); | ||
}); | ||
|
||
it('should construct a policy which combines the other policies sequentially', async (): Promise<void> => { | ||
let onTimeoutExecuted = 0; | ||
let onRetryExecuted = 0; | ||
let onFallbackExecuted = 0; | ||
|
||
const fallbackPolicy = new FallbackPolicy<void>(); | ||
fallbackPolicy.reactOnException((e): boolean => e instanceof TimeoutException); | ||
fallbackPolicy.fallback((): void => { | ||
// empty | ||
}); | ||
fallbackPolicy.onFallback((): void => { | ||
expect(onTimeoutExecuted).to.equal(1); | ||
expect(onRetryExecuted).to.equal(1); | ||
expect(onFallbackExecuted).to.equal(0); | ||
onFallbackExecuted++; | ||
}); | ||
|
||
const retryPolicy = new RetryPolicy<void>(); | ||
retryPolicy.reactOnException((e): boolean => e instanceof TimeoutException); | ||
retryPolicy.onRetry((): void => { | ||
expect(onTimeoutExecuted).to.equal(1); | ||
expect(onRetryExecuted).to.equal(0); | ||
expect(onFallbackExecuted).to.equal(0); | ||
onRetryExecuted++; | ||
}); | ||
|
||
const timeoutPolicy = new TimeoutPolicy<void>(); | ||
timeoutPolicy.timeoutAfter(1); | ||
timeoutPolicy.onTimeout((): void => { | ||
expect(onTimeoutExecuted).to.equal(0); | ||
expect(onRetryExecuted).to.equal(0); | ||
expect(onFallbackExecuted).to.equal(0); | ||
onTimeoutExecuted++; | ||
}); | ||
|
||
const wrappedPolicies = PolicyCombination.combine<void>([fallbackPolicy, retryPolicy, timeoutPolicy]); | ||
await wrappedPolicies.execute( | ||
async (): Promise<void> => { | ||
return new Promise((resolve): void => { | ||
setTimeout(resolve, 5); | ||
}); | ||
}, | ||
); | ||
|
||
expect(onTimeoutExecuted).to.equal(1); | ||
expect(onRetryExecuted).to.equal(1); | ||
expect(onFallbackExecuted).to.equal(1); | ||
}); | ||
}); |