-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implemented gzip_reader, added tests
- Loading branch information
Showing
8 changed files
with
351 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package ioutils | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
|
||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
type GzipReader struct { | ||
gz io.ReadCloser | ||
r io.ReadCloser | ||
} | ||
|
||
func NewGzipReader(r io.ReadCloser, usePgzip bool) (*GzipReader, error) { | ||
gz, err := GetGzipReadCloser(r, usePgzip) | ||
if err != nil { | ||
if err := r.Close(); err != nil { | ||
log.Warn(). | ||
Err(err). | ||
Msg("error closing dump file") | ||
} | ||
return nil, fmt.Errorf("cannot create gzip reader: %w", err) | ||
} | ||
|
||
return &GzipReader{ | ||
gz: gz, | ||
r: r, | ||
}, nil | ||
|
||
} | ||
|
||
func (r *GzipReader) Read(p []byte) (n int, err error) { | ||
return r.gz.Read(p) | ||
} | ||
|
||
func (r *GzipReader) Close() error { | ||
var lastErr error | ||
if err := r.gz.Close(); err != nil { | ||
lastErr = fmt.Errorf("error closing gzip reader: %w", err) | ||
log.Warn(). | ||
Err(err). | ||
Msg("error closing gzip reader") | ||
} | ||
if err := r.r.Close(); err != nil { | ||
lastErr = fmt.Errorf("error closing dump file: %w", err) | ||
log.Warn(). | ||
Err(err). | ||
Msg("error closing dump file") | ||
} | ||
return lastErr | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package ioutils | ||
|
||
import ( | ||
"bytes" | ||
"compress/gzip" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type readCloserMock struct { | ||
*bytes.Buffer | ||
closeCallCount int | ||
} | ||
|
||
func (r *readCloserMock) Close() error { | ||
r.closeCallCount++ | ||
return nil | ||
} | ||
|
||
func TestNewGzipReader_Read(t *testing.T) { | ||
data := `20383 24ca7574-0adb-4b17-8777-93f5589dbea2 2017-12-13 13:46:49.39 | ||
20384 d0d4a55c-7752-453e-8334-772a889fb917 2017-12-13 13:46:49.453 | ||
20385 ac8617aa-5a2d-4bb8-a9a5-ed879a4b33cd 2017-12-13 13:46:49.5 | ||
` | ||
buf := new(bytes.Buffer) | ||
gzData := gzip.NewWriter(buf) | ||
_, err := gzData.Write([]byte(data)) | ||
require.NoError(t, err) | ||
err = gzData.Flush() | ||
require.NoError(t, err) | ||
err = gzData.Close() | ||
require.NoError(t, err) | ||
objSrc := &readCloserMock{Buffer: buf} | ||
r, err := NewGzipReader(objSrc, false) | ||
require.NoError(t, err) | ||
readBuf := make([]byte, 1024) | ||
n, err := r.Read(readBuf) | ||
require.NoError(t, err) | ||
require.Equal(t, []byte(data), readBuf[:n]) | ||
} | ||
|
||
func TestNewGzipReader_Close(t *testing.T) { | ||
data := "" | ||
buf := new(bytes.Buffer) | ||
gzData := gzip.NewWriter(buf) | ||
_, err := gzData.Write([]byte(data)) | ||
require.NoError(t, err) | ||
err = gzData.Flush() | ||
require.NoError(t, err) | ||
err = gzData.Close() | ||
objSrc := &readCloserMock{Buffer: buf, closeCallCount: 0} | ||
r, err := NewGzipReader(objSrc, false) | ||
require.NoError(t, err) | ||
err = r.Close() | ||
require.NoError(t, err) | ||
require.Equal(t, 1, objSrc.closeCallCount) | ||
gz := r.gz.(*gzip.Reader) | ||
_, err = gz.Read([]byte{}) | ||
require.Error(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.