From fde2329eaa1fab2327ae2e775af5aa04e2726ed5 Mon Sep 17 00:00:00 2001 From: Aaron Lehmann Date: Thu, 10 Dec 2015 16:48:09 -0800 Subject: [PATCH] Avoid outputting last progress item twice A watcher would output the current progress item when it was detached, in case it missed that item earlier, which would leave the user seeing some intermediate step of the operation. This commit changes it to only output it on detach if it didn't already output the same item. Signed-off-by: Aaron Lehmann --- distribution/xfer/transfer.go | 16 ++++++++++------ distribution/xfer/transfer_test.go | 9 --------- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/distribution/xfer/transfer.go b/distribution/xfer/transfer.go index 6852225488680f9582fdb7004dd432d37041a480..dd83f8b8db847dd7135965677cc40c472dd9c650 100644 --- a/distribution/xfer/transfer.go +++ b/distribution/xfer/transfer.go @@ -124,7 +124,6 @@ func (t *transfer) Broadcast(masterProgressChan <-chan progress.Progress) { default: } } - } else { t.broadcastDone = true } @@ -159,18 +158,23 @@ func (t *transfer) Watch(progressOutput progress.Output) *Watcher { defer func() { close(w.running) }() - done := false + var ( + done bool + lastWritten progress.Progress + hasLastWritten bool + ) for { t.mu.Lock() hasLastProgress := t.hasLastProgress lastProgress := t.lastProgress t.mu.Unlock() - // This might write the last progress item a - // second time (since channel closure also gets - // us here), but that's fine. - if hasLastProgress { + // Make sure we don't write the last progress item + // twice. + if hasLastProgress && (!done || !hasLastWritten || lastProgress != lastWritten) { progressOutput.WriteProgress(lastProgress) + lastWritten = lastProgress + hasLastWritten = true } if done { diff --git a/distribution/xfer/transfer_test.go b/distribution/xfer/transfer_test.go index 8fe24661bcd06091fa8c32775d64c15fc398e9aa..c9c9a20a6dc24e983cd499f954f8ad12bb90093e 100644 --- a/distribution/xfer/transfer_test.go +++ b/distribution/xfer/transfer_test.go @@ -41,15 +41,6 @@ func TestTransfer(t *testing.T) { if p.Current != 0 { t.Fatalf("got unexpected progress value: %d (expected 0)", p.Current) } - } else if p.Current == 10 { - // Special case: last progress output may be - // repeated because the transfer finishing - // causes the latest progress output to be - // written to the channel (in case the watcher - // missed it). - if p.Current != 9 && p.Current != 10 { - t.Fatalf("got unexpected progress value: %d (expected %d)", p.Current, val+1) - } } else if p.Current != val+1 { t.Fatalf("got unexpected progress value: %d (expected %d)", p.Current, val+1) }