featurebase/util_test.go
Samir Patel 33a916c0f4 Revert "increase time range for timestamp by using specified granularity"
This PR reverts the timestamp work.

The timestamp work requires changes to FB and IDK; and there are
circular dependencies between tests in either repo preventing merging
of either. The work here is pretty stable, but required bypassing
the smoketest. Meanwhile, I found some additional things in IDK that
need addressing which means I merged this work in pre-maturely. Once
I get that worked out, I'll re-commit these commits.

This reverts following commits related to timestamp work:

bypass of smoke test b/c of circular dep with IDK: 0676790
update codec to reflect changes to timstamp range: bd5dc76
fix few bugs regarding timestamp: 33fce8a
increase time range for timestamp by using specified granularity: 5939923.
2022-07-18 11:09:04 -05:00

109 lines
2.9 KiB
Go

// Copyright 2021 Molecula Corp. All rights reserved.
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 TestFormatTimestampNano(t *testing.T) {
if FormatTimestampNano(0, 69, "s") != "1970-01-01T00:01:09Z" {
t.Fatal("Timestamp not formatted properly")
}
if FormatTimestampNano(0, 420, "ms") != "1970-01-01T00:00:00.42Z" {
t.Fatal("Timestamp not formatted properly")
}
if FormatTimestampNano(420, 0, "μs") != "1970-01-01T00:00:00.00000042Z" {
t.Fatal("Timestamp not formatted properly")
}
if FormatTimestampNano(420, 69, "us") != "1970-01-01T00:00:00.000489Z" {
t.Fatal("Timestamp not formatted properly")
}
if FormatTimestampNano(69, 420, "ns") != "1970-01-01T00:00:00.000000489Z" {
t.Fatal("Timestamp not formatted properly")
}
}
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")
}
}