-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage_test.go
56 lines (47 loc) · 1.13 KB
/
storage_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
package metrica
import (
"os"
"sync"
"testing"
"time"
)
func TestWriteRead(t *testing.T) {
c := Counter(time.Now())
f, err := os.CreateTemp("", "test")
if err != nil {
t.Fatalf("error creating temp file: %v", err)
}
defer func() {
_ = os.Remove(f.Name())
}()
fs := NewFileStorage(&sync.Mutex{}, f.Name())
if err := fs.Write(c); err != nil {
t.Fatalf("error writing to file: %v", err)
}
got, err := fs.Read()
if err != nil {
t.Fatalf("error reading file: %v", err)
}
if len(got) == 1 {
assert(t, c.Format(), got[0].Format())
} else {
t.Fatalf("expected 1; got %v", len(got))
}
}
func TestCountersCount60sec(t *testing.T) {
currentTime := time.Now()
counters := Counters{
Counter(currentTime.Add(-time.Second * 10)),
Counter(currentTime.Add(-time.Second * 10)),
Counter(currentTime.Add(-time.Second * 20)),
Counter(currentTime.Add(-time.Second * 30)),
Counter(currentTime.Add(-time.Second * 40)),
Counter(currentTime.Add(-time.Second * 50)),
Counter(currentTime.Add(-time.Second * 60)),
}
want := int64(6)
got := counters.Count60sec()
if got != want {
t.Fatalf("expected %v; got %v", want, got)
}
}