Browse code

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 <aaron.lehmann@docker.com>

Aaron Lehmann authored on 2015/12/11 09:48:09
Showing 2 changed files
... ...
@@ -124,7 +124,6 @@ func (t *transfer) Broadcast(masterProgressChan <-chan progress.Progress) {
124 124
 				default:
125 125
 				}
126 126
 			}
127
-
128 127
 		} else {
129 128
 			t.broadcastDone = true
130 129
 		}
... ...
@@ -159,18 +158,23 @@ func (t *transfer) Watch(progressOutput progress.Output) *Watcher {
159 159
 		defer func() {
160 160
 			close(w.running)
161 161
 		}()
162
-		done := false
162
+		var (
163
+			done           bool
164
+			lastWritten    progress.Progress
165
+			hasLastWritten bool
166
+		)
163 167
 		for {
164 168
 			t.mu.Lock()
165 169
 			hasLastProgress := t.hasLastProgress
166 170
 			lastProgress := t.lastProgress
167 171
 			t.mu.Unlock()
168 172
 
169
-			// This might write the last progress item a
170
-			// second time (since channel closure also gets
171
-			// us here), but that's fine.
172
-			if hasLastProgress {
173
+			// Make sure we don't write the last progress item
174
+			// twice.
175
+			if hasLastProgress && (!done || !hasLastWritten || lastProgress != lastWritten) {
173 176
 				progressOutput.WriteProgress(lastProgress)
177
+				lastWritten = lastProgress
178
+				hasLastWritten = true
174 179
 			}
175 180
 
176 181
 			if done {
... ...
@@ -41,15 +41,6 @@ func TestTransfer(t *testing.T) {
41 41
 				if p.Current != 0 {
42 42
 					t.Fatalf("got unexpected progress value: %d (expected 0)", p.Current)
43 43
 				}
44
-			} else if p.Current == 10 {
45
-				// Special case: last progress output may be
46
-				// repeated because the transfer finishing
47
-				// causes the latest progress output to be
48
-				// written to the channel (in case the watcher
49
-				// missed it).
50
-				if p.Current != 9 && p.Current != 10 {
51
-					t.Fatalf("got unexpected progress value: %d (expected %d)", p.Current, val+1)
52
-				}
53 44
 			} else if p.Current != val+1 {
54 45
 				t.Fatalf("got unexpected progress value: %d (expected %d)", p.Current, val+1)
55 46
 			}