storj/pkg/storage/segments/peek_test.go
Natalie Villasana a4bb03d0da
adds inline segment support for segmentstore (#162)
* begins adding inline segment support for segmentstore

* adds PeekThresholdReader struct plus Read and isInline methods

* moves PeekThresholdReader to peek.go, adds more simplified Read function

* adds PeekThresholdReader tests

* reverts Read function to earlier version, updates tests to use ReadFull instead

* Get function now handles inline type pointers

* adds correct type Size and ExpirationDate to inline segment

* fixes return value in Put func error condition

* moves thresholdBuf and Read tests into a table test

* adds border case test, fixes redundant parts

* passes sizedReader size to makeRemotePointer
2018-08-01 16:25:06 -04:00

83 lines
2.5 KiB
Go

// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package segments
import (
"bytes"
"io"
"testing"
"github.com/stretchr/testify/assert"
)
func TestThresholdBufAndRead(t *testing.T) {
for _, tt := range []struct {
name string
file []byte
thresholdSize int
expectedIsRemote bool
outputBufLen int
readsToEnd bool
}{
{"Test strictly less than threshold: ", []byte("abcdefghijklmnopqrstuvwxyz"), 30, false, 30, true},
{"Test strictly greater than threshold: ", []byte("abcdefghijklmnopqrstuvwxyz"), 10, true, 30, true},
{"Test read less than threshold buf: ", []byte("abcdefghijklmnopqrstuvwxyz"), 20, true, 10, false},
{"Test empty file: ", []byte(""), 10, false, 10, true},
{"Test threshold size == len(file): ", []byte("abcdefghijklmnopqrstuvwxyz"), 26, false, 26, true},
} {
ioReader := bytes.NewReader(tt.file)
p := NewPeekThresholdReader(ioReader)
isRemote, err := p.IsLargerThan(tt.thresholdSize)
assert.Equal(t, tt.expectedIsRemote, isRemote, tt.name)
assert.NoError(t, err, tt.name)
outputBuf := make([]byte, tt.outputBufLen)
n, err := io.ReadFull(p, outputBuf)
if tt.readsToEnd && err != nil && err != io.EOF && err != io.ErrUnexpectedEOF {
t.Fatalf(tt.name, "unexpected err from ReadFull:\n%s", err.Error())
} else if !tt.readsToEnd && err != nil {
t.Fatalf(tt.name, "unexpected err from ReadFull:\n%s", err.Error())
}
if tt.readsToEnd && n != len(tt.file) {
t.Fatalf(tt.name, "expected size of n to equal length of file")
}
if !tt.readsToEnd && n != tt.outputBufLen {
t.Fatalf(tt.name, "expected n to equal length of outputBuf")
}
if !bytes.Equal(outputBuf[:n], tt.file[:n]) {
t.Fatalf(tt.name, "expected data in outputBuf to match data in file")
}
}
}
func TestMultipleIsLargerCall(t *testing.T) {
file := []byte("abcdefghijklmnopqrstuvwxyz")
ioReader := bytes.NewReader(file)
p := NewPeekThresholdReader(ioReader)
_, err := p.IsLargerThan(20)
assert.NoError(t, err)
_, err = p.IsLargerThan(20)
if err == nil {
t.Fatal("expected to err because multiple call")
}
}
func TestIsLargerThanCalledAfterRead(t *testing.T) {
file := []byte("abcdefghijklmnopqrstuvwxyz")
ioReader := bytes.NewReader(file)
p := NewPeekThresholdReader(ioReader)
outputBuf := make([]byte, 10)
_, err := p.Read(outputBuf)
assert.NoError(t, err)
_, err = p.IsLargerThan(20)
if err == nil {
t.Fatal("expected to err because IsLargerThan called after Read")
}
}