Files
cmsp/app_close_test.go
T

46 lines
1.2 KiB
Go

package main
import (
"context"
"testing"
"time"
"cmsp/internal/task"
)
func Test退出前先停止取视频任务(t *testing.T) {
started := make(chan struct{})
runner := task.NewRunner(task.Options{
Load: func(_ context.Context, id string) (task.Product, error) {
return task.Product{ID: id}, nil
},
Prepare: func(ctx context.Context, _ task.Product) (task.Work, error) {
close(started)
<-ctx.Done()
return task.Work{}, ctx.Err()
},
})
if err := runner.Start(context.Background(), []string{"fictional-product"}); err != nil {
t.Fatal(err)
}
select {
case <-started:
case <-time.After(2 * time.Second):
t.Fatal("取视频任务未启动")
}
a := NewApp()
a.videoTask = runner
// 此测试没有 Wails 前端;只检查退出门,不打开系统消息框。
a.exitNoticeOnce.Do(func() {})
if !a.beforeClose(context.Background()) {
t.Fatal("任务仍在运行时必须阻止本次退出")
}
deadline := time.Now().Add(2 * time.Second)
for runner.Snapshot().State == task.StateRunning && time.Now().Before(deadline) {
time.Sleep(10 * time.Millisecond)
}
if got := runner.Snapshot().State; got != task.StateStopped {
t.Fatalf("退出前未停止任务:%s", got)
}
}