@@ -95,21 +95,27 @@ func (m *mockCloser) isClosed() bool {
9595 return m .closed
9696}
9797
98+ // mockReadOnlyCloser combines mockCloser with Read-Only capabilities for testing.
99+ type mockReadOnlyCloser struct {
100+ * mockCloser
101+ io.Reader
102+ }
103+
98104// mockReadWriteCloser combines mockCloser with Read/Write capabilities for testing.
99105type mockReadWriteCloser struct {
100106 * mockCloser
101107 io.Reader
102108 io.Writer
103109}
104110
105- func newMockReadCloser (failCount int , err error ) * mockReadWriteCloser {
106- return & mockReadWriteCloser {
111+ func newMockReadOnlyCloser (failCount int , err error ) * mockReadOnlyCloser {
112+ return & mockReadOnlyCloser {
107113 mockCloser : newMockCloser (failCount , err ),
108114 Reader : bytes .NewReader ([]byte ("test data" )),
109115 }
110116}
111117
112- func newMockWriteCloser (failCount int , err error ) * mockReadWriteCloser {
118+ func newMockReadWriteCloser (failCount int , err error ) * mockReadWriteCloser {
113119 return & mockReadWriteCloser {
114120 mockCloser : newMockCloser (failCount , err ),
115121 Writer : & bytes.Buffer {},
@@ -289,7 +295,7 @@ func TestCloseWithRetryContextCancellation(t *testing.T) {
289295 assert .Less (t , closer .getCloseCalled (), maxFileCloseRetries + 1 )
290296}
291297
292- // TestBackupFileSourceCloseError tests error handling when source file close fails during backup.
298+ // TestBackupFileSourceCloseError tests error handling when a source file close fails during backup.
293299func TestBackupFileSourceCloseError (t * testing.T ) {
294300 ctx := context .Background ()
295301 logger := logutil .NewMemoryLogger ()
@@ -304,10 +310,10 @@ func TestBackupFileSourceCloseError(t *testing.T) {
304310 DataDir : tmpDir ,
305311 }
306312 be := & BuiltinBackupEngine {}
307- // Create a mock backup handle with a destination that closes successfully .
308- destCloser := newMockWriteCloser ( 0 , nil )
313+ // Create a mock backup handle with a source file that fails to close multiple times .
314+ sourceCloser := newMockReadWriteCloser ( 2 , errors . New ( "failed to close source file" ) )
309315 bh := newMockBackupHandle ()
310- bh .addFileReturn = destCloser
316+ bh .addFileReturn = sourceCloser
311317 params := BackupParams {
312318 Cnf : cnf ,
313319 Logger : logger ,
@@ -322,13 +328,13 @@ func TestBackupFileSourceCloseError(t *testing.T) {
322328 // backupFile should handle the error gracefully.
323329 err = be .backupFile (ctx , params , bh , fe , "0" )
324330
325- // The backup should succeed even if we can't perfectly verify the close behavior
326- // because we can't easily inject a failing close into the real file system
327- // This test mainly verifies the code path exists.
331+ // Should succeed after retries.
328332 assert .NoError (t , err )
333+ assert .Equal (t , 3 , sourceCloser .getCloseCalled ()) // 2 failures + 1 success
334+ assert .True (t , sourceCloser .isClosed ())
329335}
330336
331- // TestBackupFileDestinationCloseError tests error handling when destination file close fails during backup.
337+ // TestBackupFileDestinationCloseError tests error handling when a destination file close fails during backup.
332338func TestBackupFileDestinationCloseError (t * testing.T ) {
333339 ctx := context .Background ()
334340 logger := logutil .NewMemoryLogger ()
@@ -345,7 +351,7 @@ func TestBackupFileDestinationCloseError(t *testing.T) {
345351 }
346352 be := & BuiltinBackupEngine {}
347353 // Create a mock backup handle with a destination that fails to close multiple times.
348- destCloser := newMockWriteCloser (3 , errors .New ("failed to close destination" ))
354+ destCloser := newMockReadWriteCloser (3 , errors .New ("failed to close destination file " ))
349355 bh := newMockBackupHandle ()
350356 bh .addFileReturn = destCloser
351357 params := BackupParams {
@@ -377,19 +383,19 @@ func TestBackupFileDestinationCloseMaxRetries(t *testing.T) {
377383 logger := logutil .NewMemoryLogger ()
378384 // Create a temporary directory for test files.
379385 tmpDir := t .TempDir ()
380- // Create test source file
381- sourceFile := path .Join (tmpDir , "source .txt" )
386+ // Create test destination file.
387+ destFile := path .Join (tmpDir , "destination .txt" )
382388 content := []byte ("test content for max retries" )
383- err := os .WriteFile (sourceFile , content , 0644 )
389+ err := os .WriteFile (destFile , content , 0644 )
384390 require .NoError (t , err )
385391 // Create Mycnf pointing to our temp directory.
386392 cnf := & Mycnf {
387393 DataDir : tmpDir ,
388394 }
389395 be := & BuiltinBackupEngine {}
390- // Create a mock backup handle with a destination that always fails to close.
396+ // Create a mock backup handle with a destination file that always fails to close.
391397 destCloser := & mockReadWriteCloser {
392- mockCloser : newAlwaysFailingCloser (errors .New ("permanent close failure" )),
398+ mockCloser : newAlwaysFailingCloser (errors .New ("permanent file close failure" )),
393399 Writer : & bytes.Buffer {},
394400 }
395401 bh := newMockBackupHandle ()
@@ -402,14 +408,14 @@ func TestBackupFileDestinationCloseMaxRetries(t *testing.T) {
402408 }
403409 fe := & FileEntry {
404410 Base : backupData ,
405- Name : "source .txt" ,
411+ Name : "destination .txt" ,
406412 }
407413
408414 err = be .backupFile (ctx , params , bh , fe , "0" )
409415
410416 // Should fail due to close error (context deadline exceeded).
411417 assert .Error (t , err )
412- assert .Contains (t , err .Error (), "failed to close" )
418+ assert .Contains (t , err .Error (), "failed to close destination file " )
413419 // Should have attempted multiple times before timeout.
414420 assert .Greater (t , destCloser .getCloseCalled (), 1 )
415421 assert .False (t , destCloser .isClosed ())
@@ -470,7 +476,7 @@ func TestBackupManifestCloseError(t *testing.T) {
470476 Writer : & bytes.Buffer {},
471477 }
472478 } else {
473- wc = newMockWriteCloser (tc .failCount , errors .New ("transient write error" ))
479+ wc = newMockReadWriteCloser (tc .failCount , errors .New ("transient write error" ))
474480 }
475481 bh := newMockBackupHandle ()
476482 bh .addFileReturn = wc
@@ -499,14 +505,14 @@ func TestBackupManifestCloseError(t *testing.T) {
499505 }
500506}
501507
502- // TestRestoreFileSourceCloseError tests error handling when source file close fails during restore.
508+ // TestRestoreFileSourceCloseError tests error handling when a source file close fails during restore.
503509func TestRestoreFileSourceCloseError (t * testing.T ) {
504510 ctx := context .Background ()
505511 logger := logutil .NewMemoryLogger ()
506512 tmpDir := t .TempDir ()
507513 be := & BuiltinBackupEngine {}
508- // Create a mock backup handle with a source that fails to close.
509- sourceCloser := newMockReadCloser (2 , errors .New ("failed to close source" ))
514+ // Create a mock backup handle with a file source that fails to close.
515+ sourceCloser := newMockReadOnlyCloser (2 , errors .New ("failed to close source file " ))
510516 bh := newMockBackupHandle ()
511517 bh .readFileReturn = sourceCloser
512518 cnf := & Mycnf {
@@ -534,8 +540,8 @@ func TestRestoreFileSourceCloseError(t *testing.T) {
534540 assert .GreaterOrEqual (t , sourceCloser .getCloseCalled (), 1 )
535541}
536542
537- // TestRestoreFileDestinationCloseError tests error handling when destination file close fails during restore.
538- func TestRestoreFileDestinationCloseError (t * testing.T ) {
543+ // TestRestoreFileDestinationClose tests the happy path when closing a destination file during restore.
544+ func TestRestoreFileDestinationClose (t * testing.T ) {
539545 ctx := context .Background ()
540546 logger := logutil .NewMemoryLogger ()
541547 tmpDir := t .TempDir ()
@@ -590,8 +596,8 @@ func TestRestoreFileWithCloseRetriesIntegration(t *testing.T) {
590596 be := & BuiltinBackupEngine {}
591597 content := []byte ("integration test content for restore" )
592598 // Create a source that will fail to close a few times.
593- sourceCloser := & mockReadWriteCloser {
594- mockCloser : newMockCloser (1 , errors .New ("transient close error" )),
599+ sourceCloser := & mockReadOnlyCloser {
600+ mockCloser : newMockCloser (1 , errors .New ("transient file close error" )),
595601 Reader : bytes .NewReader (content ),
596602 }
597603 bh := newMockBackupHandle ()
0 commit comments