| 1 | package docs_test |
| 2 | |
| 3 | // Realtime audit fixture (perf/native-realtime-audit, 2026-11-21). Not a |
| 4 | // gate: skipped unless DOCS_RT_AUDIT_DIR is set by |
| 5 | // native/benchmarks/realtime/run-e2e.py. It serves the real authenticated |
| 6 | // application on loopback, seeds one document with DOCS_RT_AUDIT_SEED (a |
| 7 | // Yjs update from fixtures/generate-large-doc.mjs), and appends one JSON |
| 8 | // line per request to $DOCS_RT_AUDIT_DIR/requests.jsonl: when it started |
| 9 | // and ended, method, path, bytes each way and status. The native probe |
| 10 | // (RealtimeAuditTests.testEndToEnd*) reads that log to count requests. |
| 11 | |
| 12 | import ( |
| 13 | "bytes" |
| 14 | "encoding/json" |
| 15 | "net" |
| 16 | "net/http" |
| 17 | "net/http/httptest" |
| 18 | "os" |
| 19 | "path/filepath" |
| 20 | "strings" |
| 21 | "sync" |
| 22 | "testing" |
| 23 | "time" |
| 24 | |
| 25 | "github.com/carlosframework/docs/internal/docs" |
| 26 | ) |
| 27 | |
| 28 | type auditWriter struct { |
| 29 | http.ResponseWriter |
| 30 | status int |
| 31 | bytes int64 |
| 32 | } |
| 33 | |
| 34 | func (w *auditWriter) WriteHeader(code int) { w.status = code; w.ResponseWriter.WriteHeader(code) } |
| 35 | func (w *auditWriter) Write(p []byte) (int, error) { |
| 36 | if w.status == 0 { |
| 37 | w.status = 200 |
| 38 | } |
| 39 | n, err := w.ResponseWriter.Write(p) |
| 40 | w.bytes += int64(n) |
| 41 | return n, err |
| 42 | } |
| 43 | func (w *auditWriter) Flush() { |
| 44 | if f, ok := w.ResponseWriter.(http.Flusher); ok { |
| 45 | f.Flush() |
| 46 | } |
| 47 | } |
| 48 | func (w *auditWriter) Unwrap() http.ResponseWriter { return w.ResponseWriter } |
| 49 | |
| 50 | type countingBody struct { |
| 51 | r interface { |
| 52 | Read([]byte) (int, error) |
| 53 | Close() error |
| 54 | } |
| 55 | n int64 |
| 56 | } |
| 57 | |
| 58 | func (b *countingBody) Read(p []byte) (int, error) { |
| 59 | n, err := b.r.Read(p) |
| 60 | b.n += int64(n) |
| 61 | return n, err |
| 62 | } |
| 63 | func (b *countingBody) Close() error { return b.r.Close() } |
| 64 | |
| 65 | func TestNativeRealtimeAuditServer(t *testing.T) { |
| 66 | dir := os.Getenv("DOCS_RT_AUDIT_DIR") |
| 67 | if dir == "" { |
| 68 | t.Skip("Run native/benchmarks/realtime/run-e2e.py") |
| 69 | } |
| 70 | listener, err := net.Listen("tcp", "127.0.0.1:0") |
| 71 | if err != nil { |
| 72 | t.Fatal(err) |
| 73 | } |
| 74 | defer listener.Close() |
| 75 | origin := "http://" + listener.Addr().String() |
| 76 | app, err := docs.NewForTestWith(t, docs.Config{AllowedAddresses: "audit@example.com", Origin: origin}) |
| 77 | if err != nil { |
| 78 | t.Fatal(err) |
| 79 | } |
| 80 | cookies := signInCookies(t, app, "audit@example.com") |
| 81 | create := httptest.NewRequest("POST", "/documents", strings.NewReader("title=Realtime+audit")) |
| 82 | create.Header.Set("Origin", origin) |
| 83 | create.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 84 | for _, cookie := range cookies { |
| 85 | create.AddCookie(cookie) |
| 86 | } |
| 87 | created := httptest.NewRecorder() |
| 88 | app.Handler().ServeHTTP(created, create) |
| 89 | if created.Code != http.StatusSeeOther { |
| 90 | t.Fatalf("create: status %d", created.Code) |
| 91 | } |
| 92 | sqid := strings.TrimPrefix(created.Header().Get("Location"), "/d/") |
| 93 | if seed := os.Getenv("DOCS_RT_AUDIT_SEED"); seed != "" { |
| 94 | update, err := os.ReadFile(seed) |
| 95 | if err != nil { |
| 96 | t.Fatal(err) |
| 97 | } |
| 98 | post := httptest.NewRequest("POST", "/d/"+sqid+"/updates", bytes.NewReader(update)) |
| 99 | post.Header.Set("Origin", origin) |
| 100 | post.Header.Set("Content-Type", "application/octet-stream") |
| 101 | for _, cookie := range cookies { |
| 102 | post.AddCookie(cookie) |
| 103 | } |
| 104 | posted := httptest.NewRecorder() |
| 105 | app.Handler().ServeHTTP(posted, post) |
| 106 | if posted.Code/100 != 2 { |
| 107 | t.Fatalf("seed: status %d %s", posted.Code, posted.Body.String()) |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | logFile, err := os.OpenFile(filepath.Join(dir, "requests.jsonl"), os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600) |
| 112 | if err != nil { |
| 113 | t.Fatal(err) |
| 114 | } |
| 115 | defer logFile.Close() |
| 116 | var mu sync.Mutex |
| 117 | base := time.Now() |
| 118 | inner := app.Handler() |
| 119 | handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 120 | start := time.Since(base) |
| 121 | body := &countingBody{r: r.Body} |
| 122 | r.Body = body |
| 123 | aw := &auditWriter{ResponseWriter: w} |
| 124 | // Stream lines are logged when the stream opens too, so an idle |
| 125 | // probe can see a reconnect without waiting for its end. |
| 126 | if strings.HasSuffix(r.URL.Path, "/stream") { |
| 127 | mu.Lock() |
| 128 | json.NewEncoder(logFile).Encode(map[string]any{"event": "open", "start_ms": float64(start.Microseconds()) / 1000, "method": r.Method, "path": r.URL.RequestURI()}) |
| 129 | mu.Unlock() |
| 130 | } |
| 131 | inner.ServeHTTP(aw, r) |
| 132 | end := time.Since(base) |
| 133 | mu.Lock() |
| 134 | defer mu.Unlock() |
| 135 | json.NewEncoder(logFile).Encode(map[string]any{ |
| 136 | "start_ms": float64(start.Microseconds()) / 1000, "end_ms": float64(end.Microseconds()) / 1000, |
| 137 | "method": r.Method, "path": r.URL.RequestURI(), "req_bytes": body.n, "resp_bytes": aw.bytes, |
| 138 | "status": aw.status, "gzip": r.Header.Get("Content-Encoding"), |
| 139 | }) |
| 140 | }) |
| 141 | server := httptest.NewUnstartedServer(handler) |
| 142 | server.Listener.Close() |
| 143 | server.Listener = listener |
| 144 | server.Start() |
| 145 | defer server.Close() |
| 146 | // NativeSession carries one "name=value"; any further cookies ride in |
| 147 | // its value after "; ", which makes the same Cookie header. |
| 148 | value := cookies[0].Value |
| 149 | names := []string{cookies[0].Name} |
| 150 | for _, c := range cookies[1:] { |
| 151 | value += "; " + c.Name + "=" + c.Value |
| 152 | names = append(names, c.Name) |
| 153 | } |
| 154 | t.Logf("cookies: %v", names) |
| 155 | config := map[string]any{"origin": server.URL, "sqid": sqid, "name": cookies[0].Name, "value": value, "base_unix_ms": base.UnixMilli()} |
| 156 | data, err := json.Marshal(config) |
| 157 | if err != nil { |
| 158 | t.Fatal(err) |
| 159 | } |
| 160 | if err := os.WriteFile(filepath.Join(dir, "connection.json"), data, 0600); err != nil { |
| 161 | t.Fatal(err) |
| 162 | } |
| 163 | deadline := time.Now().Add(20 * time.Minute) |
| 164 | for time.Now().Before(deadline) { |
| 165 | if _, err := os.Stat(filepath.Join(dir, "done")); err == nil { |
| 166 | return |
| 167 | } |
| 168 | time.Sleep(20 * time.Millisecond) |
| 169 | } |
| 170 | t.Fatal("audit never completed") |
| 171 | } |
| 172 | |