diff --git a/ui/task/reporter.go b/ui/task/reporter.go index e4f53a713..8aa283e36 100644 --- a/ui/task/reporter.go +++ b/ui/task/reporter.go @@ -17,7 +17,7 @@ type ReporterImpl struct { eventMarkers []eventMarker lastGlobalEvent *Event - outputRest string + outputRest map[int]string sync.Mutex } @@ -33,7 +33,13 @@ const ( ) func NewReporter(ui boshui.UI, isForEvents bool) *ReporterImpl { - return &ReporterImpl{ui: ui, isForEvents: isForEvents, events: map[int][]*Event{}, eventMarkers: []eventMarker{}} + return &ReporterImpl{ + ui: ui, + isForEvents: isForEvents, + events: map[int][]*Event{}, + eventMarkers: []eventMarker{}, + outputRest: map[int]string{}, + } } func (r *ReporterImpl) TaskStarted(id int) { @@ -83,17 +89,17 @@ func (r *ReporterImpl) TaskOutputChunk(id int, chunk []byte) { } if r.isForEvents { - r.outputRest += string(chunk) + r.outputRest[id] += string(chunk) for { - idx := strings.Index(r.outputRest, "\n") + idx := strings.Index(r.outputRest[id], "\n") if idx == -1 { break } - if len(r.outputRest[0:idx]) > 0 { - r.showEvent(id, r.outputRest[0:idx]) + if len(r.outputRest[id][0:idx]) > 0 { + r.showEvent(id, r.outputRest[id][0:idx]) } - r.outputRest = r.outputRest[idx+1:] + r.outputRest[id] = r.outputRest[id][idx+1:] } } else { r.showChunk(chunk) diff --git a/ui/task/reporter_test.go b/ui/task/reporter_test.go index e74b81cdf..2cf96bf34 100644 --- a/ui/task/reporter_test.go +++ b/ui/task/reporter_test.go @@ -287,6 +287,39 @@ Task 2663 Started Tue Nov 8 00:26:38 UTC 2016 Task 2663 Finished Tue Nov 8 00:26:38 UTC 2016 Task 2663 Duration 00:00:00 Task 2663 error +`)) + }) + + It("is able to deal with partial event chunks coming in parallel", func() { + chunk1 := ` +{"time":1478564798,"stage":"chunk1"` + chunk2 := `{"time":1478564798,"stage":"chunk2","tags":[],"total":1,"task":"Finding packages to compile","index":1,"state":"started","progress":0} +` + chunk3 := `,"tags":[],"total":1,"task":"Preparing deployment","index":1,"state":"started","progress":0} +` + + reporter.TaskStarted(100) + reporter.TaskStarted(101) + reporter.TaskOutputChunk(100, []byte(chunk1)) + reporter.TaskOutputChunk(101, []byte(chunk2)) + reporter.TaskOutputChunk(100, []byte(chunk3)) + reporter.TaskFinished(100, "done") + reporter.TaskFinished(101, "done") + Expect(outBuf.String()).To(Equal(`Task 100 +Task 101 +Task 101 | 00:26:38 | chunk2: Finding packages to compile +Task 100 | 00:26:38 | chunk1: Preparing deployment + +Task 100 Started Tue Nov 8 00:26:38 UTC 2016 +Task 100 Finished Tue Nov 8 00:26:38 UTC 2016 +Task 100 Duration 00:00:00 +Task 100 done + + +Task 101 Started Tue Nov 8 00:26:38 UTC 2016 +Task 101 Finished Tue Nov 8 00:26:38 UTC 2016 +Task 101 Duration 00:00:00 +Task 101 done `)) }) })