From 9981318d75b60a4e7cf9248a0dfb802ff6835bad Mon Sep 17 00:00:00 2001 From: Ahmet Alp Balkan Date: Sat, 17 Jan 2015 17:08:04 -0800 Subject: [PATCH] modify TestWriteReadLargeStreams to write in one shot --- storagedriver/testsuites/testsuites.go | 38 ++++++++++++++++++++------ 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/storagedriver/testsuites/testsuites.go b/storagedriver/testsuites/testsuites.go index c3229fcc..12a55244 100644 --- a/storagedriver/testsuites/testsuites.go +++ b/storagedriver/testsuites/testsuites.go @@ -259,16 +259,13 @@ func (suite *DriverSuite) TestWriteReadLargeStreams(c *check.C) { defer suite.StorageDriver.Delete(firstPart(filename)) checksum := sha1.New() - var offset int64 - var chunkSize int64 = 1024 * 1024 + var fileSize int64 = 5 * 1024 * 1024 * 1024 + + contents := newRandReader(fileSize) + written, err := suite.StorageDriver.WriteStream(filename, 0, io.TeeReader(contents, checksum)) + c.Assert(err, check.IsNil) + c.Assert(written, check.Equals, fileSize) - for i := 0; i < 5*1024; i++ { - contents := randomContents(chunkSize) - written, err := suite.StorageDriver.WriteStream(filename, offset, io.TeeReader(bytes.NewReader(contents), checksum)) - c.Assert(err, check.IsNil) - c.Assert(written, check.Equals, chunkSize) - offset += chunkSize - } reader, err := suite.StorageDriver.ReadStream(filename, 0) c.Assert(err, check.IsNil) @@ -1088,6 +1085,29 @@ func randomContents(length int64) []byte { return b } +type randReader struct { + r int64 + m sync.Mutex +} + +func (rr *randReader) Read(p []byte) (n int, err error) { + rr.m.Lock() + defer rr.m.Unlock() + for i := 0; i < len(p) && rr.r > 0; i++ { + p[i] = byte(rand.Intn(255)) + n++ + rr.r-- + } + if rr.r == 0 { + err = io.EOF + } + return +} + +func newRandReader(n int64) *randReader { + return &randReader{r: n} +} + func firstPart(filePath string) string { if filePath == "" { return "/"