Skip to content

Commit

Permalink
slicer: Fixed object upload in some cases
Browse files Browse the repository at this point in the history
The case when the reader returns io.EOF with the last data chunk. Slicer was closing the writer without payload writing

Signed-off-by: Evgenii Baidakov <evgenii@nspcc.io>
  • Loading branch information
smallhive committed Aug 8, 2023
1 parent 38848dc commit 5988a0b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
4 changes: 2 additions & 2 deletions object/slicer/slicer.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,9 @@ func slice(ctx context.Context, ow ObjectWriter, header object.Object, data io.R
if !errors.Is(err, io.EOF) {
return rootID, fmt.Errorf("read payload chunk: %w", err)
}
}

// no more data to read

if n == 0 {
if err = writer.Close(); err != nil {
return rootID, fmt.Errorf("writer close: %w", err)
}
Expand Down
31 changes: 31 additions & 0 deletions object/slicer/slicer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,23 @@ func testSlicer(t *testing.T, size, sizeLimit uint64) {
}
}

// eofOnLastChunkReader is a special reader for tests. It returns io.EOF with the last data portion.
type eofOnLastChunkReader struct {
Payload []byte
point int
}

func (l *eofOnLastChunkReader) Read(p []byte) (int, error) {
n := copy(p, l.Payload[l.point:])
l.point += n

if l.point == len(l.Payload) {
return n, io.EOF
}

return n, nil
}

func testSlicerByHeaderType(t *testing.T, checker *slicedObjectChecker, in input, opts slicer.Options) {
ctx := context.Background()

Expand Down Expand Up @@ -296,6 +313,20 @@ func testSlicerByHeaderType(t *testing.T, checker *slicedObjectChecker, in input
checker.chainCollector.verify(checker.input, rootID)
})

t.Run("slicer.Put, io.EOF in last chunk", func(t *testing.T) {
checker.chainCollector = newChainCollector(t)

var hdr object.Object
hdr.SetSessionToken(opts.Session())
hdr.SetContainerID(in.container)
hdr.SetOwnerID(&in.owner)
hdr.SetAttributes(in.attributes...)

rootID, err := slicer.Put(ctx, checker, hdr, checker.input.signer, &eofOnLastChunkReader{Payload: in.payload}, opts)
require.NoError(t, err)
checker.chainCollector.verify(checker.input, rootID)
})

t.Run("Slicer.InitPut", func(t *testing.T) {
checker.chainCollector = newChainCollector(t)

Expand Down

0 comments on commit 5988a0b

Please sign in to comment.