|
| 1 | +package registry |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "sync" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +// --- newDeltaLog ---------------------------------------------------------- |
| 10 | + |
| 11 | +func TestNewDeltaLogInitialState(t *testing.T) { |
| 12 | + dl := newDeltaLog() |
| 13 | + if dl == nil { |
| 14 | + t.Fatal("newDeltaLog returned nil") |
| 15 | + } |
| 16 | + if dl.Len() != 0 { |
| 17 | + t.Fatalf("Len: %d", dl.Len()) |
| 18 | + } |
| 19 | + if got := dl.CurrentSeq(); got != 0 { |
| 20 | + t.Fatalf("CurrentSeq before any Append: %d, want 0 (nextSeq=1 → 1-1=0)", got) |
| 21 | + } |
| 22 | + if cap(dl.entries) != 1024 { |
| 23 | + t.Fatalf("entries cap: %d, want 1024", cap(dl.entries)) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +// --- Append --------------------------------------------------------------- |
| 28 | + |
| 29 | +func TestAppendReturnsMonotonicSeq(t *testing.T) { |
| 30 | + dl := newDeltaLog() |
| 31 | + data := json.RawMessage(`{"k":1}`) |
| 32 | + s1 := dl.Append(DeltaRegister, 10, data) |
| 33 | + s2 := dl.Append(DeltaHeartbeat, 20, nil) |
| 34 | + s3 := dl.Append(DeltaDeregister, 30, nil) |
| 35 | + if s1 != 1 || s2 != 2 || s3 != 3 { |
| 36 | + t.Fatalf("seq sequence: %d %d %d, want 1 2 3", s1, s2, s3) |
| 37 | + } |
| 38 | + if dl.Len() != 3 { |
| 39 | + t.Fatalf("Len: %d", dl.Len()) |
| 40 | + } |
| 41 | + if dl.CurrentSeq() != 3 { |
| 42 | + t.Fatalf("CurrentSeq: %d", dl.CurrentSeq()) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func TestAppendPopulatesFields(t *testing.T) { |
| 47 | + dl := newDeltaLog() |
| 48 | + raw := json.RawMessage(`{"hello":"world"}`) |
| 49 | + dl.Append(DeltaRegister, 42, raw) |
| 50 | + dl.mu.Lock() |
| 51 | + e := dl.entries[0] |
| 52 | + dl.mu.Unlock() |
| 53 | + if e.SeqNo != 1 || e.Type != DeltaRegister || e.NodeID != 42 || string(e.Data) != `{"hello":"world"}` { |
| 54 | + t.Fatalf("entry: %+v", e) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func TestAppendTrimsOldestWhenExceedingMax(t *testing.T) { |
| 59 | + dl := newDeltaLog() |
| 60 | + // Fill beyond maxDeltaLogSize (10000) — add exactly +5 over max. |
| 61 | + total := maxDeltaLogSize + 5 |
| 62 | + for i := 0; i < total; i++ { |
| 63 | + dl.Append(DeltaHeartbeat, uint32(i), nil) |
| 64 | + } |
| 65 | + if dl.Len() != maxDeltaLogSize { |
| 66 | + t.Fatalf("Len after overflow: %d, want %d", dl.Len(), maxDeltaLogSize) |
| 67 | + } |
| 68 | + // Oldest 5 entries (SeqNos 1..5) should have been dropped. |
| 69 | + // Now the smallest seq in the log should be 6. |
| 70 | + dl.mu.Lock() |
| 71 | + first := dl.entries[0].SeqNo |
| 72 | + last := dl.entries[len(dl.entries)-1].SeqNo |
| 73 | + dl.mu.Unlock() |
| 74 | + if first != 6 { |
| 75 | + t.Fatalf("oldest retained SeqNo: %d, want 6", first) |
| 76 | + } |
| 77 | + if last != uint64(total) { |
| 78 | + t.Fatalf("newest SeqNo: %d, want %d", last, total) |
| 79 | + } |
| 80 | + if dl.CurrentSeq() != uint64(total) { |
| 81 | + t.Fatalf("CurrentSeq: %d, want %d", dl.CurrentSeq(), total) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func TestAppendConcurrentProducesUniqueMonotonicSeqs(t *testing.T) { |
| 86 | + dl := newDeltaLog() |
| 87 | + const goroutines = 10 |
| 88 | + const per = 50 |
| 89 | + var wg sync.WaitGroup |
| 90 | + seqs := make(chan uint64, goroutines*per) |
| 91 | + for g := 0; g < goroutines; g++ { |
| 92 | + wg.Add(1) |
| 93 | + go func() { |
| 94 | + defer wg.Done() |
| 95 | + for i := 0; i < per; i++ { |
| 96 | + seqs <- dl.Append(DeltaHeartbeat, 0, nil) |
| 97 | + } |
| 98 | + }() |
| 99 | + } |
| 100 | + wg.Wait() |
| 101 | + close(seqs) |
| 102 | + |
| 103 | + seen := make(map[uint64]bool, goroutines*per) |
| 104 | + for s := range seqs { |
| 105 | + if seen[s] { |
| 106 | + t.Fatalf("duplicate seq %d", s) |
| 107 | + } |
| 108 | + seen[s] = true |
| 109 | + } |
| 110 | + if len(seen) != goroutines*per { |
| 111 | + t.Fatalf("seqs: %d, want %d", len(seen), goroutines*per) |
| 112 | + } |
| 113 | + // Must be contiguous from 1..N. |
| 114 | + for i := uint64(1); i <= uint64(goroutines*per); i++ { |
| 115 | + if !seen[i] { |
| 116 | + t.Fatalf("missing seq %d", i) |
| 117 | + } |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +// --- Since ---------------------------------------------------------------- |
| 122 | + |
| 123 | +func TestSinceEmptyLogReturnsNil(t *testing.T) { |
| 124 | + dl := newDeltaLog() |
| 125 | + if got := dl.Since(0); got != nil { |
| 126 | + t.Fatalf("Since on empty log: %v, want nil", got) |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +func TestSinceTooOldReturnsNil(t *testing.T) { |
| 131 | + dl := newDeltaLog() |
| 132 | + // Forge the log to start at SeqNo=100 — simulates a log that has trimmed old entries. |
| 133 | + dl.mu.Lock() |
| 134 | + dl.entries = []DeltaEntry{ |
| 135 | + {SeqNo: 100}, {SeqNo: 101}, {SeqNo: 102}, |
| 136 | + } |
| 137 | + dl.nextSeq = 103 |
| 138 | + dl.mu.Unlock() |
| 139 | + |
| 140 | + // Asking for Since(50) is before oldest → nil, caller must fall back to full snapshot. |
| 141 | + if got := dl.Since(50); got != nil { |
| 142 | + t.Fatalf("Since(50) too old: %v, want nil", got) |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +func TestSinceUpToDateReturnsEmptyNonNil(t *testing.T) { |
| 147 | + dl := newDeltaLog() |
| 148 | + dl.Append(DeltaRegister, 1, nil) // seq 1 |
| 149 | + dl.Append(DeltaRegister, 2, nil) // seq 2 |
| 150 | + got := dl.Since(2) // nothing newer than 2 |
| 151 | + if got == nil { |
| 152 | + t.Fatalf("Since(2) when current=2 should return empty non-nil slice (up-to-date signal)") |
| 153 | + } |
| 154 | + if len(got) != 0 { |
| 155 | + t.Fatalf("Since(2): %d entries, want 0", len(got)) |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +func TestSinceReturnsOnlyNewerEntries(t *testing.T) { |
| 160 | + dl := newDeltaLog() |
| 161 | + for i := uint32(1); i <= 5; i++ { |
| 162 | + dl.Append(DeltaHeartbeat, i, nil) // seqs 1..5 |
| 163 | + } |
| 164 | + got := dl.Since(2) |
| 165 | + if len(got) != 3 { |
| 166 | + t.Fatalf("Since(2): %d entries, want 3 (3,4,5)", len(got)) |
| 167 | + } |
| 168 | + for i, want := range []uint64{3, 4, 5} { |
| 169 | + if got[i].SeqNo != want { |
| 170 | + t.Fatalf("got[%d].SeqNo=%d, want %d", i, got[i].SeqNo, want) |
| 171 | + } |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +func TestSinceReturnsCopyNotAlias(t *testing.T) { |
| 176 | + dl := newDeltaLog() |
| 177 | + dl.Append(DeltaRegister, 1, nil) // seq 1 |
| 178 | + dl.Append(DeltaRegister, 2, nil) // seq 2 |
| 179 | + dl.Append(DeltaRegister, 3, nil) // seq 3 |
| 180 | + // Since(1) returns entries with SeqNo > 1, i.e. seq 2 and seq 3. |
| 181 | + got := dl.Since(1) |
| 182 | + if len(got) != 2 { |
| 183 | + t.Fatalf("expected 2 entries, got %d", len(got)) |
| 184 | + } |
| 185 | + got[0].SeqNo = 999 // mutate returned slice |
| 186 | + got[1].NodeID = 999 |
| 187 | + // Internal state must be unaffected. |
| 188 | + again := dl.Since(1) |
| 189 | + // Internal should still reflect the values passed to Append (seq 2,3 / nodeID 2,3). |
| 190 | + if again[0].SeqNo != 2 || again[1].SeqNo != 3 || again[1].NodeID != 3 { |
| 191 | + t.Fatalf("mutation leaked into internal entries: %+v", again) |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +func TestSinceBoundaryAtOldestEntry(t *testing.T) { |
| 196 | + dl := newDeltaLog() |
| 197 | + for i := uint32(1); i <= 3; i++ { |
| 198 | + dl.Append(DeltaHeartbeat, i, nil) |
| 199 | + } |
| 200 | + // sinceSeq == oldest.SeqNo exactly → NOT too-old, should return entries > 1. |
| 201 | + got := dl.Since(1) |
| 202 | + if len(got) != 2 { |
| 203 | + t.Fatalf("Since(1) at exact oldest: %d entries, want 2 (2,3)", len(got)) |
| 204 | + } |
| 205 | + if got[0].SeqNo != 2 { |
| 206 | + t.Fatalf("first entry SeqNo: %d, want 2", got[0].SeqNo) |
| 207 | + } |
| 208 | +} |
| 209 | + |
| 210 | +// --- CurrentSeq + Len ----------------------------------------------------- |
| 211 | + |
| 212 | +func TestCurrentSeqTracksNextSeq(t *testing.T) { |
| 213 | + dl := newDeltaLog() |
| 214 | + for i := 1; i <= 4; i++ { |
| 215 | + dl.Append(DeltaRegister, uint32(i), nil) |
| 216 | + if dl.CurrentSeq() != uint64(i) { |
| 217 | + t.Fatalf("after %d Appends CurrentSeq=%d want %d", i, dl.CurrentSeq(), i) |
| 218 | + } |
| 219 | + } |
| 220 | +} |
| 221 | + |
| 222 | +func TestCurrentSeqReturnsZeroWhenNextSeqZero(t *testing.T) { |
| 223 | + // Explicitly test the `nextSeq == 0` guard branch. |
| 224 | + dl := &deltaLog{nextSeq: 0} |
| 225 | + if dl.CurrentSeq() != 0 { |
| 226 | + t.Fatalf("CurrentSeq with nextSeq=0: %d, want 0", dl.CurrentSeq()) |
| 227 | + } |
| 228 | +} |
| 229 | + |
| 230 | +func TestLenReflectsActualCount(t *testing.T) { |
| 231 | + dl := newDeltaLog() |
| 232 | + if dl.Len() != 0 { |
| 233 | + t.Fatalf("empty Len: %d", dl.Len()) |
| 234 | + } |
| 235 | + for i := 0; i < 7; i++ { |
| 236 | + dl.Append(DeltaHeartbeat, 0, nil) |
| 237 | + if dl.Len() != i+1 { |
| 238 | + t.Fatalf("Len after %d Appends: %d, want %d", i+1, dl.Len(), i+1) |
| 239 | + } |
| 240 | + } |
| 241 | +} |
0 commit comments