-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
337 additions
and
77 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,44 @@ | ||
import { IEngine, Transport } from '@dcl/ecs' | ||
import { AsyncQueue } from '@well-known-components/pushable-channel' | ||
|
||
import { CrdtStreamMessage } from '../data-layer/remote-data-layer' | ||
import { DataLayerRpcClient } from '../data-layer/types' | ||
import { consumeAllMessagesInto } from '../logic/consume-stream' | ||
import { serializeCrdtMessages } from './crdt-logger' | ||
|
||
export function connectCrdtToEngine( | ||
engine: IEngine, | ||
dataLayerStream: DataLayerRpcClient['crdtStream'], | ||
engineKey: string | ||
) { | ||
// <HERE BE DRAGONS (TRANSPORT)> | ||
const outgoingMessagesStream = new AsyncQueue<CrdtStreamMessage>((_, _action) => {}) | ||
|
||
const transport: Transport = { | ||
filter() { | ||
return !outgoingMessagesStream.closed | ||
}, | ||
async send(message) { | ||
if (outgoingMessagesStream.closed) return | ||
outgoingMessagesStream.enqueue({ data: message }) | ||
if (message.byteLength) { | ||
Array.from(serializeCrdtMessages(`${engineKey}>Datalayer`, message, engine)).forEach(($) => console.log($)) | ||
} | ||
} | ||
} | ||
Object.assign(transport, { name: `${engineKey}TransportClient` }) | ||
engine.addTransport(transport) | ||
|
||
function onMessage(message: Uint8Array) { | ||
if (message.byteLength) { | ||
Array.from(serializeCrdtMessages(`DataLayer>${engineKey}`, message, engine)).forEach(($) => console.log($)) | ||
} | ||
transport.onmessage!(message) | ||
void engine.update(1) | ||
} | ||
|
||
consumeAllMessagesInto(dataLayerStream(outgoingMessagesStream), onMessage).catch((e) => { | ||
console.error(`${engineKey} consumeAllMessagesInto failed: `, e) | ||
outgoingMessagesStream.close() | ||
}) | ||
} |
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
119 changes: 119 additions & 0 deletions
119
packages/@dcl/inspector/src/redux/data-layer/sagas/connect.spec.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,119 @@ | ||
/* eslint-disable @typescript-eslint/ban-types */ | ||
import { expectSaga, testSaga } from 'redux-saga-test-plan' | ||
import * as codegen from '@dcl/rpc/dist/codegen' | ||
|
||
import { connectSaga, createSocketChannel, createWebSocketConnection, getWsUrl } from './connect' | ||
import { WebSocketTransport } from '@dcl/rpc/dist/transports/WebSocket' | ||
import { RpcClient, RpcClientPort, Transport, createRpcClient } from '@dcl/rpc' | ||
import { DataLayerRpcClient } from '../../../lib/data-layer/types' | ||
import reducer, { connected, reconnect } from '..' | ||
import { createLocalDataLayerRpcClient } from '../../../lib/data-layer/client/local-data-layer' | ||
import { call } from 'redux-saga/effects' | ||
|
||
describe('WebSocket Connection Saga', () => { | ||
it('Should create LOCAL data-layer if no ws url is provided', async () => { | ||
const dataLayer = { boedo: 'casla' } as any as DataLayerRpcClient | ||
|
||
return expectSaga(connectSaga) | ||
.withReducer(reducer) | ||
.provide([ | ||
[call(getWsUrl), undefined], | ||
[call(createLocalDataLayerRpcClient), dataLayer] | ||
]) | ||
.put(connected({ dataLayer })) | ||
.hasFinalState({ | ||
dataLayer, | ||
error: undefined, | ||
reconnectAttempts: 0 | ||
}) | ||
.run() | ||
}) | ||
|
||
it('Should create remote data-layer with Ws', async () => { | ||
const url = 'ws://boedo.com' | ||
const ws = new MockWebSocket() | ||
const channel = createSocketChannel(ws as any as WebSocket) | ||
const clientTransport = {} as Transport | ||
const client: RpcClient = { createPort: (_port: string) => {} } as RpcClient | ||
const clientPort: RpcClientPort = {} as RpcClientPort | ||
const dataLayer = { boedo: 'casla' } as any as DataLayerRpcClient | ||
jest.spyOn(codegen, 'loadService').mockReturnValue(dataLayer as any) | ||
const consoleSpy = jest.spyOn(console, 'error').mockImplementation(() => {}) | ||
|
||
testSaga(connectSaga) | ||
.next() | ||
.call(getWsUrl) | ||
.next(url) | ||
.call(createWebSocketConnection, url) | ||
.next(ws) | ||
.call(createSocketChannel, ws) | ||
.next(channel) | ||
.take(channel as any) | ||
|
||
// OPEN event. Connect data layer | ||
.next({ type: 'WS_OPENED' }) | ||
.call(WebSocketTransport, ws) | ||
.next(clientTransport) | ||
.call(createRpcClient, clientTransport) | ||
.next(client) | ||
.call(client.createPort, 'scene-ctx') | ||
.next(clientPort) | ||
.put(connected({ dataLayer })) | ||
.next() | ||
|
||
// Error event. console.error (TODO: handle this) | ||
.next({ type: 'WS_ERROR', error: 'some - error' }) | ||
|
||
// Break the connection. Should reconnect | ||
.finish() | ||
.put(reconnect()) | ||
.next() | ||
.isDone() | ||
|
||
// Error logic | ||
expect(consoleSpy).toBeCalledWith('some - error') | ||
}) | ||
}) | ||
|
||
// Mock WebSocket | ||
|
||
class MockWebSocket { | ||
listeners: { [key: string]: Function[] } = {} | ||
|
||
addEventListener(event: string, callback: Function) { | ||
if (!this.listeners[event]) { | ||
this.listeners[event] = [] | ||
} | ||
this.listeners[event].push(callback) | ||
} | ||
|
||
removeEventListener(event: string, callback: Function) { | ||
if (this.listeners[event]) { | ||
this.listeners[event] = this.listeners[event].filter((cb) => cb !== callback) | ||
} | ||
} | ||
|
||
simulateOpen() { | ||
if (this.listeners['open']) { | ||
this.listeners['open'].forEach((callback) => callback()) | ||
} | ||
} | ||
|
||
simulateMessage(data: any) { | ||
if (this.listeners['message']) { | ||
this.listeners['message'].forEach((callback) => callback({ data })) | ||
} | ||
} | ||
|
||
simulateClose() { | ||
if (this.listeners['close']) { | ||
this.listeners['close'].forEach((callback) => callback()) | ||
} | ||
} | ||
|
||
simulateError() { | ||
if (this.listeners['error']) { | ||
this.listeners['error'].forEach((callback) => callback()) | ||
} | ||
} | ||
} |
Oops, something went wrong.