mirror of
https://github.com/restic/restic.git
synced 2025-12-12 03:22:07 +00:00
ui/progress: Simplify Updater
Removed a defer'd call that was a bit subtle.
This commit is contained in:
@@ -57,17 +57,13 @@ func (c *Updater) Done() {
|
|||||||
|
|
||||||
func (c *Updater) run() {
|
func (c *Updater) run() {
|
||||||
defer close(c.stopped)
|
defer close(c.stopped)
|
||||||
defer func() {
|
|
||||||
// Must be a func so that time.Since isn't called at defer time.
|
|
||||||
c.report(time.Since(c.start), true)
|
|
||||||
}()
|
|
||||||
|
|
||||||
var tick <-chan time.Time
|
var tick <-chan time.Time
|
||||||
if c.tick != nil {
|
if c.tick != nil {
|
||||||
tick = c.tick.C
|
tick = c.tick.C
|
||||||
}
|
}
|
||||||
signalsCh := signals.GetProgressChannel()
|
signalsCh := signals.GetProgressChannel()
|
||||||
for {
|
for final := false; !final; {
|
||||||
var now time.Time
|
var now time.Time
|
||||||
|
|
||||||
select {
|
select {
|
||||||
@@ -76,9 +72,9 @@ func (c *Updater) run() {
|
|||||||
debug.Log("Signal received: %v\n", sig)
|
debug.Log("Signal received: %v\n", sig)
|
||||||
now = time.Now()
|
now = time.Now()
|
||||||
case <-c.stop:
|
case <-c.stop:
|
||||||
return
|
final, now = true, time.Now()
|
||||||
}
|
}
|
||||||
|
|
||||||
c.report(now.Sub(c.start), false)
|
c.report(now.Sub(c.start), final)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,13 +9,17 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestUpdater(t *testing.T) {
|
func TestUpdater(t *testing.T) {
|
||||||
finalSeen := false
|
var (
|
||||||
var ncalls int
|
finalSeen = false
|
||||||
|
ncalls = 0
|
||||||
|
dur time.Duration
|
||||||
|
)
|
||||||
|
|
||||||
report := func(d time.Duration, final bool) {
|
report := func(d time.Duration, final bool) {
|
||||||
if final {
|
if final {
|
||||||
finalSeen = true
|
finalSeen = true
|
||||||
}
|
}
|
||||||
|
dur = d
|
||||||
ncalls++
|
ncalls++
|
||||||
}
|
}
|
||||||
c := progress.NewUpdater(10*time.Millisecond, report)
|
c := progress.NewUpdater(10*time.Millisecond, report)
|
||||||
@@ -24,6 +28,7 @@ func TestUpdater(t *testing.T) {
|
|||||||
|
|
||||||
test.Assert(t, finalSeen, "final call did not happen")
|
test.Assert(t, finalSeen, "final call did not happen")
|
||||||
test.Assert(t, ncalls > 0, "no progress was reported")
|
test.Assert(t, ncalls > 0, "no progress was reported")
|
||||||
|
test.Assert(t, dur > 0, "duration must be positive")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUpdaterStopTwice(_ *testing.T) {
|
func TestUpdaterStopTwice(_ *testing.T) {
|
||||||
|
|||||||
Reference in New Issue
Block a user