From 54d395f53e344a3cf3861c0e3d2f82ad14ecc602 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Fri, 6 Feb 2026 07:08:29 +0700 Subject: all: improve the Server-Sent Events (SSE) output In the ExecResponse, store the event in the Output instead of message data, so the server can iterate the Output directly and pass it to WriteEvent directly. The event ID now start at 1 with type "begin". This is to minimize confusion when comparing empty Last-Event-ID from client, which is equal to 0. --- http_server_test.go | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) (limited to 'http_server_test.go') diff --git a/http_server_test.go b/http_server_test.go index f6a32aa..d5cb270 100644 --- a/http_server_test.go +++ b/http_server_test.go @@ -222,6 +222,9 @@ func TestHttpServer_Execute(t *testing.T) { log.Fatal(err) } }() + t.Cleanup(func() { + aww.Stop() + }) err = libnet.WaitAlive(`tcp`, address, 10*time.Second) if err != nil { @@ -299,25 +302,19 @@ func TestHttpServer_Execute(t *testing.T) { for ever { select { case ev = <-ssec.C: - if len(ev.Type) != 0 { - fmt.Fprintf(&buf, "event: %s\n", ev.Type) - } - if len(ev.Data) != 0 { - fmt.Fprintf(&buf, "data: %q\n", ev.Data) - } - if len(ev.ID) != 0 { - fmt.Fprintf(&buf, "id: %s\n", ev.ID) - } - buf.WriteByte('\n') + fmt.Fprintf(&buf, "event: %s\ndata: %q\nid: %s\n\n", + ev.Type, ev.Data, ev.ID) if ev.Type == "end" { ever = false break } + timeWait.Reset(time.Second) case <-timeWait.C: break } } + timeWait.Stop() test.Assert(t, `Execute tail`, string(tdata.Output[`local:/local.aww:1-:tail`]), buf.String()) } @@ -353,6 +350,9 @@ func TestHttpServer_ExecuteCancel(t *testing.T) { log.Fatal(err) } }() + t.Cleanup(func() { + aww.Stop() + }) err = libnet.WaitAlive(`tcp`, address, 10*time.Second) if err != nil { @@ -430,16 +430,8 @@ func TestHttpServer_ExecuteCancel(t *testing.T) { for ever { select { case ev = <-ssec.C: - if len(ev.Type) != 0 { - fmt.Fprintf(&buf, "event: %s\n", ev.Type) - } - if len(ev.Data) != 0 { - fmt.Fprintf(&buf, "data: %q\n", ev.Data) - } - if len(ev.ID) != 0 { - fmt.Fprintf(&buf, "id: %s\n", ev.ID) - } - buf.WriteByte('\n') + fmt.Fprintf(&buf, "event: %s\ndata: %q\nid: %s\n\n", + ev.Type, ev.Data, ev.ID) if ev.ID == `1` { testDoExecuteCancel(t, tdata, cl, execResp.ID) @@ -449,10 +441,12 @@ func TestHttpServer_ExecuteCancel(t *testing.T) { ever = false break } + timeWait.Reset(time.Second) case <-timeWait.C: break } } + timeWait.Stop() test.Assert(t, `Execute cancel`, string(tdata.Output[`local:/cancel.aww:1-:tail`]), buf.String()) } -- cgit v1.3