forked from FeatureBaseDB/featurebase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil_test.go
92 lines (83 loc) · 2.28 KB
/
util_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
// Copyright 2022 Molecula Corp. (DBA FeatureBase).
// SPDX-License-Identifier: Apache-2.0
package pilosa
// util_test.go has unit tests for utility functions from util.go
import (
"testing"
"time"
)
func TestGetLoopProgress(t *testing.T) {
// TODO: try to find more sneaky cases
cases := []struct {
name string
start time.Time
now time.Time
i uint
total uint
}{
{
"one minute, half done",
time.Date(1969, time.June, 9, 4, 20, 0, 0, time.UTC),
time.Date(1969, time.June, 9, 4, 21, 0, 0, time.UTC),
10,
20,
},
{
"four seconds, half done",
time.Date(1969, time.June, 9, 4, 20, 0, 0, time.UTC),
time.Date(1969, time.June, 9, 4, 20, 4, 0, time.UTC),
10,
20,
},
{
"one minute, one second, 5 μs, half done",
time.Date(1969, time.June, 9, 4, 20, 0, 0, time.UTC),
time.Date(1969, time.June, 9, 4, 21, 1, 5, time.UTC),
10,
20,
},
{
"one minute, one done",
time.Date(1969, time.June, 9, 4, 20, 0, 0, time.UTC),
time.Date(1969, time.June, 9, 4, 21, 0, 0, time.UTC),
1,
20,
},
{
"one minute, 1/5 done",
time.Date(1969, time.June, 9, 4, 20, 0, 0, time.UTC),
time.Date(1969, time.June, 9, 4, 21, 0, 0, time.UTC),
10,
50,
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
// we expect that it will be the avg time per message times
// the number of remaining messages
expected := time.Duration((float64(c.now.Sub(c.start)) / float64(c.i+1)) * float64(c.total-(c.i+1)))
expectedPct := 100 * (float64(c.i+1) / float64(c.total))
timeLeft, pctDone := GetLoopProgress(c.start, c.now, c.i, c.total)
if timeLeft != expected {
t.Errorf("Time left was incorrect, expected: %d, but got: %d", expected, timeLeft)
}
if pctDone != expectedPct {
t.Errorf("Percentage done was incorrect, expected: %f, but got: %f", expectedPct, pctDone)
}
})
}
}
func TestGetMemoryUsage(t *testing.T) {
if _, err := GetMemoryUsage(); err != nil {
t.Fatalf("unexpected error getting memory usage: %v", err)
}
}
func TestGetDiskUsage(t *testing.T) {
tdir := t.TempDir()
if _, err := GetDiskUsage(tdir); err != nil {
t.Fatalf("unexpected error getting disk usage: %v", err)
}
if _, err := GetDiskUsage(""); err == nil {
t.Fatal("expected error getting disk usage but got nil")
}
}