Skip to content

Commit

Permalink
add the mock transport and remove asClient* helpers (#268)
Browse files Browse the repository at this point in the history
## Why

<!-- Describe what you are trying to accomplish with this pull request
-->

- `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

<!-- Describe the changes you made in this pull request or pointers for
the reviewer -->

## Versioning

- [ ] Breaking protocol change
- [x] Breaking ts/js API change

<!-- Kind reminder to add tests and updated documentation if needed -->
  • Loading branch information
jackyzha0 authored Sep 10, 2024
1 parent 1bc1ce9 commit 3617905
Show file tree
Hide file tree
Showing 35 changed files with 481 additions and 388 deletions.
11 changes: 6 additions & 5 deletions __tests__/bandwidth.bench.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { afterAll, assert, bench, describe } from 'vitest';
import { getClientSendFn, waitForMessage } from '../util/testHelpers';
import { TestServiceSchema } from './fixtures/services';
import { getClientSendFn, waitForMessage } from '../testUtil';
import { TestServiceSchema } from '../testUtil/fixtures/services';
import { createServer } from '../router/server';
import { createClient } from '../router/client';
import { transports } from './fixtures/transports';
import { transports } from '../testUtil/fixtures/transports';
import { nanoid } from 'nanoid';

let n = 0;
Expand All @@ -19,7 +19,7 @@ const dummyPayloadSmall = () => ({
// give time for v8 to warm up
const BENCH_DURATION = 10_000;
describe('bandwidth', async () => {
for (const { name, setup } of transports) {
for (const { name, setup } of transports.filter((t) => t.name !== 'mock')) {
const { getClientTransport, getServerTransport, cleanup } = await setup();
afterAll(cleanup);

Expand Down Expand Up @@ -57,11 +57,12 @@ describe('bandwidth', async () => {
);

const { reqWritable, resReadable } = client.test.echo.stream({});
const resIter = resReadable[Symbol.asyncIterator]();
bench(
`${name} -- stream`,
async () => {
reqWritable.write({ msg: nanoid(), ignore: false });
const result = await resReadable[Symbol.asyncIterator]().next();
const result = await resIter.next();
assert(result.value?.ok);
},
{ time: BENCH_DURATION },
Expand Down
6 changes: 3 additions & 3 deletions __tests__/cancellation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import {
createClient,
createServer,
} from '../router';
import { testMatrix } from './fixtures/matrix';
import { testMatrix } from '../testUtil/fixtures/matrix';
import {
cleanupTransports,
createPostTestCleanups,
testFinishesCleanly,
waitFor,
} from './fixtures/cleanup';
} from '../testUtil/fixtures/cleanup';
import { CANCEL_CODE, UNCAUGHT_ERROR_CODE } from '../router/errors';
import { TestSetupHelpers } from './fixtures/transports';
import { TestSetupHelpers } from '../testUtil/fixtures/transports';

function makeMockHandler<T extends ValidProcType>(
_type: T,
Expand Down
10 changes: 5 additions & 5 deletions __tests__/cleanup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import {
isReadableDone,
numberOfConnections,
getClientSendFn,
} from '../util/testHelpers';
} from '../testUtil';
import {
SubscribableServiceSchema,
TestServiceSchema,
UploadableServiceSchema,
} from './fixtures/services';
} from '../testUtil/fixtures/services';
import {
Ok,
Procedure,
Expand All @@ -26,9 +26,9 @@ import {
createPostTestCleanups,
testFinishesCleanly,
waitFor,
} from './fixtures/cleanup';
import { testMatrix } from './fixtures/matrix';
import { TestSetupHelpers } from './fixtures/transports';
} from '../testUtil/fixtures/cleanup';
import { testMatrix } from '../testUtil/fixtures/matrix';
import { TestSetupHelpers } from '../testUtil/fixtures/transports';
import { ControlFlags } from '../transport/message';
import { Type } from '@sinclair/typebox';
import { nanoid } from 'nanoid';
Expand Down
9 changes: 6 additions & 3 deletions __tests__/context.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { beforeEach, describe, expect, test } from 'vitest';
import { cleanupTransports, createPostTestCleanups } from './fixtures/cleanup';
import { testMatrix } from './fixtures/matrix';
import { TestSetupHelpers } from './fixtures/transports';
import {
cleanupTransports,
createPostTestCleanups,
} from '../testUtil/fixtures/cleanup';
import { testMatrix } from '../testUtil/fixtures/matrix';
import { TestSetupHelpers } from '../testUtil/fixtures/transports';
import {
Ok,
Procedure,
Expand Down
12 changes: 6 additions & 6 deletions __tests__/disconnects.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import {
isReadableDone,
numberOfConnections,
readNextResult,
} from '../util/testHelpers';
} from '../testUtil';
import {
SubscribableServiceSchema,
TestServiceSchema,
UploadableServiceSchema,
} from './fixtures/services';
} from '../testUtil/fixtures/services';
import {
createClient,
createServer,
Expand All @@ -20,10 +20,10 @@ import {
cleanupTransports,
testFinishesCleanly,
waitFor,
} from './fixtures/cleanup';
import { testMatrix } from './fixtures/matrix';
import { TestSetupHelpers } from './fixtures/transports';
import { createPostTestCleanups } from './fixtures/cleanup';
} from '../testUtil/fixtures/cleanup';
import { testMatrix } from '../testUtil/fixtures/matrix';
import { TestSetupHelpers } from '../testUtil/fixtures/transports';
import { createPostTestCleanups } from '../testUtil/fixtures/cleanup';

describe.each(testMatrix())(
'procedures should handle unexpected disconnects ($transport.name transport, $codec.name codec)',
Expand Down
11 changes: 6 additions & 5 deletions __tests__/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
isReadableDone,
numberOfConnections,
readNextResult,
} from '../util/testHelpers';
} from '../testUtil';
import { createServer } from '../router/server';
import { createClient } from '../router/client';
import {
Expand All @@ -17,15 +17,15 @@ import {
UploadableServiceSchema,
OrderingServiceSchema,
NonObjectSchemas,
} from './fixtures/services';
} from '../testUtil/fixtures/services';
import {
advanceFakeTimersBySessionGrace,
cleanupTransports,
createPostTestCleanups,
testFinishesCleanly,
waitFor,
} from './fixtures/cleanup';
import { testMatrix } from './fixtures/matrix';
} from '../testUtil/fixtures/cleanup';
import { testMatrix } from '../testUtil/fixtures/matrix';
import { Type } from '@sinclair/typebox';
import {
Procedure,
Expand All @@ -38,7 +38,7 @@ import {
createClientHandshakeOptions,
createServerHandshakeOptions,
} from '../router/handshake';
import { TestSetupHelpers } from './fixtures/transports';
import { TestSetupHelpers } from '../testUtil/fixtures/transports';

describe.each(testMatrix())(
'client <-> server integration test ($transport.name transport, $codec.name codec)',
Expand Down Expand Up @@ -869,6 +869,7 @@ describe.each(testMatrix())(
createClientHandshakeOptions(requestSchema, () => ({ data: 'foobar' })),
);
const serverTransport = getServerTransport(
'SERVER',
createServerHandshakeOptions(requestSchema, (metadata) => {
return {
data: metadata.data,
Expand Down
Loading

0 comments on commit 3617905

Please sign in to comment.