(t *testing.T)
| 267 | } |
| 268 | |
| 269 | func 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) |
nothing calls this directly
no test coverage detected
searching dependent graphs…