package windows import ( "testing" "time" ) // TestSwitchDesktopCOMCrossGoroutine 模拟真实触发模式:每次切换由 // 独立 goroutine(可能不同 OS 线程)发起,复现"第一次成功第二次崩溃"。 func TestSwitchDesktopCOMCrossGoroutine(t *testing.T) { if testing.Short() { t.Skip("short mode") } // 先初始化缓存(模拟程序长期运行后首次触发) procCoInitializeEx.Call(0, coinitMultithreaded) procCoUninitialize.Call() for i := 0; i < 4; i++ { dir := int32(1) if i%2 == 1 { dir = -1 } done := make(chan error, 1) go func() { done <- SwitchDesktopCOM(dir) }() select { case err := <-done: if err != nil { t.Logf("第%d次切换返回错误: %v", i+1, err) } case <-time.After(5 * time.Second): t.Fatalf("第%d次切换超时", i+1) } time.Sleep(300 * time.Millisecond) } }