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++
|
|
|
|
}
|
2022-12-29 11:29:46 +00:00
|
|
|
c := progress.NewCounter(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)
|
2022-12-29 11:29:46 +00:00
|
|
|
c.SetMax(42)
|
2020-11-04 13:11:29 +00:00
|
|
|
c.Done()
|
|
|
|
}
|