MCPcopy Create free account
hub / github.com/golang/appengine / TestDelayedLogFlushing

Function TestDelayedLogFlushing

internal/api_test.go:269–330  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

267}
268
269func TestDelayedLogFlushing(t *testing.T) {
270 defer restoreEnvVar(logserviceEnvVarKey)()
271
272 testCases := []struct {
273 logToLogservice string
274 wantInitialFlushes int32
275 wantHeader string
276 wantEndFlushes int32
277 }{
278 {logToLogservice: "", wantHeader: "1", wantInitialFlushes: 1, wantEndFlushes: 2}, // default behavior
279 {logToLogservice: "1", wantHeader: "1", wantInitialFlushes: 1, wantEndFlushes: 2},
280 {logToLogservice: "0", wantHeader: "", wantInitialFlushes: 0, wantEndFlushes: 0},
281 }
282 for _, tc := range testCases {
283 t.Run(fmt.Sprintf("$%s=%q", logserviceEnvVarKey, tc.logToLogservice), func(t *testing.T) {
284 f, c, cleanup := setup()
285 defer cleanup()
286 os.Setenv(logserviceEnvVarKey, tc.logToLogservice)
287
288 path := "/slow_log_" + tc.logToLogservice
289
290 http.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
291 logC := WithContext(context.Background(), r)
292 Logf(logC, 1, "It's a lovely day.")
293 w.WriteHeader(200)
294 time.Sleep(1200 * time.Millisecond)
295 w.Write(make([]byte, 100<<10)) // write 100 KB to force HTTP flush
296 })
297
298 r := &http.Request{
299 Method: "GET",
300 URL: &url.URL{
301 Scheme: "http",
302 Path: path,
303 },
304 Header: c.req.Header,
305 Body: ioutil.NopCloser(bytes.NewReader(nil)),
306 }
307 w := httptest.NewRecorder()
308
309 handled := make(chan struct{})
310 go func() {
311 defer close(handled)
312 Middleware(http.DefaultServeMux).ServeHTTP(w, r)
313 }()
314 // Check that the log flush eventually comes in.
315 time.Sleep(1200 * time.Millisecond)
316 if got := atomic.LoadInt32(&f.LogFlushes); got != tc.wantInitialFlushes {
317 t.Errorf("After 1.2s: f.LogFlushes = %d, want %d", got, tc.wantInitialFlushes)
318 }
319
320 <-handled
321 const hdr = "X-AppEngine-Log-Flush-Count"
322 if got := w.HeaderMap.Get(hdr); got != tc.wantHeader {
323 t.Errorf("%s header = %q, want %q", hdr, got, tc.wantHeader)
324 }
325 if got := atomic.LoadInt32(&f.LogFlushes); got != tc.wantEndFlushes {
326 t.Errorf("After HTTP response: f.LogFlushes = %d, want %d", got, tc.wantEndFlushes)

Callers

nothing calls this directly

Calls 10

restoreEnvVarFunction · 0.85
setupFunction · 0.70
WithContextFunction · 0.70
LogfFunction · 0.70
MiddlewareFunction · 0.70
RunMethod · 0.45
WriteHeaderMethod · 0.45
WriteMethod · 0.45
ServeHTTPMethod · 0.45
GetMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…