waitForFileContent polls until a file has the expected content or timeout expires. This is needed because writebackCache defers data upload to background goroutines, so there is a brief window after close() where the file may not yet be readable.
(t *testing.T, path string, expected []byte, timeout time.Duration)
| 36 | // This is needed because writebackCache defers data upload to background goroutines, |
| 37 | // so there is a brief window after close() where the file may not yet be readable. |
| 38 | func waitForFileContent(t *testing.T, path string, expected []byte, timeout time.Duration) { |
| 39 | t.Helper() |
| 40 | deadline := time.Now().Add(timeout) |
| 41 | var lastErr error |
| 42 | for time.Now().Before(deadline) { |
| 43 | actual, err := os.ReadFile(path) |
| 44 | if err == nil && bytes.Equal(expected, actual) { |
| 45 | return |
| 46 | } |
| 47 | if err != nil { |
| 48 | lastErr = err |
| 49 | } else { |
| 50 | lastErr = fmt.Errorf("content mismatch: got %d bytes, want %d bytes", len(actual), len(expected)) |
| 51 | } |
| 52 | time.Sleep(200 * time.Millisecond) |
| 53 | } |
| 54 | t.Fatalf("file %s did not have expected content within %v: %v", path, timeout, lastErr) |
| 55 | } |
| 56 | |
| 57 | // waitForFileSize polls until a file reports the expected size or timeout expires. |
| 58 | func waitForFileSize(t *testing.T, path string, expectedSize int64, timeout time.Duration) { |
no test coverage detected