Skip to content

Commit

Permalink
fix race condition for outputting event chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
cppforlife committed Oct 13, 2017
1 parent be8570e commit f58b3a1
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
20 changes: 13 additions & 7 deletions ui/task/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type ReporterImpl struct {
eventMarkers []eventMarker
lastGlobalEvent *Event

outputRest string
outputRest map[int]string
sync.Mutex
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down
33 changes: 33 additions & 0 deletions ui/task/reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
`))
})
})
Expand Down

0 comments on commit f58b3a1

Please sign in to comment.