2020-11-04 13:11:29 +00:00
|
|
|
package progress_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/restic/restic/internal/test"
|
|
|
|
"github.com/restic/restic/internal/ui/progress"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCounter(t *testing.T) {
|
|
|
|
const N = 100
|
2020-12-05 22:57:06 +00:00
|
|
|
const startTotal = uint64(12345)
|
2020-11-04 13:11:29 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
finalSeen = false
|
|
|
|
increasing = true
|
|
|
|
last uint64
|
2020-12-05 22:57:06 +00:00
|
|
|
lastTotal = startTotal
|
2020-11-04 13:11:29 +00:00
|
|
|
ncalls int
|
2020-12-05 22:57:06 +00:00
|
|
|
nmaxChange int
|
2020-11-04 13:11:29 +00:00
|
|
|
)
|
|
|
|
|
2020-12-05 22:57:06 +00:00
|
|
|
report := func(value uint64, total uint64, d time.Duration, final bool) {
|
2020-12-05 23:18:50 +00:00
|
|
|
if final {
|
|
|
|
finalSeen = true
|
|
|
|
}
|
2020-11-04 13:11:29 +00:00
|
|
|
if value < last {
|
|
|
|
increasing = false
|
|
|
|
}
|
|
|
|
last = value
|
2020-12-05 22:57:06 +00:00
|
|
|
if total != lastTotal {
|
|
|
|
nmaxChange++
|
|
|
|
}
|
|
|
|
lastTotal = total
|
2020-11-04 13:11:29 +00:00
|
|
|
ncalls++
|
|
|
|
}
|
2020-12-05 22:57:06 +00:00
|
|
|
c := progress.New(10*time.Millisecond, startTotal, report)
|
2020-11-04 13:11:29 +00:00
|
|
|
|
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
defer close(done)
|
|
|
|
for i := 0; i < N; i++ {
|
|
|
|
time.Sleep(time.Millisecond)
|
|
|
|
c.Add(1)
|
|
|
|
}
|
2020-12-05 22:57:06 +00:00
|
|
|
c.SetMax(42)
|
2020-11-04 13:11:29 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
<-done
|
|
|
|
c.Done()
|
|
|
|
|
|
|
|
test.Assert(t, finalSeen, "final call did not happen")
|
|
|
|
test.Assert(t, increasing, "values not increasing")
|
|
|
|
test.Equals(t, uint64(N), last)
|
2020-12-05 22:57:06 +00:00
|
|
|
test.Equals(t, uint64(42), lastTotal)
|
|
|
|
test.Equals(t, int(1), nmaxChange)
|
2020-11-04 13:11:29 +00:00
|
|
|
|
|
|
|
t.Log("number of calls:", ncalls)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCounterNil(t *testing.T) {
|
|
|
|
// Shouldn't panic.
|
2022-03-28 20:33:17 +00:00
|
|
|
var c *progress.Counter
|
2020-11-04 13:11:29 +00:00
|
|
|
c.Add(1)
|
|
|
|
c.Done()
|
|
|
|
}
|
2020-12-28 22:11:42 +00:00
|
|
|
|
|
|
|
func TestCounterNoTick(t *testing.T) {
|
|
|
|
finalSeen := false
|
|
|
|
otherSeen := false
|
|
|
|
|
2020-12-05 22:57:06 +00:00
|
|
|
report := func(value, total uint64, d time.Duration, final bool) {
|
2020-12-28 22:11:42 +00:00
|
|
|
if final {
|
|
|
|
finalSeen = true
|
|
|
|
} else {
|
|
|
|
otherSeen = true
|
|
|
|
}
|
|
|
|
}
|
2020-12-05 22:57:06 +00:00
|
|
|
c := progress.New(0, 1, report)
|
2020-12-28 22:11:42 +00:00
|
|
|
time.Sleep(time.Millisecond)
|
|
|
|
c.Done()
|
|
|
|
|
|
|
|
test.Assert(t, finalSeen, "final call did not happen")
|
|
|
|
test.Assert(t, !otherSeen, "unexpected status update")
|
|
|
|
}
|