-
Notifications
You must be signed in to change notification settings - Fork 130
fix: add udp test to system test #2417
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,6 +1,7 @@ | ||||||||||||||
| import { serve } from "@hono/node-server"; | ||||||||||||||
| import { createNodeWebSocket } from "@hono/node-ws"; | ||||||||||||||
| import { createAndStartServer } from "../shared/server.js"; | ||||||||||||||
| import dgram from 'dgram'; | ||||||||||||||
|
|
||||||||||||||
| let injectWebSocket: any; | ||||||||||||||
| const { app, port } = createAndStartServer((app) => { | ||||||||||||||
|
|
@@ -12,3 +13,40 @@ const { app, port } = createAndStartServer((app) => { | |||||||||||||
|
|
||||||||||||||
| const server = serve({ fetch: app.fetch, port }); | ||||||||||||||
| injectWebSocket(server); | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| // Get port from environment | ||||||||||||||
| const portEnv = | ||||||||||||||
| typeof Deno !== "undefined" | ||||||||||||||
| ? Deno.env.get("PORT_UDP") | ||||||||||||||
| : process.env.PORT_UDP; | ||||||||||||||
|
|
||||||||||||||
| if (portEnv) { | ||||||||||||||
| // Create a UDP socket | ||||||||||||||
| const udpServer = dgram.createSocket('udp4'); | ||||||||||||||
|
|
||||||||||||||
| // Listen for incoming messages | ||||||||||||||
| udpServer.on('message', (msg, rinfo) => { | ||||||||||||||
| console.log(`UDP server received: ${msg} from ${rinfo.address}:${rinfo.port}`); | ||||||||||||||
|
|
||||||||||||||
| // Echo the message back to the sender | ||||||||||||||
| udpServer.send(msg, rinfo.port, rinfo.address, (err) => { | ||||||||||||||
| if (err) console.error('Failed to send UDP response:', err); | ||||||||||||||
| }); | ||||||||||||||
| }); | ||||||||||||||
|
|
||||||||||||||
| // Handle errors | ||||||||||||||
| udpServer.on('error', (err) => { | ||||||||||||||
| console.error('UDP server error:', err); | ||||||||||||||
| udpServer.close(); | ||||||||||||||
| }); | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| const port2 = Number.parseInt(portEnv); | ||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: No validation on port2 being a valid number - parseInt() could return NaN
Suggested change
|
||||||||||||||
|
|
||||||||||||||
| udpServer.bind(port2, () => { | ||||||||||||||
| console.log(`UDP echo server running on port ${port2}`); | ||||||||||||||
| }); | ||||||||||||||
| } else { | ||||||||||||||
| console.warn("missing PORT_UDP"); | ||||||||||||||
| } | ||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,5 +1,6 @@ | ||||||||||||||
| import { RivetClient } from "@rivet-gg/api"; | ||||||||||||||
| import WebSocket from "ws"; | ||||||||||||||
| import dgram from 'dgram'; | ||||||||||||||
|
|
||||||||||||||
| // Can be opt since they're not required for dev | ||||||||||||||
| const RIVET_ENDPOINT = process.env.RIVET_ENDPOINT; | ||||||||||||||
|
|
@@ -48,6 +49,13 @@ async function run() { | |||||||||||||
| guard: {}, | ||||||||||||||
| }, | ||||||||||||||
| }, | ||||||||||||||
| udp: { | ||||||||||||||
| protocol: "udp", | ||||||||||||||
| // internalPort: 80, | ||||||||||||||
| routing: { | ||||||||||||||
| host: {}, | ||||||||||||||
| }, | ||||||||||||||
| }, | ||||||||||||||
| }, | ||||||||||||||
| }, | ||||||||||||||
| lifecycle: { | ||||||||||||||
|
|
@@ -106,6 +114,7 @@ async function run() { | |||||||||||||
| await new Promise((resolve) => setTimeout(resolve, 100)); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // WS | ||||||||||||||
| await new Promise((resolve, reject) => { | ||||||||||||||
| // Open a WebSocket to that endpoint | ||||||||||||||
| const ws = new WebSocket(`${actorOrigin}/ws`); | ||||||||||||||
|
|
@@ -144,6 +153,47 @@ async function run() { | |||||||||||||
| }; | ||||||||||||||
| }); | ||||||||||||||
|
|
||||||||||||||
| // UDP | ||||||||||||||
| let res = await client.actor.get(actor.id, { | ||||||||||||||
| project: RIVET_PROJECT, | ||||||||||||||
| environment: RIVET_ENVIRONMENT, | ||||||||||||||
| }); | ||||||||||||||
|
Comment on lines
+157
to
+160
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: Unnecessary API call - the UDP port info is already available in the original
Suggested change
|
||||||||||||||
|
|
||||||||||||||
| console.log("Connecting to UDP echo server..."); | ||||||||||||||
| const udpPort = res.actor.network.ports.udp; | ||||||||||||||
| const udpServer = `${udpPort.hostname}:${udpPort.port}`; | ||||||||||||||
| console.log("UDP server address:", udpServer); | ||||||||||||||
|
|
||||||||||||||
| // Create a UDP socket | ||||||||||||||
| const udpClient = dgram.createSocket('udp4'); | ||||||||||||||
|
|
||||||||||||||
| // Send a message to the UDP echo server | ||||||||||||||
| const message = Buffer.from('Hello UDP server!'); | ||||||||||||||
| udpClient.send(message, udpPort.port, udpPort.hostname, (err) => { | ||||||||||||||
| if (err) { | ||||||||||||||
| console.error("Error sending UDP message:", err); | ||||||||||||||
| udpClient.close(); | ||||||||||||||
| } else { | ||||||||||||||
| console.log("UDP message sent"); | ||||||||||||||
| } | ||||||||||||||
| }); | ||||||||||||||
|
|
||||||||||||||
| // Listen for a response | ||||||||||||||
| udpClient.on('message', (msg, rinfo) => { | ||||||||||||||
| console.log(`UDP message received: ${msg.toString()}`); | ||||||||||||||
| console.log(`From: ${rinfo.address}:${rinfo.port}`); | ||||||||||||||
| udpClient.close(); | ||||||||||||||
| }); | ||||||||||||||
|
|
||||||||||||||
| udpClient.on('error', (err) => { | ||||||||||||||
| console.error("UDP client error:", err); | ||||||||||||||
| udpClient.close(); | ||||||||||||||
| }); | ||||||||||||||
|
|
||||||||||||||
| udpClient.on('close', () => { | ||||||||||||||
| console.log("UDP connection closed"); | ||||||||||||||
| }); | ||||||||||||||
|
Comment on lines
+167
to
+195
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: UDP operations are not properly awaited - wrap in Promise to ensure completion before test continues |
||||||||||||||
|
|
||||||||||||||
| console.log("Sleeping forever so you can debug"); | ||||||||||||||
| await new Promise((resolve) => setTimeout(resolve, 100_000_000)); | ||||||||||||||
| } catch (error) { | ||||||||||||||
|
|
||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logic: dgram is a built-in Node.js module and should not be added as an npm dependency. Remove this line as it's not needed.