github.com/activestate/go@v0.0.0-20170614201249-0b81c023a722/src/cmd/go/testdata/inprogress_interrupt_test.go (about)

     1  // Copyright 2017 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  package inprogress_interrupt_test
     5  
     6  import (
     7  	"os"
     8  	"os/signal"
     9  	"sync"
    10  	"syscall"
    11  	"testing"
    12  )
    13  
    14  func TestParallel(t *testing.T) {
    15  	t.Parallel()
    16  }
    17  
    18  func TestSerial(t *testing.T) {
    19  	sigCh := make(chan os.Signal, 1)
    20  	signal.Notify(sigCh, os.Interrupt)
    21  
    22  	var wg sync.WaitGroup
    23  	wg.Add(1)
    24  	go func() {
    25  		<-sigCh // catch initial signal
    26  		<-sigCh // catch propagated signal
    27  		wg.Done()
    28  	}()
    29  
    30  	proc, err := os.FindProcess(syscall.Getpid())
    31  	if err != nil {
    32  		t.Fatalf("unable to find current process: %v", err)
    33  	}
    34  	err = proc.Signal(os.Interrupt)
    35  	if err != nil {
    36  		t.Fatalf("failed to interrupt current process: %v", err)
    37  	}
    38  
    39  	wg.Wait()
    40  }