|
1 | 1 | package codex |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "io" |
4 | 5 | "os" |
5 | 6 | "path/filepath" |
6 | 7 | "runtime" |
7 | 8 | "strings" |
8 | 9 | "testing" |
| 10 | + "testing/iotest" |
9 | 11 |
|
10 | 12 | "github.com/merefield/codexometer/internal/diagnostics" |
11 | 13 | ) |
12 | 14 |
|
| 15 | +type coverageReadCloser struct { |
| 16 | + io.Reader |
| 17 | + closeErr error |
| 18 | + closed bool |
| 19 | +} |
| 20 | + |
| 21 | +func (f *coverageReadCloser) Close() error { |
| 22 | + f.closed = true |
| 23 | + return f.closeErr |
| 24 | +} |
| 25 | + |
| 26 | +func TestUsageCoverageReadAndCloseFailuresAreLoggedAndRetried(t *testing.T) { |
| 27 | + for _, failure := range []string{"read", "close", "read and close"} { |
| 28 | + t.Run(failure, func(t *testing.T) { |
| 29 | + dir := t.TempDir() |
| 30 | + path, logPath := filepath.Join(dir, "config.toml"), filepath.Join(dir, "error.log") |
| 31 | + const config = "[features]\nasync_compaction = true\n" |
| 32 | + if err := os.WriteFile(path, []byte(config), 0600); err != nil { |
| 33 | + t.Fatal(err) |
| 34 | + } |
| 35 | + info, err := os.Stat(path) |
| 36 | + if err != nil { |
| 37 | + t.Fatal(err) |
| 38 | + } |
| 39 | + closeLog, err := diagnostics.Install(logPath, false) |
| 40 | + if err != nil { |
| 41 | + t.Fatal(err) |
| 42 | + } |
| 43 | + t.Cleanup(func() { closeLog() }) |
| 44 | + file := &coverageReadCloser{Reader: strings.NewReader(config)} |
| 45 | + if strings.Contains(failure, "read") { |
| 46 | + file.Reader = iotest.ErrReader(os.ErrPermission) |
| 47 | + } |
| 48 | + if strings.Contains(failure, "close") { |
| 49 | + file.closeErr = os.ErrPermission |
| 50 | + } |
| 51 | + r := &LiveUsageReader{localFeatureChecks: make(map[string]localFeatureCheck)} |
| 52 | + enabled, issue := r.readLocalAsyncCompaction(file, info, diagnostics.Fields{Path: path, Session: "thread"}) |
| 53 | + if enabled || issue != "background usage configuration could not be verified" || !file.closed { |
| 54 | + t.Fatal("filesystem failure did not block verification and close the file", enabled, issue, file.closed) |
| 55 | + } |
| 56 | + if len(r.localFeatureChecks) != 0 { |
| 57 | + t.Fatal("failed filesystem operation was cached", r.localFeatureChecks) |
| 58 | + } |
| 59 | + if err := closeLog(); err != nil { |
| 60 | + t.Fatal(err) |
| 61 | + } |
| 62 | + data, err := os.ReadFile(logPath) |
| 63 | + if err != nil { |
| 64 | + t.Fatal(err) |
| 65 | + } |
| 66 | + for _, operation := range []string{"read", "close"} { |
| 67 | + if strings.Contains(failure, operation) && !strings.Contains(string(data), `"msg":"`+operation+`"`) { |
| 68 | + t.Fatal("filesystem failure was not logged", operation, string(data)) |
| 69 | + } |
| 70 | + } |
| 71 | + // Retry the same path and unchanged metadata after the transient |
| 72 | + // failure. A successful read must now detect and cache the flag. |
| 73 | + if enabled, issue := r.localAsyncCompaction(path, "thread"); !enabled || issue != "" || !r.localFeatureChecks[path].enabled { |
| 74 | + t.Fatal("unchanged configuration was not successfully retried", enabled, issue) |
| 75 | + } |
| 76 | + }) |
| 77 | + } |
| 78 | +} |
| 79 | + |
13 | 80 | func TestUsageCoverageLogsFilesystemCauseWithoutChangingTheGuard(t *testing.T) { |
14 | 81 | home := t.TempDir() |
15 | 82 | logPath, config := filepath.Join(home, "error.log"), filepath.Join(home, "config.toml") |
|
0 commit comments