Skip to content
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

add the mock transport and remove asClient* helpers #268

Merged
merged 7 commits into from
Sep 10, 2024

Conversation

jackyzha0
Copy link
Member

@jackyzha0 jackyzha0 commented Sep 9, 2024

Why

  • asClient* style test helpers relied on emulating the logic in router precisely and this can go out of date/drift as changes are made
  • we noticed, for example, that the helpers dont abort the signal after both sides of the handler exit for example

What changed

  • remove these test helpers in favour of a mock transport network

Versioning

  • Breaking protocol change
  • Breaking ts/js API change

@jackyzha0 jackyzha0 requested a review from a team as a code owner September 9, 2024 19:41
@jackyzha0 jackyzha0 requested review from bradymadden97 and removed request for a team September 9, 2024 19:41
Copy link
Contributor

@lhchavez lhchavez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good in general. left two biggish comments.

@@ -255,177 +239,6 @@ function dummyCtx<State>(
};
}

export function asClientRpc<
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


const transports: Array<MockClientTransport | MockServerTransport> = [];
class MockClientTransport extends ClientTransport<InMemoryConnection> {
async createNewOutgoingConnection(): Promise<InMemoryConnection> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feels like to should be accepted here and then the "MockTransport" should only care about connections targeted at its clientId

Comment on lines 148 to 149
getServerTransport: (handshakeOptions) => {
const serverTransport = new MockServerTransport('SERVER', opts?.server);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
getServerTransport: (handshakeOptions) => {
const serverTransport = new MockServerTransport('SERVER', opts?.server);
getServerTransport: (id = `SERVER${nanoid()}`, handshakeOptions) => {
const serverTransport = new MockServerTransport(id, opts?.server);

Comment on lines 117 to 126
for (const conn of Object.values(conns)) {
if (conn.handled) {
continue;
}

// if we find one, handle it
conn.handled = true;
const connection = new InMemoryConnection(conn.serverToClient);

this.handleConnection(connection);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems strange that you can create many server transports but not have control over which one clients are connecting to.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right now, our transport fixture just assumes theres only one server (our ws mock

assumes this too)

i think its ok as none of our tests assume multiple server transports? i can also refactor both fixtures to allow for multiple transports but seems a bit YAGNI

clientTransport.extendHandshake(handshakeOptions);
}

transports.push(clientTransport);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we storing these here? seems like the only place we're using transports we're filtering out clients.

async restartServer() {
for (const transport of transports) {
if (transport.clientId !== 'SERVER') continue;
transport.close();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this a restart? isn't close destructive?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah we destroy the old one and make an entirely new one, transports dont have the ability to 'restart' so we emulate it losing state by doing this

Comment on lines 158 to 162
simulatePhantomDisconnect() {
for (const conn of Object.values(connections.get())) {
conn.serverToClient.pause();
}
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how do you resume?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you dont, this is just following the interface in

export interface TestSetupHelpers {

we can add the ability to do that if we need it?

@jackyzha0 jackyzha0 merged commit 3617905 into main Sep 10, 2024
4 checks passed
@jackyzha0 jackyzha0 deleted the jackyzha0/mock-transport branch September 10, 2024 02:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants