Simplify lounge

This commit is contained in:
Brahim Hadriche committed 2026-10-19 11:37:27 -04:00
1 parent 6822f12336
commit 0c31ee1354
9 files changed
+88 -245

No files matched your search

@@ -75,7 +75,7 @@ namespace Tests
m.assertEqual(backchannel.closeReason, LoungeCloseReason.Empty)
end function
@it("returns the commands of the whole frames of an answer that ends mid-frame, and reports what is left")
@it("returns the commands of the whole frames of an answer that ends mid-frame, and reports where")
function _()
backchannel = new LoungeBackchannel()
commands = m.PollOnce(backchannel, "/lounge/partial", { status: 200, body: m.Frame(`[[7,["noop"]]]`) + `40\n[[8,` })
@@ -84,7 +84,49 @@ namespace Tests
m.assertEqual(backchannel.closeReason, LoungeCloseReason.Complete)
event = m.global.telemetry.LogEvent
m.assertEqual(event.fingerprint, "lounge-partial-frame")
m.assertTrue(event.message.InStr("7 bytes left") <> -1, event.message)
m.assertTrue(event.message.InStr("at byte 17 of 24") <> -1, event.message)
end function
@it("counts a frame's length in bytes, so a multi-byte character does not shift the next frame")
function _()
backchannel = new LoungeBackchannel()
titleBytes = CreateObject("roByteArray")
titleBytes.FromHexString("636166C3A9")
title = titleBytes.ToAsciiString()
commands = backchannel.StartSession(m.Frame(`[[0,["setTitle","${title}"]]]`) + m.Frame(`[[1,["noop"]]]`))
m.assertEqual(commands.Count(), 2)
m.assertEqual(commands[0].args, title)
m.assertEqual(commands[1].type, "noop")
m.assertEmpty(m.global.telemetry.LogEvent)
end function
@it("reads a length line that ends in CR LF")
function _()
backchannel = new LoungeBackchannel()
commands = backchannel.StartSession(`\r\n14\r\n[[0,["noop"]]]`)
m.assertEqual(commands.Count(), 1)
m.assertEqual(commands[0].type, "noop")
m.assertEmpty(m.global.telemetry.LogEvent)
end function
@it("stops at a frame without a valid length, keeps the frames before it, and reports it")
function _()
for each badFrame in [`0\n`, `x\n[[1,["noop"]]]`, `14 [[1,["noop"]]]`, `99999999999999999999\n[[1,["noop"]]]`]
m.global.telemetry.LogEvent = {}
backchannel = new LoungeBackchannel()
commands = backchannel.StartSession(m.Frame(`[[0,["noop"]]]`) + badFrame + m.Frame(`[[2,["noop"]]]`))
m.assertEqual(commands.Count(), 1, badFrame)
m.assertEqual(commands[0].index, 0, badFrame)
m.assertEqual(m.global.telemetry.LogEvent.fingerprint, "lounge-partial-frame", badFrame)
end for
end function
@it("ends a 2xx answer that is not framed as empty, and reports it")
function _()
backchannel = new LoungeBackchannel()
m.assertEmpty(m.PollOnce(backchannel, "/lounge/unframed", { status: 200, body: "<html></html>" }))
m.assertEqual(backchannel.closeReason, LoungeCloseReason.Empty)
m.assertEqual(m.global.telemetry.LogEvent.fingerprint, "lounge-partial-frame")
end function
@it("skips the commands it already received")
@@ -145,16 +187,14 @@ namespace Tests
backchannel = new LoungeBackchannel()
m.assertEmpty(m.PollOnce(backchannel, "/lounge/unknownsid", { status: 400, body: "Unknown SID" }))
m.assertEqual(backchannel.closeReason, LoungeCloseReason.Http)
m.assertEqual(backchannel.CloseLabel(), "http_400")
m.assertEqual(backchannel.statusCode, 400)
end function
@it("ends a poll the server drops with its curl code")
@it("ends a poll the server drops with the status the OS returns")
function _()
backchannel = new LoungeBackchannel()
m.assertEmpty(m.PollOnce(backchannel, "/lounge/dropped", { drop: true }))
m.assertEqual(backchannel.closeReason, LoungeCloseReason.Curl)
m.assertEqual(backchannel.CloseLabel(), "curl_52")
m.assertEqual(backchannel.closeReason, LoungeCloseReason.Http)
m.assertEqual(backchannel.statusCode, HttpClient.CurlErrors.CURLE_GOT_NOTHING)
end function
@@ -1,41 +0,0 @@
import "pkg:/components/Services/LoungeService/LoungeBytes.bs"
namespace Tests
@suite
class LoungeBytesTests extends Rooibos.BaseTestSuite
@describe("LoungeBytes byte helpers")
@it("converts a byte range to a string, byte-exact across a UTF-8 boundary")
function rangeToString()
' "Xcafé" where é is 2 bytes; extract "café" = bytes [1, end).
cafe = "caf" + m.Utf8(&hC3, &hA9)
bytes = m.Bytes("X" + cafe)
m.assertEqual(LoungeBytes.RangeToString(bytes, 1, bytes.Count()), cafe)
end function
@it("finds the first byte value at or after a position, or -1")
function indexOfByte()
bytes = m.Bytes("a,b,c")
m.assertEqual(LoungeBytes.IndexOfByte(bytes, 0, Asc(",")), 1)
m.assertEqual(LoungeBytes.IndexOfByte(bytes, 2, Asc(",")), 3)
m.assertEqual(LoungeBytes.IndexOfByte(bytes, 0, Asc("z")), -1)
end function
function Bytes(text as string) as object
bytes = CreateObject("roByteArray")
bytes.FromAsciiString(text)
return bytes
end function
function Utf8(b1 as integer, b2 as integer) as string
bytes = CreateObject("roByteArray")
bytes.Push(b1)
bytes.Push(b2)
return bytes.ToAsciiString()
end function
end class
end namespace
@@ -1,105 +0,0 @@
import "pkg:/components/Services/LoungeService/LoungeFraming.bs"
namespace Tests
@suite
class LoungeFramingTests extends Rooibos.BaseTestSuite
@describe("LoungeFraming.Extract")
@it("extracts a single complete frame and reports the consumed position")
function singleFrame()
result = LoungeFraming.Extract(m.Bytes(`5\nhello`), 0)
m.assertEqual(result.frames.Count(), 1)
m.assertEqual(result.frames[0], "hello")
m.assertEqual(result.nextPos, 7)
end function
@it("extracts multiple consecutive frames in one pass")
function multipleFrames()
result = LoungeFraming.Extract(m.Bytes(`5\nhello3\nfoo`), 0)
m.assertEqual(result.frames, ["hello", "foo"])
end function
@it("skips leading CR/LF between frames")
function leadingNewlines()
result = LoungeFraming.Extract(m.Bytes(`\r\n5\nhello`), 0)
m.assertEqual(result.frames, ["hello"])
end function
' The bug that motivated the rewrite: the length prefix is a BYTE count, but the old
' parser sliced with character-based string ops, so any multi-byte payload shifted every
' subsequent frame. A char-based slice of 5 from "café2..." keeps 5 characters ("café2")
' and desyncs; a byte-accurate slice keeps "café" (5 bytes) and frame 2 still lines up.
@it("slices payloads by byte length so multi-byte UTF-8 does not desync the stream")
function utf8ByteAccurate()
cafe = "caf" + m.Utf8(&hC3, &hA9) ' é is 2 bytes -> "café" is 5 bytes, 4 characters
body = m.Bytes(Str(m.ByteLen(cafe)).Trim() + Chr(10) + cafe + Str(m.ByteLen("ok")).Trim() + Chr(10) + "ok")
result = LoungeFraming.Extract(body, 0)
m.assertEqual(result.frames.Count(), 2, "a character-based slice would mis-split frame 2")
m.assertEqual(result.frames[0], cafe)
m.assertEqual(result.frames[1], "ok")
end function
@it("leaves a whole frame unconsumed when its payload has not fully arrived")
function partialPayload()
result = LoungeFraming.Extract(m.Bytes(`5\nhel`), 0)
m.assertEqual(result.frames.Count(), 0)
m.assertEqual(result.nextPos, 0)
end function
@it("consumes complete frames but retains an incomplete trailing frame")
function partialAfterComplete()
result = LoungeFraming.Extract(m.Bytes(`5\nhello2\nh`), 0)
m.assertEqual(result.frames, ["hello"])
m.assertEqual(result.nextPos, 7)
end function
@it("waits when the length line is not yet terminated")
function partialLengthLine()
result = LoungeFraming.Extract(m.Bytes("12"), 0)
m.assertEqual(result.frames.Count(), 0)
m.assertEqual(result.nextPos, 0)
end function
@it("resumes extraction from a caller-supplied position")
function resumeFromPosition()
' Index 7 is the start of the second frame ("3\nfoo").
result = LoungeFraming.Extract(m.Bytes(`5\nhello3\nfoo`), 7)
m.assertEqual(result.frames, ["foo"])
end function
@it("skips a non-positive length line instead of stalling")
function malformedLength()
result = LoungeFraming.Extract(m.Bytes(`0\n5\nhello`), 0)
m.assertEqual(result.frames, ["hello"])
end function
@it("returns no frames for an empty body")
function emptyBody()
result = LoungeFraming.Extract(m.Bytes(""), 0)
m.assertEqual(result.frames.Count(), 0)
m.assertEqual(result.nextPos, 0)
end function
function Bytes(text as string) as object
bytes = CreateObject("roByteArray")
bytes.FromAsciiString(text)
return bytes
end function
function ByteLen(text as string) as integer
return m.Bytes(text).Count()
end function
function Utf8(b1 as integer, b2 as integer) as string
bytes = CreateObject("roByteArray")
bytes.Push(b1)
bytes.Push(b2)
return bytes.ToAsciiString()
end function
end class
end namespace
@@ -14,7 +14,7 @@ namespace Tests
@it("retries a failed poll")
function _()
m.assertEqual(m.Step(LoungeCloseReason.Curl, -7, 0, false, false, 1000), LoungeSessionStep.Retry)
m.assertEqual(m.Step(LoungeCloseReason.Http, -7, 0, false, false, 1000), LoungeSessionStep.Retry)
m.assertEqual(m.Step(LoungeCloseReason.Timeout, 0, 0, false, false, 40000), LoungeSessionStep.Retry)
m.assertEqual(m.Step(LoungeCloseReason.Http, 503, 0, false, false, 1000), LoungeSessionStep.Retry)
m.assertEqual(m.Step(LoungeCloseReason.Empty, 200, 0, false, false, 1000), LoungeSessionStep.Retry)
@@ -42,11 +42,11 @@ namespace Tests
@it("drops a session that delivered no poll for the give-up time, whatever the failure")
function _()
m.assertEqual(m.Step(LoungeCloseReason.Curl, -7, 0, false, false, LoungeSession.GIVE_UP_MS), LoungeSessionStep.Drop)
m.assertEqual(m.Step(LoungeCloseReason.Http, -7, 0, false, false, LoungeSession.GIVE_UP_MS), LoungeSessionStep.Drop)
m.assertEqual(m.Step(LoungeCloseReason.Http, 400, 0, false, false, LoungeSession.GIVE_UP_MS), LoungeSessionStep.Drop)
m.assertEqual(m.Step(LoungeCloseReason.Http, 401, 0, false, false, LoungeSession.GIVE_UP_MS), LoungeSessionStep.Drop)
m.assertEqual(m.Step(LoungeCloseReason.Stalled, 200, 3, false, false, LoungeSession.GIVE_UP_MS), LoungeSessionStep.Drop)
m.assertEqual(m.Step(LoungeCloseReason.Curl, -7, 0, false, false, LoungeSession.GIVE_UP_MS - 1), LoungeSessionStep.Retry)
m.assertEqual(m.Step(LoungeCloseReason.Http, -7, 0, false, false, LoungeSession.GIVE_UP_MS - 1), LoungeSessionStep.Retry)
end function
@it("never drops a session whose poll just delivered")