Skip to content

Commit

Permalink
Assert OpenReader from file does not panic after closure
Browse files Browse the repository at this point in the history
Write a test that asserts reader instantiated from `OpenReader` do not
panic and instead error gracefully if underlying IO is closed.

Relates to #211
  • Loading branch information
masih committed Aug 20, 2021
1 parent e4019f0 commit ed281f9
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions v2/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,40 @@ func TestReader_WithCarV2Consistency(t *testing.T) {
}
}

func TestOpenReader_DoesNotPanicForReadersCreatedBeforeClosure(t *testing.T) {
subject, err := carv2.OpenReader("testdata/sample-wrapped-v2.car")
require.NoError(t, err)
dReaderBeforeClosure := subject.DataReader()
iReaderBeforeClosure := subject.IndexReader()
require.NoError(t, subject.Close())

buf := make([]byte, 1)
panicTest := func(r io.Reader) {
_, err := r.Read(buf)
require.EqualError(t, err, "mmap: closed")
}

require.NotPanics(t, func() { panicTest(dReaderBeforeClosure) })
require.NotPanics(t, func() { panicTest(iReaderBeforeClosure) })
}

func TestOpenReader_DoesNotPanicForReadersCreatedAfterClosure(t *testing.T) {
subject, err := carv2.OpenReader("testdata/sample-wrapped-v2.car")
require.NoError(t, err)
require.NoError(t, subject.Close())
dReaderAfterClosure := subject.DataReader()
iReaderAfterClosure := subject.IndexReader()

buf := make([]byte, 1)
panicTest := func(r io.Reader) {
_, err := r.Read(buf)
require.EqualError(t, err, "mmap: closed")
}

require.NotPanics(t, func() { panicTest(dReaderAfterClosure) })
require.NotPanics(t, func() { panicTest(iReaderAfterClosure) })
}

func requireNewCarV1ReaderFromV2File(t *testing.T, carV12Path string, zerLenAsEOF bool) *carv1.CarReader {
f, err := os.Open(carV12Path)
require.NoError(t, err)
Expand Down

0 comments on commit ed281f9

Please sign in to comment.