-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmonitor_test.go
41 lines (38 loc) · 944 Bytes
/
monitor_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
package microcache
import (
"net/http"
"testing"
"time"
)
// Remove should work as expected
func TestMonitor(t *testing.T) {
var hits int
var expected = 4
testMonitor := MonitorFunc(100*time.Second, func(s Stats) {
hits = s.Hits
})
testMonitor.hits = int64(expected)
testMonitor.Log(Stats{})
if hits != expected {
t.Fatalf("Monitor not logging correctly (%d != %d)", hits, expected)
}
}
// Microcache calls monitor
func TestMicrocacheCallsMonitor(t *testing.T) {
var statChan = make(chan int)
testMonitor := &monitorFunc{interval: 10 * time.Millisecond, logFunc: func(s Stats) {
statChan <- s.Size
}}
cache := New(Config{
TTL: 30 * time.Second,
Monitor: testMonitor,
Driver: NewDriverLRU(10),
})
defer cache.Stop()
handler := cache.Middleware(http.HandlerFunc(noopSuccessHandler))
batchGet(handler, []string{"/"})
size := <-statChan
if size != 1 {
t.Fatal("Monitor was not called by microcache")
}
}