-
Notifications
You must be signed in to change notification settings - Fork 437
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Removed detach #576
Merged
Merged
Removed detach #576
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4e8598d
Removed detach
MarcusLongmuir 1ac9eb3
Included setTimeouts in tests
MarcusLongmuir 7cb021b
Fixed fake transport race
MarcusLongmuir e1e10d7
Rethrowing callback exceptions
MarcusLongmuir 92f7873
Removed detach suite
MarcusLongmuir 2f66ce9
Simplify FakeTransport changes and change autoTrigger behaviour
MarcusLongmuir 45373d1
Added README explanation of withManualTrigger()
MarcusLongmuir File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ describe("FakeTransportBuilder", () => { | |
it("should allow the response messages to be stubbed", done => { | ||
const pingResponseMsg = makePingResponse("hello"); | ||
const transport = new FakeTransportBuilder() | ||
.withManualTrigger() | ||
.withMessages([ pingResponseMsg ]) | ||
.build(); | ||
|
||
|
@@ -20,6 +21,7 @@ describe("FakeTransportBuilder", () => { | |
it("should allow the response trailers to be stubbed", done => { | ||
const expectedTrailers = new grpc.Metadata({ foo: "bar" }); | ||
const transport = new FakeTransportBuilder() | ||
.withManualTrigger() | ||
.withTrailers(expectedTrailers) | ||
.build(); | ||
|
||
|
@@ -31,6 +33,7 @@ describe("FakeTransportBuilder", () => { | |
|
||
it("should allow an error to be injected before any headers are sent", done => { | ||
const transport = new FakeTransportBuilder() | ||
.withManualTrigger() | ||
.withPreHeadersError(500) | ||
.withMessages([ makePingResponse("hello") ]) | ||
.build(); | ||
|
@@ -50,6 +53,7 @@ describe("FakeTransportBuilder", () => { | |
const pingResponseMsg = makePingResponse("hello"); | ||
const trailers = new grpc.Metadata({ foo: "bar" }); | ||
const transport = new FakeTransportBuilder() | ||
.withManualTrigger() | ||
.withPreMessagesError(grpc.Code.FailedPrecondition, "failed precondition :)") | ||
.withMessages([ pingResponseMsg ]) | ||
.withTrailers(trailers) | ||
|
@@ -75,6 +79,7 @@ describe("FakeTransportBuilder", () => { | |
const pingResponseMsg = makePingResponse("hello"); | ||
const trailers = new grpc.Metadata({ foo: "bar" }); | ||
const transport = new FakeTransportBuilder() | ||
.withManualTrigger() | ||
.withPreTrailersError(grpc.Code.FailedPrecondition, "failed precondition :)") | ||
.withMessages([ pingResponseMsg ]) | ||
.withTrailers(trailers) | ||
|
@@ -100,6 +105,7 @@ describe("FakeTransportBuilder", () => { | |
it("should not be called if no message is sent", done => { | ||
const messageSpy = jest.fn(); | ||
const transport = new FakeTransportBuilder() | ||
.withManualTrigger() | ||
.withMessageListener(messageSpy) | ||
.build(); | ||
|
||
|
@@ -115,6 +121,7 @@ describe("FakeTransportBuilder", () => { | |
const expectedBytes = frameRequest(req); | ||
|
||
const transport = new FakeTransportBuilder() | ||
.withManualTrigger() | ||
.withMessageListener(messageSpy) | ||
.build(); | ||
|
||
|
@@ -132,6 +139,7 @@ describe("FakeTransportBuilder", () => { | |
const expectedBytes = [ frameRequest(reqA), frameRequest(reqB) ]; | ||
|
||
const transport = new FakeTransportBuilder() | ||
.withManualTrigger() | ||
.withMessageListener(messageSpy) | ||
.build(); | ||
|
||
|
@@ -151,6 +159,7 @@ describe("FakeTransportBuilder", () => { | |
const expectedHeaders = new grpc.Metadata({ expected: "header" }); | ||
|
||
const transport = new FakeTransportBuilder() | ||
.withManualTrigger() | ||
.withHeadersListener(headersSpy) | ||
.build(); | ||
|
||
|
@@ -163,6 +172,7 @@ describe("FakeTransportBuilder", () => { | |
|
||
client.start(expectedHeaders); | ||
client.finishSend(); | ||
transport.sendAll(); | ||
}) | ||
}); | ||
|
||
|
@@ -171,6 +181,7 @@ describe("FakeTransportBuilder", () => { | |
const requestSpy = jest.fn(); | ||
|
||
const transport = new FakeTransportBuilder() | ||
.withManualTrigger() | ||
.withRequestListener(requestSpy) | ||
.build(); | ||
|
||
|
@@ -189,6 +200,7 @@ describe("FakeTransportBuilder", () => { | |
const cancelSpy = jest.fn(); | ||
|
||
const transport = new FakeTransportBuilder() | ||
.withManualTrigger() | ||
.withCancelListener(cancelSpy) | ||
.build(); | ||
|
||
|
@@ -208,6 +220,7 @@ describe("FakeTransportBuilder", () => { | |
const finishSendSpy = jest.fn(); | ||
|
||
const transport = new FakeTransportBuilder() | ||
.withManualTrigger() | ||
.withFinishSendListener(finishSendSpy) | ||
.build(); | ||
|
||
|
@@ -233,6 +246,7 @@ describe("FakeTransportBuilder", () => { | |
const onEndSpy = jest.fn(); | ||
|
||
const transport = new FakeTransportBuilder() | ||
.withManualTrigger() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Duplicated line. |
||
.withManualTrigger() | ||
.withHeaders(new grpc.Metadata({ header: "value" })) | ||
.withMessages([ makePingResponse("msgA") ]) | ||
|
@@ -258,8 +272,6 @@ describe("FakeTransportBuilder", () => { | |
|
||
transport.sendHeaders(); | ||
|
||
// Note that we must defer the assertions as the grpc-web package detaches all callbacks, so they | ||
// will not run immediately. | ||
setTimeout(() => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You've removed the comment, but not the timeout :) |
||
expect(onHeadersSpy).toHaveBeenCalled(); | ||
expect(onMessageSpy).not.toHaveBeenCalled(); | ||
|
@@ -280,7 +292,6 @@ describe("FakeTransportBuilder", () => { | |
done(); | ||
}, 0); | ||
}, 0); | ||
|
||
}, 0); | ||
}); | ||
}); | ||
|
@@ -342,6 +353,7 @@ describe("FakeTransportBuilder", () => { | |
client.start(); | ||
client.send(req); | ||
client.finishSend(); | ||
transport.sendAll(); | ||
} | ||
|
||
function doPingStreamRequest(transport: TriggerableTransport, requests: PingRequest[], callback: (data: RequestResponse<PingResponse>) => void) { | ||
|
@@ -351,5 +363,6 @@ describe("FakeTransportBuilder", () => { | |
client.send(req); | ||
} | ||
client.finishSend(); | ||
transport.sendAll(); | ||
} | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,43 +1,3 @@ | ||
// awaitingExecution is null if there is no current timer | ||
let awaitingExecution: Array<() => void> | null = null; | ||
|
||
function runCallbacks() { | ||
if (awaitingExecution) { | ||
// Use a new reference to the awaitingExecution array to allow callbacks to add to the "new" awaitingExecution array | ||
const thisCallbackSet = awaitingExecution; | ||
awaitingExecution = null; | ||
for (let i = 0; i < thisCallbackSet.length; i++) { | ||
try { | ||
thisCallbackSet[i](); | ||
} catch (e) { | ||
if (awaitingExecution === null) { | ||
awaitingExecution = []; | ||
setTimeout(() => { | ||
runCallbacks(); | ||
}, 0); | ||
} | ||
// Add the remaining callbacks to the array so that they can be invoked on the next pass | ||
for (let k = thisCallbackSet.length - 1; k > i; k--) { | ||
awaitingExecution.unshift(thisCallbackSet[k]); | ||
} | ||
// rethrow the error | ||
throw e; | ||
} | ||
} | ||
} | ||
} | ||
|
||
// detach executes the callbacks in the order they are added with no context - this is used to avoid errors thrown | ||
// in user callbacks being caught by handlers such as fetch's catch. This function is necessary as setTimeout in | ||
// Safari is prone to switching the order of execution of setTimeout(0). | ||
export default function detach(cb: () => void) { | ||
if (awaitingExecution !== null) { | ||
// there is a timer running, add to the list and this function will be executed with that existing timer | ||
awaitingExecution.push(cb); | ||
return; | ||
} | ||
awaitingExecution = [cb]; | ||
setTimeout(() => { | ||
runCallbacks(); | ||
}, 0); | ||
cb(); | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see you've made it be a manual trigger, but I don't see you triggering it anywhere in the test? I don't follow, you please explain this change?