-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsked_test.go
103 lines (94 loc) · 1.95 KB
/
sked_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
package sked
import (
"bytes"
"io/ioutil"
"os"
"reflect"
"testing"
"time"
)
func TestNew(t *testing.T) {
l := &DiscardLogger{}
tmpfile, _ := ioutil.TempFile("", "sked")
defer os.Remove(tmpfile.Name())
o := &Options{DBPath: tmpfile.Name()}
s, err := New(o, l)
defer s.Close()
if err != nil {
t.Error(err)
return
}
want := &Options{
Bucket: DefaultBucket,
CheckIntervalMS: DefaultCheckIntervalMS,
OpenTimeoutMS: DefaultOpenTimeoutMS,
DBPath: tmpfile.Name(),
EventChannelSize: DefaultEventChannelSize,
}
if !reflect.DeepEqual(want, s.options) {
t.Errorf("want options %+v, got %+v", want, s.options)
}
}
func TestAddDeleteGet(t *testing.T) {
l := NewLogLogger()
tmpfile, _ := ioutil.TempFile("", "sked")
defer os.Remove(tmpfile.Name())
o := &Options{DBPath: tmpfile.Name()}
s, err := New(o, l)
if err != nil {
t.Fatal(err)
}
defer s.Close()
now := time.Now().UTC()
event := []byte("{some event data}")
id, err := s.Add(now, event, "tagme", 2)
if err != nil {
t.Error(err)
}
if id == 0 {
t.Errorf("want some id > 0, got %d", id)
return
}
r, err := s.Get(id)
if err != nil {
t.Error(err)
}
if !bytes.Equal(r.Event, event) {
t.Errorf("want %s, got %s", event, r.Event)
}
err = s.Delete(id, "")
if err != nil {
t.Error(err)
}
r, err = s.Get(id)
if err != nil {
t.Error(err)
}
if r != nil {
}
}
func TestGetAll(t *testing.T) {
l := &DiscardLogger{}
tmpfile, _ := ioutil.TempFile("", "sked")
defer os.Remove(tmpfile.Name())
o := &Options{DBPath: tmpfile.Name()}
s, err := New(o, l)
defer s.Close()
if err != nil {
t.Error(err)
return
}
now := time.Now().UTC()
event := []byte("{some event data}")
s.Add(now, event, "tagme", 2)
s.Add(now, event, "tagme", 1)
s.Add(now, event, "tagme", 3)
s.Add(now, event, "someothertagme", 3)
results, err := s.GetAll("tagme")
if err != nil {
t.Error(err)
}
if len(results) != 3 {
t.Errorf("want 3 results, got %d", len(results))
}
}