From f9749638e3393f471d7e28362795689bf37cc023 Mon Sep 17 00:00:00 2001 From: Stefan Majewsky Date: Mon, 30 Apr 2018 14:14:56 +0200 Subject: revamp the LargeObject API I thought about this some more, and I believe the Writer-based approach in the previous version of the LargeObject API does not scale: It makes it very hard to write code that uploads segments without resorting to a buffer the same size as the segments. I don't want gigabyte-scale buffers filling up my RAM, so this commit switches to a different API based on Readers. LargeObject.Append() now behaves very similar to Object.Upload(), which I find quite nice. --- largeobject_test.go | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) (limited to 'largeobject_test.go') diff --git a/largeobject_test.go b/largeobject_test.go index fe781ab..724654a 100644 --- a/largeobject_test.go +++ b/largeobject_test.go @@ -18,7 +18,11 @@ package schwift -import "testing" +import ( + "bytes" + "io/ioutil" + "testing" +) func TestParseHTTPRange(t *testing.T) { testCases := []struct { @@ -62,3 +66,47 @@ func TestParseHTTPRange(t *testing.T) { } } } + +func TestSegmentingReader(t *testing.T) { + testCases := []struct { + input string + segments []string + }{ + {"abcdefghi", []string{"abc", "def", "ghi"}}, + {"abcdefgh", []string{"abc", "def", "gh"}}, + {"abcdefg", []string{"abc", "def", "g"}}, + } + + for _, tc := range testCases { + sr := segmentingReader{ + Reader: bytes.NewReader([]byte(tc.input)), + SegmentSizeBytes: 3, + } + + for _, expected := range tc.segments { + segment := sr.NextSegment() + if segment == nil { + t.Errorf("expected segment %q, but NextSegment() returned nil", expected) + break + } + actual, err := ioutil.ReadAll(segment) + if err != nil { + t.Errorf("expected segment %q, but got read error %q", expected, err.Error()) + break + } + if string(actual) != expected { + t.Errorf("expected segment %q, but got %q", expected, string(actual)) + } + } + + segment := sr.NextSegment() + if segment != nil { + actual, err := ioutil.ReadAll(segment) + if err == nil { + t.Errorf("expected no more segments, but got segment producing read error %q", err.Error()) + } else { + t.Errorf("expected no more segments, but got %q", string(actual)) + } + } + } +} -- cgit v1.2.3