feat: toggle managed instance start and stop
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"chub/internal/domain"
|
||||
"chub/internal/platform/browser"
|
||||
@@ -54,6 +55,37 @@ func TestInstanceStarterResolvesSelectedProxyAtLaunch(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestInstanceStopperGracefullyStopsOnlyVerifiedManagedProfile(t *testing.T) {
|
||||
managed := &fakeManagedStopper{use: browser.ProfileUse{Occupied: true, PID: 4242, Source: "chub_registry"}, releaseOnStop: true}
|
||||
stopper := instanceStopper{launcher: managed, managedProfiles: managed, waitTimeout: time.Second, waitInterval: time.Millisecond}
|
||||
err := stopper.Stop(context.Background(), ui.InstanceRow{ID: "managed", Status: "运行中", PID: 4242, OccupancySource: "chub_registry", UserDataDir: `C:\profiles\managed`})
|
||||
if err != nil || managed.stopCalls != 1 || managed.force || managed.userDataDir != `C:\profiles\managed` {
|
||||
t.Fatalf("Stop() = %v, calls=%d force=%v dir=%q", err, managed.stopCalls, managed.force, managed.userDataDir)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInstanceStopperRejectsMismatchedOrExternalProfiles(t *testing.T) {
|
||||
managed := &fakeManagedStopper{use: browser.ProfileUse{Occupied: true, PID: 4243, Source: "chub_registry"}}
|
||||
stopper := instanceStopper{launcher: managed, managedProfiles: managed, waitTimeout: 10 * time.Millisecond, waitInterval: time.Millisecond}
|
||||
err := stopper.Stop(context.Background(), ui.InstanceRow{Status: "运行中", PID: 4242, OccupancySource: "chub_registry", UserDataDir: `C:\profiles\managed`})
|
||||
if !errors.Is(err, domain.ErrIdentityMismatch) || managed.stopCalls != 0 {
|
||||
t.Fatalf("mismatch Stop() = %v, calls=%d", err, managed.stopCalls)
|
||||
}
|
||||
err = stopper.Stop(context.Background(), ui.InstanceRow{Status: "运行中", PID: 4243, OccupancySource: "browser_message_window", UserDataDir: `C:\profiles\managed`})
|
||||
if !errors.Is(err, domain.ErrIdentityMismatch) || managed.stopCalls != 0 {
|
||||
t.Fatalf("external Stop() = %v, calls=%d", err, managed.stopCalls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInstanceStopperDoesNotEscalateWhenGracefulCloseTimesOut(t *testing.T) {
|
||||
managed := &fakeManagedStopper{use: browser.ProfileUse{Occupied: true, PID: 4242, Source: "chub_registry"}}
|
||||
stopper := instanceStopper{launcher: managed, managedProfiles: managed, waitTimeout: 10 * time.Millisecond, waitInterval: time.Millisecond}
|
||||
err := stopper.Stop(context.Background(), ui.InstanceRow{Status: "运行中", PID: 4242, OccupancySource: "chub_registry", UserDataDir: `C:\profiles\managed`})
|
||||
if err == nil || managed.stopCalls != 1 || managed.force {
|
||||
t.Fatalf("timeout Stop() = %v, calls=%d force=%v", err, managed.stopCalls, managed.force)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInstanceStarterAssociatesVerifiedExternalProfileWithoutLaunching(t *testing.T) {
|
||||
launcher := &fakeProcessLauncher{handle: fakeProcessHandle{pid: 4242}}
|
||||
remote := &fakeRemoteDebugInspector{endpoint: browser.RemoteDebugEndpoint{Port: 9668}}
|
||||
@@ -187,6 +219,28 @@ func (i fakeManagedProfileInspector) InspectProfile(context.Context, string) (br
|
||||
return i.use, nil
|
||||
}
|
||||
|
||||
type fakeManagedStopper struct {
|
||||
use browser.ProfileUse
|
||||
stopCalls int
|
||||
force bool
|
||||
userDataDir string
|
||||
releaseOnStop bool
|
||||
}
|
||||
|
||||
func (s *fakeManagedStopper) InspectProfile(context.Context, string) (browser.ProfileUse, error) {
|
||||
return s.use, nil
|
||||
}
|
||||
|
||||
func (s *fakeManagedStopper) StopProfile(_ context.Context, userDataDir string, force bool) error {
|
||||
s.stopCalls++
|
||||
s.force = force
|
||||
s.userDataDir = userDataDir
|
||||
if s.releaseOnStop {
|
||||
s.use.Occupied = false
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type fakeExternalProfileInspector struct {
|
||||
use browser.ProfileUse
|
||||
err error
|
||||
|
||||
Reference in New Issue
Block a user