Skip to content

Commit

Permalink
Added some tests for large file write / read-back handling
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Ferquel <[email protected]>
  • Loading branch information
simonferquel committed Oct 6, 2016
1 parent d76341b commit de8158b
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions api/go-datakit/client_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package datakit

import (
"bytes"
"io"
"log"
"testing"

Expand Down Expand Up @@ -48,6 +50,11 @@ func TestInit(t *testing.T) {
t.Fatalf("Mkdir failed: %v", err)
}
filePath := append(path, "filename")
largeFilePath := append(path, "largefile")
var largeDataInput []byte
for ix := 0; ix < client.session.MaxReadSize()+150; ix++ {
largeDataInput = append(largeDataInput, byte(ix))
}
err = client.Remove(ctx, filePath...)
if err != nil {
t.Fatalf("Remove failed: %v", err)
Expand All @@ -74,4 +81,28 @@ func TestInit(t *testing.T) {
}
file.Close(ctx)
file.Close(ctx) // should be idempotent

file, err = client.Create(ctx, largeFilePath...)
if err != nil {
t.Fatalf("Create %v failed: %v", filePath, err)
}
defer file.Close(ctx)
n, err = file.NewIOWriter(ctx, 0).Write(largeDataInput)
if err != nil {
t.Fatalf("Write failed: %v", err)
}
if n != len(largeDataInput) {
t.Fatalf("Write was only partial: %v", err)
}
readBackData := make([]byte, len(largeDataInput)+2) // make sure reported length when ReadAll is called has the right value
n, err = io.ReadFull(file.NewIOReader(ctx, 0), readBackData)
if err != nil && err != io.EOF && err != io.ErrUnexpectedEOF {
t.Fatalf("Read failed: %v", err)
}
if n != len(largeDataInput) {
t.Fatalf("Failed to read back the number of bytes we wrote")
}
if bytes.Compare(largeDataInput, readBackData[:n]) != 0 {
t.Fatalf("The message we read back was different to the message we wrote")
}
}

0 comments on commit de8158b

Please sign in to comment.