summaryrefslogtreecommitdiff
path: root/src/pkg/os/signal/signal_windows_test.go
blob: 8d807ff7b18d87160a6645228f5a8c8ac7c12e5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package signal

import (
	"flag"
	"os"
	"syscall"
	"testing"
	"time"
)

var runCtrlBreakTest = flag.Bool("run_ctlbrk_test", false, "force to run Ctrl+Break test")

func sendCtrlBreak(t *testing.T) {
	d, e := syscall.LoadDLL("kernel32.dll")
	if e != nil {
		t.Fatalf("LoadDLL: %v\n", e)
	}
	p, e := d.FindProc("GenerateConsoleCtrlEvent")
	if e != nil {
		t.Fatalf("FindProc: %v\n", e)
	}
	r, _, e := p.Call(0, 0)
	if r == 0 {
		t.Fatalf("GenerateConsoleCtrlEvent: %v\n", e)
	}
}

func TestCtrlBreak(t *testing.T) {
	if !*runCtrlBreakTest {
		t.Logf("test disabled; use -run_ctlbrk_test to enable")
		return
	}
	go func() {
		time.Sleep(1 * time.Second)
		sendCtrlBreak(t)
	}()
	c := make(chan os.Signal, 10)
	Notify(c)
	select {
	case s := <-c:
		if s != os.Interrupt {
			t.Fatalf("Wrong signal received: got %q, want %q\n", s, os.Interrupt)
		}
	case <-time.After(3 * time.Second):
		t.Fatalf("Timeout waiting for Ctrl+Break\n")
	}
}