Files
zip/stream_test.go
T
zeekayandHanzo Dev 5f71d2496b The router is three nouns, and every one of them is concrete
App, Group, Operation. A group is a prefix and what holds under it, an operation
is one typed route declared there, and the verb methods are generic methods —
which Go 1.27 allows on a concrete type and still refuses on an interface. So In
and Out come from the handler and the caller writes no type arguments:

	users := app.Group("/v1/users")
	users.Post("/", api.Create)

	mint := users.Group("/mint").With(middleware.Mint(gate))
	mint.Post("/deposit", api.Deposit)

A Group is a CLOSED HANDLE: two unexported fields, and no method that hands back
the app, the parent or the fiber underneath. A subsystem given the group at its
own prefix cannot reach above it, so confinement is a property of the type
instead of a rule something has to police — which is what a host needs in order
to hand out a bounded surface and know what comes back.

That is bought by giving up the router interface, deliberately. Router and
OpTarget are gone, and with them the ability to implement a route table outside
this package: a decorator is no longer a router, a gate is middleware on a group,
and a bound is a path. The five package-level verb functions go the same way —
a registration is a method on the thing it registers into.

Raw carries what is not a typed operation: a stream, an upgrade, a probe, a
foreign subtree. It names its method, and it is the longer spelling because
opting out of the typed model is the exception and should read like one.



Co-authored-by: Hanzo Dev <[email protected]>
2026-09-17 01:39:17 -07:00

119 lines
2.8 KiB
Go

package zip_test
import (
"bufio"
"strings"
"testing"
"time"
"github.com/valyala/fasthttp"
"github.com/zap-proto/http"
"github.com/zap-proto/zip"
)
// TestListenZAP_Streams proves the streaming transport is wired THROUGH the
// framework: a zip route that uses c.SendStreamWriter (SSE) pushes each event
// over ListenZAP as it flushes — real server→client streaming over ZAP, end to
// end, with no per-handler transport code. This is the bidirectional brick made
// available to every zip app for free.
func TestListenZAP_Streams(t *testing.T) {
const n = 3
release := make(chan struct{}, n)
app := zip.New(zip.Config{AppName: "streamer", DisableStartupMessage: true})
app.Raw("GET", "/events", func(c *zip.Ctx) error {
return c.SendStreamWriter(func(w *bufio.Writer) {
for i := 0; i < n; i++ {
<-release
_, _ = w.WriteString("data: e" + string(rune('0'+i)) + "\n\n")
_ = w.Flush()
}
})
})
addr := freeAddr(t)
go func() { _ = app.Listen(addr) }() // bare addr = ZAP
defer func() { _ = app.Shutdown() }()
// Wait for the ZAP listener.
tr := http.Dial("tcp", addr)
defer tr.CloseIdleConnections()
for i := 0; i < 50; i++ {
req := fasthttp.AcquireRequest()
resp := fasthttp.AcquireResponse()
req.SetRequestURI("/health")
req.Header.SetMethod("GET")
err := tr.Do(req, resp)
fasthttp.ReleaseRequest(req)
fasthttp.ReleaseResponse(resp)
if err == nil {
break
}
time.Sleep(40 * time.Millisecond)
}
req := fasthttp.AcquireRequest()
resp := fasthttp.AcquireResponse()
defer fasthttp.ReleaseRequest(req)
defer fasthttp.ReleaseResponse(resp)
req.SetRequestURI("/events")
req.Header.SetMethod("GET")
if err := tr.Do(req, resp); err != nil {
t.Fatalf("Do /events: %v", err)
}
if !resp.IsBodyStream() {
t.Fatal("zip route did not stream over ZAP — got a buffered response")
}
br := bufio.NewReader(resp.BodyStream())
for i := 0; i < n; i++ {
release <- struct{}{}
got, err := readOneEvent(br)
if err != nil {
t.Fatalf("event %d: %v", i, err)
}
if want := "data: e" + string(rune('0'+i)); !strings.Contains(got, want) {
t.Fatalf("event %d = %q, want %q", i, got, want)
}
}
}
func readOneEvent(br *bufio.Reader) (string, error) {
ch := make(chan struct {
s string
e error
}, 1)
go func() {
var b strings.Builder
for {
line, err := br.ReadString('\n')
b.WriteString(line)
if err != nil {
ch <- struct {
s string
e error
}{b.String(), err}
return
}
if line == "\n" && b.Len() > 1 {
ch <- struct {
s string
e error
}{b.String(), nil}
return
}
}
}()
select {
case r := <-ch:
return r.s, r.e
case <-time.After(2 * time.Second):
return "", &streamTimeout{}
}
}
type streamTimeout struct{}
func (*streamTimeout) Error() string { return "timed out waiting for streamed event over ZAP" }