forked from tpjg/goriakpbc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rfile_test.go
107 lines (102 loc) · 3.2 KB
/
rfile_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package riak
import (
"github.com/bmizerany/assert"
"io"
"testing"
)
func TestRFile(t *testing.T) {
client := setupConnection(t)
assert.T(t, client != nil)
f, err := client.CreateFile("rfile_test.go", "test", "text/plain", 1024)
assert.T(t, err == nil)
assert.T(t, f != nil)
b, err := f.Write([]byte{'1', '2', '3', '4', '5', '6', '7', '8'})
assert.T(t, err == nil)
assert.T(t, b == 8)
// Check the root was updated
assert.T(t, f.root.Meta["chunk_count"] == "1")
// Add a full chunk
extra := make([]byte, 1024)
b, err = f.Write(extra)
assert.T(t, err == nil)
assert.T(t, b == 1024)
f.Meta()["mymeta"] = "meta1"
f.Indexes()["myindex_bin"] = []string{"index1"}
f.Indexes()["mymultiindex_bin"] = []string{"multiindex1a", "multiindex1b"}
f.Flush()
// Now rewind and change some content
f.Seek(0, 0)
b, err = f.Write([]byte{'a', 'b', 'c'}) // Overwriting first three bytes
assert.T(t, err == nil)
assert.T(t, b == 3)
f.Seek(1024, 0)
b, err = f.Write([]byte{'d', 'e', 'f'}) // Overwriting first three bytes in 2nd chunk
assert.T(t, err == nil)
assert.T(t, b == 3)
// Seek to the end
bp, err := f.Seek(0, 2)
assert.T(t, err == nil)
assert.T(t, bp == 1032)
b, err = f.Write([]byte{'z'}) // Append one byte
assert.T(t, err == nil)
assert.T(t, b == 1)
// Test the Reader functionality as well as OpenFile
f, err = client.OpenFile("rfile_test.go", "test")
assert.T(t, err == nil)
buf := make([]byte, 1024)
f.Seek(0, 0)
// Read the first chunk
b, err = f.Read(buf)
assert.T(t, err == nil)
assert.T(t, b == 1024)
assert.T(t, buf[0] == 'a')
assert.T(t, buf[1] == 'b')
assert.T(t, buf[2] == 'c')
assert.T(t, buf[3] == '4')
assert.T(t, buf[7] == '8')
assert.T(t, buf[8] == 0)
// Read the second chunk
b, err = f.Read(buf)
assert.T(t, err == io.EOF)
assert.T(t, b == 9)
assert.T(t, buf[0] == 'd')
assert.T(t, buf[1] == 'e')
assert.T(t, buf[2] == 'f')
assert.T(t, buf[3] == 0)
assert.T(t, buf[8] == 'z')
// Check the meta and indexes
assert.T(t, f.Meta()["mymeta"] == "meta1")
assert.T(t, f.Indexes()["myindex_bin"][0] == "index1")
assert.T(t, len(f.Indexes()["mymultiindex_bin"]) == 2)
// Now there should be three Riak objects: test, test-000000 and test-000001 in the rfile_test.go bucket
// Check <test> and cleanup
obj, err := GetFrom("rfile_test.go", "test")
assert.T(t, err == nil)
assert.T(t, obj.ContentType == "text/plain")
assert.T(t, obj.Meta["chunk_size"] == "1024")
assert.T(t, obj.Meta["chunk_count"] == "2")
obj.Destroy()
// Check <test-0000000> and cleanup
obj, err = GetFrom("rfile_test.go", "test-000000")
assert.T(t, err == nil)
assert.T(t, obj.ContentType == "text/plain")
assert.T(t, len(obj.Data) == 1024)
assert.T(t, obj.Data[0] == 'a')
assert.T(t, obj.Data[1] == 'b')
assert.T(t, obj.Data[2] == 'c')
assert.T(t, obj.Data[3] == '4')
assert.T(t, obj.Data[7] == '8')
assert.T(t, obj.Data[8] == 0)
obj.Destroy()
// Check <test-0000001> and cleanup
obj, err = GetFrom("rfile_test.go", "test-000001")
assert.T(t, err == nil)
assert.T(t, obj.ContentType == "text/plain")
assert.T(t, len(obj.Data) == 9)
assert.T(t, obj.Data[0] == 'd')
assert.T(t, obj.Data[1] == 'e')
assert.T(t, obj.Data[2] == 'f')
assert.T(t, obj.Data[3] == 0)
assert.T(t, obj.Data[8] == 'z')
obj.Destroy()
}