You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
793 B
45 lines
793 B
package progress |
|
|
|
import ( |
|
"context" |
|
"fmt" |
|
"testing" |
|
"time" |
|
) |
|
|
|
func TestProgressManager(t *testing.T) { |
|
c := make(chan struct{}) |
|
|
|
m := NewProgressManager() |
|
taskId := m.StartTask(context.Background(), "testing", func(ctx context.Context, progress IProgressReporter) error { |
|
defer close(c) |
|
|
|
progress.SetTotal(100) |
|
for range 100 { |
|
progress.AddProgress(1) |
|
time.Sleep(110 * time.Millisecond) |
|
} |
|
return nil |
|
}) |
|
|
|
go func() { |
|
task, _ := m.GetTask(taskId) |
|
for { |
|
select { |
|
case <-c: |
|
return |
|
default: |
|
} |
|
progress, total := task.Progress.Get() |
|
if total != 0 { |
|
fmt.Printf("task %s progress: %.2f%%\n", task.Name, float64(progress)/float64(total)*100) |
|
if progress == total { |
|
return |
|
} |
|
} |
|
time.Sleep(300 * time.Millisecond) |
|
} |
|
}() |
|
|
|
<-c |
|
}
|
|
|