-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbadger_test.go
84 lines (78 loc) · 1.66 KB
/
badger_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
package meter_test
import (
"context"
"fmt"
"os"
"path"
"testing"
"time"
meter "github.com/alxarch/go-meter/v2"
badger "github.com/dgraph-io/badger/v2"
)
func TestBadgerEvents(t *testing.T) {
d := path.Join(os.TempDir(), fmt.Sprintf("meter-test-%d", time.Now().UnixNano()))
if err := os.MkdirAll(d, os.ModeDir|os.ModePerm); err != nil {
t.Fatal(err)
}
opts := badger.DefaultOptions
opts.Dir = d
opts.ValueDir = d
db, err := badger.Open(opts)
if err != nil {
t.Fatal("Failed to open badger", err)
}
events, err := meter.Open(db, "test")
if err != nil {
t.Fatal("Failed to open badger store", err)
}
tm := time.Date(2019, time.May, 15, 13, 14, 0, 0, time.UTC)
req := meter.StoreRequest{
Event: "test",
Time: tm,
Labels: []string{
"country",
"host",
"method",
},
Counters: meter.Snapshot{
{
Values: []string{"USA", "www.example.org", "GET"},
Count: 12,
},
{
Values: []string{"USA", "www.example.org", "POST"},
Count: 10,
},
{
Values: []string{"GRC", "www.example.org", "GET"},
Count: 4,
},
{
Values: []string{"USA", "example.org", "GET"},
Count: 8,
},
},
}
if err := events.Store(&req); err != nil {
t.Fatal("Failed to store counters", err)
}
qr := meter.ScanQueryRunner(events)
ctx := context.Background()
q := meter.Query{
TimeRange: meter.TimeRange{
Step: time.Second,
Start: tm.Add(-1 * time.Hour),
End: tm.Add(24 * time.Hour),
},
Match: meter.Fields{
{Label: "country", Value: "USA"},
},
}
results, err := qr.RunQuery(ctx, &q, "test")
if err != nil {
t.Fatal("Query failed", err)
}
if len(results) != 3 {
t.Fatal("numResults", len(results))
}
}