Rewrite lounge service long poller

This commit is contained in:
Brahim Hadriche committed 2026-06-30 16:09:04 -04:00
1 parent 17ff6b940b
commit c81f5cd9e1
11 files changed
+935 -381

No files matched your search

@@ -0,0 +1,153 @@
import "pkg:/components/Services/LoungeService/HttpChunkedDecoder.bs"
namespace Tests
@suite
class HttpChunkedDecoderTests extends Rooibos.BaseTestSuite
@describe("HttpChunkedDecoder.Pump")
@it("decodes a single-chunk 200 response and reaches Done")
function singleChunk()
r = m.DecodeWhole(`HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nhello\r\n0\r\n\r\n`)
m.assertEqual(r.body, "hello")
m.assertEqual(r.decoder.statusCode, 200)
m.assertTrue(r.decoder.IsDone())
m.assertFalse(r.decoder.IsError())
end function
@it("concatenates multiple chunks into one body")
function multipleChunks()
r = m.DecodeWhole(`HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nhello\r\n6\r\n world\r\n0\r\n\r\n`)
m.assertEqual(r.body, "hello world")
m.assertTrue(r.decoder.IsDone())
end function
' A chunk boundary need not align with a socket read: the same response delivered one byte
' per read must reassemble identically.
@it("reassembles a response delivered one byte per socket read")
function bytewise()
r = m.Drive(`HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nhello\r\n6\r\n world\r\n0\r\n\r\n`, 1)
m.assertEqual(r.body, "hello world")
m.assertTrue(r.decoder.IsDone())
end function
@it("parses a hex chunk size greater than 9")
function hexChunkSize()
payload = "0123456789abcdef" ' 16 bytes -> chunk size "10" in hex
r = m.DecodeWhole(`HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n10\r\n` + payload + `\r\n0\r\n\r\n`)
m.assertEqual(r.body, payload)
end function
@it("ignores chunk-size extensions after a semicolon")
function chunkExtensions()
r = m.DecodeWhole(`HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n5;foo=bar\r\nhello\r\n0\r\n\r\n`)
m.assertEqual(r.body, "hello")
end function
@it("extracts a chunk byte-exactly when the body holds multi-byte UTF-8")
function utf8Chunk()
' "café" is 4 characters but 5 UTF-8 bytes; the chunk size counts bytes.
r = m.DecodeWhole(`HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n5\r\ncafé\r\n0\r\n\r\n`)
m.assertEqual(r.body, "café")
m.assertTrue(r.decoder.IsDone())
end function
@it("flags a non-200 status as an error carrying the code")
@params("410 Gone", 410)
@params("400 Bad Request", 400)
@params("401 Unauthorized", 401)
function non200(statusLine as string, expectedCode as integer)
r = m.DecodeWhole(`HTTP/1.1 ` + statusLine + `\r\n\r\n`)
m.assertTrue(r.decoder.IsError())
m.assertFalse(r.decoder.IsDone())
m.assertEqual(r.decoder.statusCode, expectedCode)
m.assertEqual(r.decoder.errorReason, "", "an HTTP status error is fingerprinted by code, not a slug")
end function
@it("flags a 200 response that is not chunked as an error rather than crashing")
function nonChunked()
r = m.DecodeWhole(`HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\nhello`)
m.assertTrue(r.decoder.IsError())
m.assertEqual(r.decoder.errorReason, "unsupported_encoding")
end function
@it("flags a malformed status line with a stable error reason")
function malformedStatus()
r = m.DecodeWhole(`HELLO not http\r\n\r\n`)
m.assertTrue(r.decoder.IsError())
m.assertEqual(r.decoder.errorReason, "malformed_status")
end function
@it("waits for more bytes when the status line is incomplete")
function partialStatus()
r = m.Drive("HTTP/1.1 20", 0)
m.assertFalse(r.decoder.IsError())
m.assertFalse(r.decoder.IsDone())
m.assertEqual(r.body, "")
end function
@it("errors on a chunk size that overflows a 32-bit int")
function chunkOverflow()
r = m.DecodeWhole(`HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\nFFFFFFFF\r\nx`)
m.assertTrue(r.decoder.IsError())
m.assertEqual(r.decoder.errorReason, "chunk_overflow")
end function
function DecodeWhole(response as string) as object
return m.Drive(response, 0)
end function
function Drive(response as string, readSize as integer) as object
decoder = new HttpChunkedDecoder()
socket = new LoungeMockSocket(m.Bytes(response), readSize)
out = CreateObject("roByteArray")
while socket.GetCountRcvBuf() > 0 and not decoder.IsDone() and not decoder.IsError()
decoder.Pump(socket, out)
end while
return { decoder: decoder, body: out.ToAsciiString() }
end function
function Bytes(text as string) as object
bytes = CreateObject("roByteArray")
bytes.FromAsciiString(text)
return bytes
end function
end class
' Feeds `bytes` to the decoder, at most `readSize` per Receive (0 = all at once), so a test
' can drive fragmented reads.
class LoungeMockSocket
private data
private readPos = 0
private readSize = 0
function new(bytes as object, readSize as integer)
m.data = bytes
m.readSize = readSize
end function
function GetCountRcvBuf() as integer
remaining = m.data.Count() - m.readPos
if remaining <= 0
return 0
end if
if m.readSize > 0 and m.readSize < remaining
return m.readSize
end if
return remaining
end function
function Receive(ba as object, index as integer, length as integer) as integer
count = 0
while count < length and m.readPos < m.data.Count()
ba[index + count] = m.data[m.readPos]
count += 1
m.readPos += 1
end while
return count
end function
end class
end namespace
@@ -0,0 +1,59 @@
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 CRLF at or after a position")
function indexOfCrlf()
bytes = m.Bytes(`ab\r\ncd\r\n`)
m.assertEqual(LoungeBytes.IndexOfCrlf(bytes, 0), 2)
m.assertEqual(LoungeBytes.IndexOfCrlf(bytes, 3), 6)
end function
@it("returns -1 when no CRLF is present")
function indexOfCrlfMissing()
m.assertEqual(LoungeBytes.IndexOfCrlf(m.Bytes("abc"), 0), -1)
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
@it("returns a tail copy from a position to the end")
function tail()
tail = LoungeBytes.Tail(m.Bytes("hello"), 2)
m.assertEqual(tail.ToAsciiString(), "llo")
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
@@ -0,0 +1,105 @@
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