|
| 1 | +// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ |
| 2 | +// ┃ ██████ ██████ ██████ █ █ █ █ █ █▄ ▀███ █ ┃ |
| 3 | +// ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█ ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄ ▀█ █ ▀▀▀▀▀ ┃ |
| 4 | +// ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄ █ ▄▄▄▄▄ ┃ |
| 5 | +// ┃ █ ██████ █ ▀█▄ █ ██████ █ ███▌▐███ ███████▄ █ ┃ |
| 6 | +// ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫ |
| 7 | +// ┃ Copyright (c) 2017, the Perspective Authors. ┃ |
| 8 | +// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃ |
| 9 | +// ┃ This file is part of the Perspective library, distributed under the terms ┃ |
| 10 | +// ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃ |
| 11 | +// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ |
| 12 | + |
| 13 | +import { test, expect } from "@finos/perspective-test"; |
| 14 | +import { make_client, make_server } from "@finos/perspective"; |
| 15 | + |
| 16 | +test("Proxy session tunnels requests through client", async () => { |
| 17 | + // test.setTimeout(2000); |
| 18 | + const { client, server } = connectClientToServer(); |
| 19 | + const { proxyClient } = connectProxyClient(client); |
| 20 | + |
| 21 | + // Verify that main client + proxy client observe the same server |
| 22 | + const clientTables1 = await client.get_hosted_table_names(); |
| 23 | + const proxyTables1 = await proxyClient.get_hosted_table_names(); |
| 24 | + expect(proxyTables1).toStrictEqual([]); |
| 25 | + expect(proxyTables1).toStrictEqual(clientTables1); |
| 26 | + const name = "abc-" + Math.random(); |
| 27 | + const _table = await client.table({ abc: [123] }, { name }); |
| 28 | + const clientTables2 = await client.get_hosted_table_names(); |
| 29 | + const proxyTables2 = await proxyClient.get_hosted_table_names(); |
| 30 | + expect(proxyTables2).toStrictEqual([name]); |
| 31 | + expect(proxyTables2).toStrictEqual(clientTables2); |
| 32 | +}); |
| 33 | + |
| 34 | +test("Proxy session tunnels on_update callbacks through client", async () => { |
| 35 | + // test.setTimeout(2000); |
| 36 | + const { client } = connectClientToServer(); |
| 37 | + const { proxyClient } = connectProxyClient(client); |
| 38 | + const name = "abc-" + Math.random(); |
| 39 | + const clientTable = await client.table({ abc: [123] }, { name }); |
| 40 | + |
| 41 | + // Add an on_update callback to the proxy client's view of the table |
| 42 | + const proxyTable = await proxyClient.open_table(name); |
| 43 | + const proxyView = await proxyTable.view(); |
| 44 | + let resolveUpdate; |
| 45 | + const onUpdateResp = new Promise((r) => (resolveUpdate = r)); |
| 46 | + await proxyView.on_update( |
| 47 | + (x) => { |
| 48 | + resolveUpdate(x); |
| 49 | + }, |
| 50 | + { mode: "row" } |
| 51 | + ); |
| 52 | + |
| 53 | + // Enact table update through client's table handle, and assert that proxy |
| 54 | + // client's on_update callback is called |
| 55 | + await clientTable.update({ abc: [999] }); |
| 56 | + const expectUpdate = expect.poll( |
| 57 | + async () => { |
| 58 | + const data = await onUpdateResp; |
| 59 | + // TODO: construct table out of onUpdateResp, assert contents? |
| 60 | + console.log("onUpdateResp, with data", data); |
| 61 | + return data; |
| 62 | + }, |
| 63 | + { |
| 64 | + message: "Ensure proxy view updates with table", |
| 65 | + timeout: 10000, |
| 66 | + } |
| 67 | + ); |
| 68 | + |
| 69 | + expect(await proxyView.to_columns()).toStrictEqual({ abc: [123, 999] }); |
| 70 | + |
| 71 | + await expectUpdate.toHaveProperty("delta"); |
| 72 | + await expectUpdate.toHaveProperty("port_id"); |
| 73 | +}); |
| 74 | + |
| 75 | +function connectClientToServer() { |
| 76 | + const server = make_server(); |
| 77 | + const session = server.make_session((msg) => { |
| 78 | + client.handle_response(msg); |
| 79 | + }); |
| 80 | + const client = make_client((msg) => { |
| 81 | + session.handle_request(msg); |
| 82 | + }); |
| 83 | + return { |
| 84 | + client, |
| 85 | + server, |
| 86 | + }; |
| 87 | +} |
| 88 | + |
| 89 | +function connectProxyClient(client) { |
| 90 | + const sess = client.new_proxy_session((res) => { |
| 91 | + proxyClient.handle_response(res); |
| 92 | + }); |
| 93 | + const proxyClient = make_client((msg) => { |
| 94 | + sess.handle_request(msg); |
| 95 | + }); |
| 96 | + return { |
| 97 | + proxyClient, |
| 98 | + }; |
| 99 | +} |
0 commit comments