|
| 1 | +package events |
| 2 | + |
| 3 | +// Coverage tests for the group-commit flusher branches: the syncOnce |
| 4 | +// no-op paths (not dirty / already syncing / closed), error retention on |
| 5 | +// failed sync, the default flush interval fallback, and Flush/Close on |
| 6 | +// a closed sink. |
| 7 | + |
| 8 | +import ( |
| 9 | + "errors" |
| 10 | + "os" |
| 11 | + "sync/atomic" |
| 12 | + "testing" |
| 13 | + "time" |
| 14 | +) |
| 15 | + |
| 16 | +// syncOnce with nothing pending must not invoke the sync function. |
| 17 | +func TestJSONLSinkSyncOnce_NotDirtyIsNoop(t *testing.T) { |
| 18 | + s, _, syncs := newCountingSink(t) |
| 19 | + s.flushInterval = time.Hour |
| 20 | + s.syncOnce() |
| 21 | + if got := atomic.LoadInt64(syncs); got != 0 { |
| 22 | + t.Fatalf("syncs = %d for a clean sink, want 0", got) |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +// A sync error must keep the sink dirty so the pending writes are |
| 27 | +// retried on the next tick instead of being silently declared durable. |
| 28 | +func TestJSONLSinkSyncOnce_ErrorKeepsDirty(t *testing.T) { |
| 29 | + s, _, syncs := newCountingSink(t) |
| 30 | + s.flushInterval = time.Hour |
| 31 | + |
| 32 | + var fail int32 |
| 33 | + s.syncFn = func() error { |
| 34 | + atomic.AddInt64(syncs, 1) |
| 35 | + if atomic.LoadInt32(&fail) == 1 { |
| 36 | + return errors.New("simulated EIO") |
| 37 | + } |
| 38 | + return nil |
| 39 | + } |
| 40 | + |
| 41 | + atomic.StoreInt32(&fail, 1) |
| 42 | + if err := s.Write(ev(1)); err != nil { |
| 43 | + t.Fatalf("Write: %v", err) |
| 44 | + } |
| 45 | + s.syncOnce() // fails, must stay dirty |
| 46 | + s.syncOnce() // fails again, must still stay dirty |
| 47 | + |
| 48 | + atomic.StoreInt32(&fail, 0) |
| 49 | + s.syncOnce() // succeeds, clears dirty |
| 50 | + if got := atomic.LoadInt64(syncs); got != 3 { |
| 51 | + t.Fatalf("syncs = %d, want 3 (two retries then success)", got) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +// Flush after Close must return os.ErrClosed, not panic or fsync a dead |
| 56 | +// file handle. |
| 57 | +func TestJSONLSinkFlush_AfterCloseReturnsErrClosed(t *testing.T) { |
| 58 | + s, _, _ := newCountingSink(t) |
| 59 | + if err := s.Close(); err != nil { |
| 60 | + t.Fatalf("Close: %v", err) |
| 61 | + } |
| 62 | + if err := s.Flush(); !errors.Is(err, os.ErrClosed) { |
| 63 | + t.Fatalf("Flush after Close = %v, want os.ErrClosed", err) |
| 64 | + } |
| 65 | + if err := s.Write(ev(1)); !errors.Is(err, os.ErrClosed) { |
| 66 | + t.Fatalf("Write after Close = %v, want os.ErrClosed", err) |
| 67 | + } |
| 68 | + // Double Close is idempotent. |
| 69 | + if err := s.Close(); err != nil { |
| 70 | + t.Fatalf("second Close = %v, want nil", err) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +// A zero flushInterval must fall back to the default rather than |
| 75 | +// spinning a zero-period ticker. |
| 76 | +func TestJSONLSink_ZeroIntervalUsesDefault(t *testing.T) { |
| 77 | + s, _, _ := newCountingSink(t) |
| 78 | + s.flushInterval = 0 |
| 79 | + if err := s.Write(ev(1)); err != nil { |
| 80 | + t.Fatalf("Write: %v", err) |
| 81 | + } |
| 82 | + // The flusher must start lazily with the default interval; just |
| 83 | + // verify it stops cleanly. |
| 84 | + if s.flushCh == nil { |
| 85 | + t.Fatal("flusher did not start after first write") |
| 86 | + } |
| 87 | + if err := s.Close(); err != nil { |
| 88 | + t.Fatalf("Close: %v", err) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +// Writes landing while a sync is in flight must re-mark the sink dirty |
| 93 | +// so the next tick commits them (syncing-guard branch). |
| 94 | +func TestJSONLSink_WriteDuringSyncStaysDirty(t *testing.T) { |
| 95 | + s, path, _ := newCountingSink(t) |
| 96 | + s.flushInterval = time.Hour |
| 97 | + |
| 98 | + release := make(chan struct{}) |
| 99 | + s.syncFn = func() error { |
| 100 | + <-release |
| 101 | + return nil |
| 102 | + } |
| 103 | + if err := s.Write(ev(1)); err != nil { |
| 104 | + t.Fatalf("Write 1: %v", err) |
| 105 | + } |
| 106 | + done := make(chan struct{}) |
| 107 | + go func() { |
| 108 | + defer close(done) |
| 109 | + s.syncOnce() // blocks inside syncFn with syncing=true |
| 110 | + }() |
| 111 | + // Wait until the sync is in flight, then write behind it. |
| 112 | + time.Sleep(20 * time.Millisecond) |
| 113 | + if err := s.Write(ev(2)); err != nil { |
| 114 | + t.Fatalf("Write 2: %v", err) |
| 115 | + } |
| 116 | + close(release) |
| 117 | + <-done |
| 118 | + |
| 119 | + s.mu.Lock() |
| 120 | + dirty := s.dirty |
| 121 | + s.mu.Unlock() |
| 122 | + if !dirty { |
| 123 | + t.Fatal("write during in-flight sync did not re-mark the sink dirty") |
| 124 | + } |
| 125 | + s.mu.Lock() |
| 126 | + s.dirty = false // clean shutdown without invoking the gated syncFn |
| 127 | + s.mu.Unlock() |
| 128 | + if err := s.Close(); err != nil { |
| 129 | + t.Fatalf("Close: %v", err) |
| 130 | + } |
| 131 | + if _, err := os.Stat(path); err != nil { |
| 132 | + t.Fatalf("sink file missing: %v", err) |
| 133 | + } |
| 134 | +} |
0 commit comments