-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make providers WS and IPC reliable (#5763)
* ws and ipc reconnection * fix * increase time for IPC * fix ipc * add error messages. add ganache to dev dep * add ganache install to actions * ws reconnection test with geth * run as a docker * debug.trace * test * skip tests * test * test * test * test * ipc fixes * fix ws tests * fix * stop 8545 port * revert * small refactor * fix unit test * try to stop docker * wait port timeout * kill 8547 port * try stable version of geth * fix * fix path * move helpers Co-authored-by: Junaid <86780488+jdevcs@users.noreply.github.com>
- Loading branch information
Showing
16 changed files
with
585 additions
and
207 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
This file is part of web3.js. | ||
web3.js is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Lesser General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
web3.js is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Lesser General Public License for more details. | ||
You should have received a copy of the GNU Lesser General Public License | ||
along with web3.js. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import { exec } from 'child_process'; | ||
import path from 'path'; | ||
import fs from 'fs'; | ||
|
||
const IPC_DIR_PATH = path.join(__dirname, '..', '..', '..', '..', 'tmp'); | ||
const IPC_PATH = path.join(IPC_DIR_PATH, 'some.ipc'); | ||
const IPC_ORIGIN_PATH = path.join(IPC_DIR_PATH, 'some.ipc'); | ||
|
||
const createSymlink = `ln -s ${path.join(IPC_DIR_PATH, 'ipc.ipc')} ${IPC_ORIGIN_PATH}`; | ||
|
||
const execPromise = async (command: string): Promise<string> => | ||
new Promise((resolve, reject) => { | ||
exec(command, (error, stdout, stderr) => { | ||
if (error) { | ||
reject(error); | ||
return; | ||
} | ||
if (stderr) { | ||
reject(stderr); | ||
return; | ||
} | ||
resolve(stdout); | ||
}); | ||
}); | ||
|
||
const removeIfExists = () => { | ||
if (fs.existsSync(IPC_PATH)) { | ||
fs.unlinkSync(IPC_PATH); | ||
} | ||
}; | ||
export const startGethServer = async (): Promise<{ path: string; close: () => void }> => { | ||
removeIfExists(); | ||
await execPromise(createSymlink); | ||
return { | ||
path: IPC_PATH, | ||
close: (): void => { | ||
removeIfExists(); | ||
}, | ||
}; | ||
}; |
104 changes: 104 additions & 0 deletions
104
packages/web3-providers-ipc/test/integration/reconnection.test.ts
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,104 @@ | ||
/* | ||
This file is part of web3.js. | ||
web3.js is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Lesser General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
web3.js is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Lesser General Public License for more details. | ||
You should have received a copy of the GNU Lesser General Public License | ||
along with web3.js. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import IpcProvider from '../../src'; | ||
|
||
import { | ||
describeIf, | ||
getSystemTestProvider, | ||
isIpc, | ||
waitForOpenSocketConnection, | ||
waitForCloseSocketConnection, | ||
waitForEvent, | ||
} from '../fixtures/system_test_utils'; | ||
import { startGethServer } from '../fixtures/helpers'; | ||
|
||
describeIf(isIpc)('IpcSocketProvider - reconnection', () => { | ||
describe('subscribe event tests', () => { | ||
let reconnectionOptions: { | ||
delay: number; | ||
autoReconnect: boolean; | ||
maxAttempts: number; | ||
}; | ||
beforeAll(() => { | ||
reconnectionOptions = { | ||
delay: 50, | ||
autoReconnect: true, | ||
maxAttempts: 1000, | ||
}; | ||
}); | ||
it('check defaults', async () => { | ||
const web3Provider = new IpcProvider(getSystemTestProvider()); | ||
// @ts-expect-error-next-line | ||
expect(web3Provider._reconnectOptions).toEqual({ | ||
autoReconnect: true, | ||
delay: 5000, | ||
maxAttempts: 5, | ||
}); | ||
await waitForOpenSocketConnection(web3Provider); | ||
web3Provider.disconnect(1000, 'test'); | ||
await waitForCloseSocketConnection(web3Provider); | ||
}); | ||
it('set custom reconnectOptions', async () => { | ||
const web3Provider = new IpcProvider(getSystemTestProvider(), {}, reconnectionOptions); | ||
// @ts-expect-error-next-line | ||
expect(web3Provider._reconnectOptions).toEqual(reconnectionOptions); | ||
await waitForOpenSocketConnection(web3Provider); | ||
web3Provider.disconnect(1000, 'test'); | ||
await waitForCloseSocketConnection(web3Provider); | ||
}); | ||
it('should emit connect and disconnected events', async () => { | ||
const web3Provider = new IpcProvider(getSystemTestProvider()); | ||
expect(!!(await waitForEvent(web3Provider, 'connect'))).toBe(true); | ||
const disconnectPromise = waitForEvent(web3Provider, 'disconnect'); | ||
web3Provider.disconnect(); | ||
expect(!!(await disconnectPromise)).toBe(true); | ||
// @ts-expect-error read protected property | ||
expect(web3Provider.isReconnecting).toBe(false); | ||
}); | ||
it('should connect, disconnect and reconnect', async () => { | ||
const web3Provider = new IpcProvider(getSystemTestProvider(), {}, reconnectionOptions); | ||
expect(!!(await waitForEvent(web3Provider, 'connect'))).toBe(true); | ||
const connectEvent = waitForEvent(web3Provider, 'connect'); | ||
// @ts-expect-error call protected function | ||
web3Provider._socketConnection?.end(); | ||
expect(!!(await connectEvent)).toBe(true); | ||
web3Provider.disconnect(); | ||
await waitForEvent(web3Provider, 'disconnect'); | ||
}); | ||
it('should connect, disconnect, try reconnect and reach max attempts', async () => { | ||
const server = await startGethServer(); | ||
const web3Provider = new IpcProvider( | ||
server.path, | ||
{}, | ||
{ | ||
...reconnectionOptions, | ||
delay: 1, | ||
maxAttempts: 3, | ||
}, | ||
); | ||
expect(!!(await waitForEvent(web3Provider, 'connect'))).toBe(true); | ||
server.close(); | ||
// @ts-expect-error call protected function | ||
web3Provider._socketConnection?.end(); | ||
const errorEvent = waitForEvent(web3Provider, 'error'); | ||
|
||
const errorMessage = await errorEvent; | ||
expect(errorMessage).toBe(`Max connection attempts exceeded (${3})`); | ||
}); | ||
}); | ||
}); |
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.