Source file test/fixedbugs/issue21576.go

     1  // run
     2  
     3  //go:build !nacl && !js && !wasip1 && !gccgo
     4  
     5  // Copyright 2019 The Go Authors. All rights reserved.
     6  // Use of this source code is governed by a BSD-style
     7  // license that can be found in the LICENSE file.
     8  //
     9  // Ensure that deadlock detection can still
    10  // run even with an import of "_ os/signal".
    11  
    12  package main
    13  
    14  import (
    15  	"bytes"
    16  	"context"
    17  	"io/ioutil"
    18  	"log"
    19  	"os"
    20  	"os/exec"
    21  	"path/filepath"
    22  	"time"
    23  )
    24  
    25  const prog = `
    26  package main
    27  
    28  import _ "os/signal"
    29  
    30  func main() {
    31    c := make(chan int)
    32    c <- 1
    33  }
    34  `
    35  
    36  func main() {
    37  	dir, err := ioutil.TempDir("", "21576")
    38  	if err != nil {
    39  		log.Fatal(err)
    40  	}
    41  	defer os.RemoveAll(dir)
    42  
    43  	file := filepath.Join(dir, "main.go")
    44  	if err := ioutil.WriteFile(file, []byte(prog), 0655); err != nil {
    45  		log.Fatalf("Write error %v", err)
    46  	}
    47  
    48  	// Using a timeout of 1 minute in case other factors might slow
    49  	// down the start of "go run". See https://golang.org/issue/34836.
    50  	ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
    51  	defer cancel()
    52  
    53  	cmd := exec.CommandContext(ctx, "go", "run", file)
    54  	output, err := cmd.CombinedOutput()
    55  	if err == nil {
    56  		log.Fatalf("Passed, expected an error")
    57  	}
    58  
    59  	want := []byte("fatal error: all goroutines are asleep - deadlock!")
    60  	if !bytes.Contains(output, want) {
    61  		log.Fatalf("Unmatched error message %q:\nin\n%s\nError: %v", want, output, err)
    62  	}
    63  }
    64  

View as plain text