archiver: Fix race condition triggered by TestArchiverAbortEarlyOnError
The Save methods of the BlobSaver, FileSaver and TreeSaver return early on when the archiver is stopped due to an error. For that they select on both the tomb.Dying() and context.Done() channels, which can lead to a race condition when the tomb is killed due to an error: The tomb first closes its Dying channel before canceling all child contexts. Archiver.SaveDir only aborts its execution once the context was canceled. When the tomb killing is paused between closing its Dying channel and canceling the child contexts, this lets the FileSaver/TreeSaver.Save methods return immediately, however, ScanDir still reads further files causing the test case to fail. As a killed tomb always cancels all child contexts and as the Savers always use a context bound to the tomb, it is sufficient to just use context.Done() as escape hatch in the Save functions. This fixes the mismatch between SaveDir and Save. Adjust the tests to use contexts bound to the tomb for all interactions with the Savers.
This commit is contained in:
parent
3ee6b8ec63
commit
bdf7ba20cb
6 changed files with 16 additions and 35 deletions
|
@ -38,12 +38,12 @@ func TestBlobSaver(t *testing.T) {
|
|||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
var tmb tomb.Tomb
|
||||
tmb, ctx := tomb.WithContext(ctx)
|
||||
saver := &saveFail{
|
||||
idx: repository.NewIndex(),
|
||||
}
|
||||
|
||||
b := NewBlobSaver(ctx, &tmb, saver, uint(runtime.NumCPU()))
|
||||
b := NewBlobSaver(ctx, tmb, saver, uint(runtime.NumCPU()))
|
||||
|
||||
var results []FutureBlob
|
||||
|
||||
|
@ -84,13 +84,13 @@ func TestBlobSaverError(t *testing.T) {
|
|||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
var tmb tomb.Tomb
|
||||
tmb, ctx := tomb.WithContext(ctx)
|
||||
saver := &saveFail{
|
||||
idx: repository.NewIndex(),
|
||||
failAt: int32(test.failAt),
|
||||
}
|
||||
|
||||
b := NewBlobSaver(ctx, &tmb, saver, uint(runtime.NumCPU()))
|
||||
b := NewBlobSaver(ctx, tmb, saver, uint(runtime.NumCPU()))
|
||||
|
||||
var results []FutureBlob
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue