@@ -2,6 +2,7 @@ package pruner
22
33import (
44 "context"
5+ "errors"
56 "os"
67 "path/filepath"
78 "testing"
@@ -160,6 +161,45 @@ func TestPurgeStopsOnCancelledContext(t *testing.T) {
160161 require .Zero (t , submitted )
161162}
162163
164+ // A block collection whose purge fails is re-queued, so the same blocks are drained and
165+ // counted again later. Counting them on the failed cycle too makes the total exceed the
166+ // blocks that were ever pruned.
167+ func TestBlockCounterIgnoresAFailedBlockPurge (t * testing.T ) {
168+ cols := DefaultCollectionConfig ()
169+
170+ q := NewEventQueue (cols )
171+ q .Push (cols .BlockCollection , testDocID (1 ))
172+ result := q .DrainDocs (1 )
173+ require .NotNil (t , result )
174+ require .Equal (t , 1 , result .BlockCount )
175+
176+ p := & Pruner {cfg : & Config {Enabled : true }, collections : cols , stopChan : make (chan struct {})}
177+ p .purgeDocs = func (context.Context , []client.DocID ) error {
178+ return errors .New ("purge failed" )
179+ }
180+
181+ require .NoError (t , p .purgeFromDrainResult (context .Background (), q , result ))
182+
183+ require .Zero (t , p .totalBlocksPruned , "blocks that were re-queued must not count as pruned" )
184+ require .Equal (t , 1 , q .BlockCount (), "guard: the blocks are back on the queue" )
185+ }
186+
187+ // The counter still moves on the path that did purge.
188+ func TestBlockCounterCountsASuccessfulBlockPurge (t * testing.T ) {
189+ cols := DefaultCollectionConfig ()
190+
191+ q := NewEventQueue (cols )
192+ q .Push (cols .BlockCollection , testDocID (1 ))
193+ result := q .DrainDocs (1 )
194+ require .NotNil (t , result )
195+
196+ p := & Pruner {cfg : & Config {Enabled : true }, collections : cols , stopChan : make (chan struct {})}
197+ p .purgeDocs = func (context.Context , []client.DocID ) error { return nil }
198+
199+ require .NoError (t , p .purgeFromDrainResult (context .Background (), q , result ))
200+ require .Equal (t , int64 (1 ), p .totalBlocksPruned )
201+ }
202+
163203// DrainDocs empties every collection up front, so a cycle that stops part-way has to re-queue the
164204// collections it never reached as well as the one it stopped on. Nothing else re-adds a document
165205// once it has replicated, so anything left behind is never pruned.
0 commit comments