This repository has been archived by the owner on Mar 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HARP-8560 Fix for missing tile data bug.
* Don't remove tile loaders from cache that are still in use. * Refactoring in TransferManager. * Added more unit tests. Signed-off-by: Marcel Pursche <marcel.pursche@here.com>
- Loading branch information
Showing
7 changed files
with
187 additions
and
61 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
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,40 @@ | ||
/* | ||
* Copyright (C) 2017-2019 HERE Europe B.V. | ||
* Licensed under Apache 2.0, see full license in LICENSE | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
/** | ||
* | ||
* DeferredPromise takes an executor function for executing it later, when [[exec]] is called. | ||
* This class allows wrapping other promises or long running functions for later execution. | ||
* @internal | ||
*/ | ||
export class DeferredPromise<T> { | ||
/** | ||
* Internal promise to store the result of the deferred executor function. | ||
*/ | ||
readonly promise: Promise<T>; | ||
private resolveFunc?: (result?: T) => void; | ||
private rejectFunc?: (reason?: any) => void; | ||
|
||
/** | ||
* Constructs a new [[DeferredPromise]] | ||
* @param executor Async function that should be executed at a later point in time. | ||
*/ | ||
constructor(private readonly executor: () => Promise<T>) { | ||
this.promise = new Promise<T>((resolve, reject) => { | ||
this.resolveFunc = resolve; | ||
this.rejectFunc = reject; | ||
}); | ||
} | ||
|
||
/** | ||
* When `exec` is called the deferred executor function is executed. | ||
*/ | ||
exec() { | ||
this.executor() | ||
.then(result => this.resolveFunc!(result)) | ||
.catch(error => this.rejectFunc!(error)); | ||
} | ||
} |
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,68 @@ | ||
/* | ||
* Copyright (C) 2017-2019 HERE Europe B.V. | ||
* Licensed under Apache 2.0, see full license in LICENSE | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
// tslint:disable:completed-docs | ||
// tslint:disable:only-arrow-functions | ||
// Mocha discourages using arrow functions, see https://mochajs.org/#arrow-functions | ||
|
||
import * as chai from "chai"; | ||
import * as chaiAsPromised from "chai-as-promised"; | ||
chai.use(chaiAsPromised); | ||
const { expect } = chai; | ||
|
||
import { DeferredPromise } from "../src/DeferredPromise"; | ||
|
||
function delay(timeout: number) { | ||
return new Promise(resolve => { | ||
setTimeout(resolve, timeout); | ||
}); | ||
} | ||
|
||
describe("DeferredPromise", function() { | ||
it("does not settle Promise unless exec() was called.", async function() { | ||
let isSettled = false; | ||
const deferred = new DeferredPromise(() => { | ||
isSettled = true; | ||
return Promise.resolve(); | ||
}); | ||
|
||
// Delay execution | ||
await delay(1); | ||
|
||
// tslint:disable-next-line: no-unused-expression | ||
expect(deferred.promise).to.not.be.undefined; | ||
// tslint:disable-next-line: no-unused-expression | ||
expect(isSettled).to.be.false; | ||
}); | ||
|
||
it("settles a Promise when exec() is called.", async function() { | ||
let isSettled = false; | ||
const deferred = new DeferredPromise(() => { | ||
isSettled = true; | ||
return Promise.resolve(); | ||
}); | ||
|
||
deferred.exec(); | ||
await expect(deferred.promise).to.eventually.be.fulfilled; | ||
|
||
// tslint:disable-next-line: no-unused-expression | ||
expect(isSettled).to.be.true; | ||
}); | ||
|
||
it("handles a rejected Promise.", async function() { | ||
let isSettled = false; | ||
const deferred = new DeferredPromise(() => { | ||
isSettled = true; | ||
return Promise.reject("Some error happened."); | ||
}); | ||
|
||
deferred.exec(); | ||
await expect(deferred.promise).to.eventually.be.rejectedWith("Some error happened."); | ||
|
||
// tslint:disable-next-line: no-unused-expression | ||
expect(isSettled).to.be.true; | ||
}); | ||
}); |