-
Notifications
You must be signed in to change notification settings - Fork 446
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add latency option to memory transport (#2810)
Allows slowing all network traffic by the passed amount of ms.
- Loading branch information
1 parent
5feba91
commit 050b01f
Showing
7 changed files
with
100 additions
and
124 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 was deleted.
Oops, something went wrong.
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 @@ | ||
import { defaultLogger } from '@libp2p/logger' | ||
import { peerIdFromString } from '@libp2p/peer-id' | ||
import { multiaddr } from '@multiformats/multiaddr' | ||
import { expect } from 'aegir/chai' | ||
import { stubInterface } from 'sinon-ts' | ||
import { memory } from '../src/index.js' | ||
import type { Upgrader, Connection } from '@libp2p/interface' | ||
|
||
describe('memory', () => { | ||
let upgrader: Upgrader | ||
|
||
beforeEach(async () => { | ||
upgrader = stubInterface<Upgrader>({ | ||
upgradeInbound: async (maConn) => { | ||
return stubInterface<Connection>() | ||
}, | ||
upgradeOutbound: async (maConn) => { | ||
return stubInterface<Connection>() | ||
} | ||
}) | ||
}) | ||
|
||
it('should dial', async () => { | ||
const transport = memory()({ | ||
peerId: peerIdFromString('12D3KooWJRSrypvnpHgc6ZAgyCni4KcSmbV7uGRaMw5LgMKT18fq'), | ||
logger: defaultLogger() | ||
}) | ||
const ma = multiaddr('/memory/address-1') | ||
const listener = transport.createListener({ | ||
upgrader | ||
}) | ||
await listener.listen(ma) | ||
|
||
const conn = await transport.dial(ma, { | ||
upgrader | ||
}) | ||
|
||
await conn.close() | ||
await listener.close() | ||
}) | ||
|
||
it('should dial with latency', async () => { | ||
const latency = 1000 | ||
const transport = memory({ | ||
latency | ||
})({ | ||
peerId: peerIdFromString('12D3KooWJRSrypvnpHgc6ZAgyCni4KcSmbV7uGRaMw5LgMKT18fq'), | ||
logger: defaultLogger() | ||
}) | ||
const ma = multiaddr('/memory/address-1') | ||
const listener = transport.createListener({ | ||
upgrader | ||
}) | ||
await listener.listen(ma) | ||
|
||
const start = Date.now() | ||
const conn = await transport.dial(ma, { | ||
upgrader | ||
}) | ||
const end = Date.now() | ||
|
||
// +/- a bit | ||
expect(end - start).to.be.greaterThan(latency / 1.1) | ||
|
||
await conn.close() | ||
await listener.close() | ||
}) | ||
}) |