forked from couchbaselabs/cbgb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_server_test.go
76 lines (62 loc) · 1.54 KB
/
file_server_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
package main
import (
"encoding/json"
"errors"
"os"
"testing"
)
const testfileservicename = ",file_server_test"
func TestFileService(t *testing.T) {
defer os.Remove(testfileservicename)
in := map[string]interface{}{"b": "bee"}
fs := NewFileService(2)
defer fs.Close()
err := fs.Do(testfileservicename, os.O_CREATE|os.O_WRONLY,
func(f *os.File) error {
e := json.NewEncoder(f)
return e.Encode(in)
})
if err != nil {
t.Fatalf("Failed to encode and persist thing: %v", err)
}
out := map[string]interface{}{}
err = fs.Do(testfileservicename, os.O_RDONLY,
func(f *os.File) error {
d := json.NewDecoder(f)
return d.Decode(&out)
})
if err != nil {
t.Fatalf("Failed to read and decode thing: %v", err)
}
if len(in) != len(out) {
t.Fatalf("in != out: %v != %v", in, out)
}
for k := range in {
if in[k] != out[k] {
t.Errorf("Error at %v: %v (%T) != %v (%T)",
k, in[k], in[k], out[k], out[k])
}
}
}
func TestFileServiceOpenError(t *testing.T) {
fs := NewFileService(2)
defer fs.Close()
err := fs.Do(",idonotexist", os.O_RDONLY, func(f *os.File) error {
t.Fatalf("Expected to avoid running function due to error")
return nil
})
if !os.IsNotExist(err) {
t.Fatalf("Unexpected error opening missing file: %v", err)
}
}
func TestFileServiceFuncError(t *testing.T) {
fs := NewFileService(2)
defer fs.Close()
e := errors.New("Expected error")
err := fs.Do("/dev/zero", os.O_RDONLY, func(f *os.File) error {
return e
})
if err != e {
t.Fatalf("Unexpected error with broken function: %v", err)
}
}