-
Notifications
You must be signed in to change notification settings - Fork 822
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: unifying shutdown once with BindOnceFuture (#2695)
Co-authored-by: Valentin Marchaud <[email protected]>
- Loading branch information
1 parent
5f3cbc2
commit d61f7be
Showing
15 changed files
with
323 additions
and
111 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
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,54 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { Deferred } from './promise'; | ||
|
||
/** | ||
* Bind the callback and only invoke the callback once regardless how many times `BindOnceFuture.call` is invoked. | ||
*/ | ||
export class BindOnceFuture< | ||
R, | ||
This = unknown, | ||
T extends (this: This, ...args: unknown[]) => R = () => R | ||
> { | ||
private _isCalled = false; | ||
private _deferred = new Deferred<R>(); | ||
constructor(private _callback: T, private _that: This) {} | ||
|
||
get isCalled() { | ||
return this._isCalled; | ||
} | ||
|
||
get promise() { | ||
return this._deferred.promise; | ||
} | ||
|
||
call(...args: Parameters<T>): Promise<R> { | ||
if (!this._isCalled) { | ||
this._isCalled = true; | ||
try { | ||
Promise.resolve(this._callback.call(this._that, ...args)) | ||
.then( | ||
val => this._deferred.resolve(val), | ||
err => this._deferred.reject(err) | ||
); | ||
} catch (err) { | ||
this._deferred.reject(err); | ||
} | ||
} | ||
return this._deferred.promise; | ||
} | ||
} |
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,39 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
export class Deferred<T> { | ||
private _promise: Promise<T>; | ||
private _resolve!: (val: T) => void; | ||
private _reject!: (error: unknown) => void; | ||
constructor() { | ||
this._promise = new Promise((resolve, reject) => { | ||
this._resolve = resolve; | ||
this._reject = reject; | ||
}); | ||
} | ||
|
||
get promise() { | ||
return this._promise; | ||
} | ||
|
||
resolve(val: T) { | ||
this._resolve(val); | ||
} | ||
|
||
reject(err: unknown) { | ||
this._reject(err); | ||
} | ||
} |
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,30 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import * as assert from 'assert'; | ||
|
||
/** | ||
* Node.js v8.x and browser compatible `assert.rejects`. | ||
*/ | ||
export async function assertRejects(promise: any, expect: any) { | ||
try { | ||
await promise; | ||
} catch (err) { | ||
assert.throws(() => { | ||
throw err; | ||
}, expect); | ||
} | ||
} |
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,63 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import * as assert from 'assert'; | ||
import * as sinon from 'sinon'; | ||
import { BindOnceFuture } from '../../src'; | ||
import { assertRejects } from '../test-utils'; | ||
|
||
describe('callback', () => { | ||
describe('BindOnceFuture', () => { | ||
it('should call once', async () => { | ||
const stub = sinon.stub(); | ||
const that = {}; | ||
const future = new BindOnceFuture(stub, that); | ||
|
||
await Promise.all([ | ||
future.call(1), | ||
future.call(2), | ||
]); | ||
await future.call(3); | ||
await future.promise; | ||
|
||
assert.strictEqual(stub.callCount, 1); | ||
assert.deepStrictEqual(stub.firstCall.args, [1]); | ||
assert.deepStrictEqual(stub.firstCall.thisValue, that); | ||
|
||
assert(future.isCalled); | ||
}); | ||
|
||
it('should handle thrown errors', async () => { | ||
const stub = sinon.stub(); | ||
stub.throws(new Error('foo')); | ||
const future = new BindOnceFuture(stub, undefined); | ||
|
||
await assertRejects(future.call(), /foo/); | ||
await assertRejects(future.call(), /foo/); | ||
await assertRejects(future.promise, /foo/); | ||
}); | ||
|
||
it('should handle rejections', async () => { | ||
const stub = sinon.stub(); | ||
stub.rejects(new Error('foo')); | ||
const future = new BindOnceFuture(stub, undefined); | ||
|
||
await assertRejects(future.call(), /foo/); | ||
await assertRejects(future.call(), /foo/); | ||
await assertRejects(future.promise, /foo/); | ||
}); | ||
}); | ||
}); |
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,41 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import * as assert from 'assert'; | ||
import { Deferred } from '../../src/utils/promise'; | ||
import { assertRejects } from '../test-utils'; | ||
|
||
describe('promise', () => { | ||
describe('Deferred', () => { | ||
it('should resolve', async () => { | ||
const deferred = new Deferred(); | ||
deferred.resolve(1); | ||
deferred.resolve(2); | ||
deferred.reject(new Error('foo')); | ||
|
||
const ret = await deferred.promise; | ||
assert.strictEqual(ret, 1); | ||
}); | ||
|
||
it('should reject', async () => { | ||
const deferred = new Deferred(); | ||
deferred.reject(new Error('foo')); | ||
deferred.reject(new Error('bar')); | ||
|
||
await assertRejects(deferred.promise, /foo/); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.