fix more race conditions

This commit is contained in:
Adrien Delorme 2018-10-15 16:31:59 +02:00
parent 27f4b9e4a1
commit 25775cd266
2 changed files with 7 additions and 5 deletions

View File

@ -93,17 +93,17 @@ func (spb *StackableProgressBar) Finish() {
spb.mtx.Lock() spb.mtx.Lock()
defer spb.mtx.Unlock() defer spb.mtx.Unlock()
if spb.items < 0 {
spb.items-- spb.items--
if spb.items == 0 { }
if spb.items == 0 && spb.Bar.ProgressBar != nil {
// slef cleanup // slef cleanup
spb.Bar.ProgressBar.Finish() spb.Bar.ProgressBar.Finish()
spb.Bar.ProgressBar = nil
spb.started = false spb.started = false
spb.total = 0 spb.total = 0
return return
} }
if spb.Bar.ProgressBar != nil {
spb.prefix()
}
} }
// BasicProgressBar is packer's basic progress bar. // BasicProgressBar is packer's basic progress bar.

View File

@ -12,6 +12,7 @@ func speedyProgressBar(bar *pb.ProgressBar) {
bar.SetUnits(pb.U_BYTES) bar.SetUnits(pb.U_BYTES)
bar.SetRefreshRate(1 * time.Millisecond) bar.SetRefreshRate(1 * time.Millisecond)
bar.NotPrint = true bar.NotPrint = true
bar.Format("[\x00=\x00>\x00-\x00]")
} }
func TestStackableProgressBar_race(t *testing.T) { func TestStackableProgressBar_race(t *testing.T) {
@ -36,6 +37,7 @@ func TestStackableProgressBar_race(t *testing.T) {
}{ }{
{"all public", fields{nil, []func(){start42Fn, finishFn, add21, add21}, finishFn}, 300}, {"all public", fields{nil, []func(){start42Fn, finishFn, add21, add21}, finishFn}, 300},
{"add", fields{start42Fn, []func(){add21}, finishFn}, 300}, {"add", fields{start42Fn, []func(){add21}, finishFn}, 300},
{"add start", fields{start42Fn, []func(){start42Fn, add21}, finishFn}, 300},
} }
for _, tt := range tests { for _, tt := range tests {