forked from deepkaran/goforestdb
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlog_test.go
112 lines (106 loc) · 2.53 KB
/
log_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
108
109
110
111
112
package forestdb
import (
"os"
"testing"
)
// TestLogCallback intentionally triggers behavior which should invoke the
// logging callback, and verifies that the callback fires as expected.
func TestLogCallback(t *testing.T) {
defer os.RemoveAll("test")
// create test file with one k/v pair
dbfile, err := Open("test", nil)
if err != nil {
t.Fatal(err)
}
kvstore, err := dbfile.OpenKVStoreDefault(nil)
if err != nil {
t.Fatal(err)
}
err = kvstore.SetKV([]byte("key"), []byte("value"))
if err != nil {
t.Fatal(err)
}
err = kvstore.Close()
if err != nil {
t.Fatal(err)
}
err = dbfile.Commit(COMMIT_NORMAL)
if err != nil {
t.Fatal(err)
}
err = dbfile.Close()
if err != nil {
t.Fatal(err)
}
// now open it again, this time read only
dbconfig := DefaultConfig()
dbconfig.SetOpenFlags(OPEN_FLAG_RDONLY)
dbfile, err = Open("test", dbconfig)
if err != nil {
t.Fatal(err)
}
kvstore, err = dbfile.OpenKVStoreDefault(nil)
if err != nil {
t.Fatal(err)
}
// setup logging
callbackFired := false
kvstore.SetLogCallback(func(name string, errCode int, msg string, ctx interface{}) {
callbackFired = true
if name != "default" {
t.Errorf("expected kvstore name to be 'default', got %s", name)
}
if errCode != -10 {
t.Errorf("expected error code -10, got %d", errCode)
}
if ctx, ok := ctx.(map[string]interface{}); ok {
if ctx["customKey"] != "customVal" {
t.Errorf("expected to see my custom context")
}
} else {
t.Errorf("expected custom context to be the type i passed in")
}
// don't check the message as it could change
}, map[string]interface{}{"customKey": "customVal"})
err = kvstore.SetKV([]byte("key"), []byte("value"))
if err == nil {
t.Fatalf("expected error, got nil")
}
if !callbackFired {
t.Errorf("expected log callback to fire, it didn't")
}
err = kvstore.Close()
if err != nil {
t.Fatal(err)
}
err = dbfile.Close()
if err != nil {
t.Fatal(err)
}
}
// if you want to assure yourself that the fatal error
// callback fires:
//
// 1. add these to the cgo defintions at the top of forestdb.go
//
//extern uint64_t _kvs_stat_get_sum_attr(void *data, uint64_t version, int attr);
//void assertFail() {
// char *data = malloc(1024);
// data[0]=1;
// _kvs_stat_get_sum_attr(data, 2, 25);
//}
//
// 2. add this to forestdb.go
//
// func fdbAssertFail() {
// C.assertFail()
// }
//
// 3. uncomment and run the test case below:
//
// func TestFatalErrorCallback(t *testing.T) {
// SetFatalErrorCallback(func() {
// log.Printf("got fatal error")
// })
// fdbAssertFail()
// }