66 lines
1.8 KiB
Go
66 lines
1.8 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/Nwflower/reims-go/internal/config"
|
|
"github.com/Nwflower/reims-go/internal/i18n"
|
|
"github.com/Nwflower/reims-go/internal/macos/reims"
|
|
"github.com/Nwflower/reims-go/internal/packetbridge"
|
|
)
|
|
|
|
func TestPacketBridgeRestoreCleanupCancelsAndWaits(t *testing.T) {
|
|
a := &App{}
|
|
started, cancelled, finish := make(chan struct{}), make(chan struct{}), make(chan struct{})
|
|
stop := a.startPacketBridgeRestore(context.Background(), func(ctx context.Context) {
|
|
close(started)
|
|
<-ctx.Done()
|
|
close(cancelled)
|
|
<-finish
|
|
})
|
|
<-started
|
|
stopped := make(chan struct{})
|
|
go func() { stop(); close(stopped) }()
|
|
select {
|
|
case <-cancelled:
|
|
case <-time.After(time.Second):
|
|
close(finish)
|
|
t.Fatal("停用没有取消恢复任务")
|
|
}
|
|
select {
|
|
case <-stopped:
|
|
t.Error("恢复任务退出之前已经完成停用")
|
|
default:
|
|
}
|
|
close(finish)
|
|
select {
|
|
case <-stopped:
|
|
case <-time.After(time.Second):
|
|
t.Fatal("恢复任务退出后清理没有结束")
|
|
}
|
|
}
|
|
|
|
func TestStoppedPacketBridgesRejectLateOpenAndActivationReopensGate(t *testing.T) {
|
|
root := t.TempDir()
|
|
installDummyRuntime(t, root)
|
|
a := &App{cfg: &config.Config{DataRoot: root, ActiveRuntime: "test-rt"}}
|
|
a.closePacketBridges()
|
|
_, err := a.ensurePacketBridge(reims.VMDetail{Name: "test"}, packetbridge.Adapter{}, "", "", 0)
|
|
var coded *i18n.CodedError
|
|
if !errors.As(err, &coded) || coded.Code != "E_NETWORK_PLUGIN_DISABLED" {
|
|
t.Fatalf("停用后允许晚到的启动任务创建通道: %v", err)
|
|
}
|
|
stop := a.startPacketBridgeRestore(context.Background(), func(context.Context) {})
|
|
stop()
|
|
a.packetMu.Lock()
|
|
stopped := a.packetBridgesStopped
|
|
a.packetMu.Unlock()
|
|
if stopped {
|
|
t.Fatal("重新激活插件没有开放桥接入口")
|
|
}
|
|
a.closePacketBridges()
|
|
}
|