aboutsummaryrefslogtreecommitdiffhomepage
path: root/testdata/signal.go
blob: a82991f08616d4a0e53efce53d71a6ff7b7ec12c (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
package main

// Test POSIX signals.
// TODO: run `tinygo test os/signal` instead, once CGo errno return values are
// supported.

import (
	"os"
	"os/signal"
	"syscall"
	"time"
)

func main() {
	c := make(chan os.Signal, 1)
	signal.Notify(c, syscall.SIGUSR1)

	// Wait for signals to arrive.
	go func() {
		for sig := range c {
			if sig == syscall.SIGUSR1 {
				println("got expected signal")
			} else {
				println("got signal:", sig.String())
			}
		}
	}()

	// Send the signal.
	syscall.Kill(syscall.Getpid(), syscall.SIGUSR1)

	time.Sleep(time.Millisecond * 100)

	// Stop notifying.
	// (This is just a smoke test, it's difficult to test the default behavior
	// in a unit test).
	signal.Ignore(syscall.SIGUSR1)

	signal.Stop(c)

	println("exiting signal program")
}