...

Source file src/github.com/bazelbuild/rules_go/go/tools/bzltestutil/timeout.go

Documentation: github.com/bazelbuild/rules_go/go/tools/bzltestutil

     1  // Copyright 2020 The Bazel Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package bzltestutil
    16  
    17  import (
    18  	"fmt"
    19  	"os"
    20  	"os/signal"
    21  	"runtime"
    22  	"syscall"
    23  )
    24  
    25  func RegisterTimeoutHandler() {
    26  	// When the Bazel test timeout is reached, Bazel sends a SIGTERM. We print stack traces for all
    27  	// goroutines just like native go test would. We do not panic (like native go test does) because
    28  	// users may legitimately want to use SIGTERM in tests and prints are less disruptive than
    29  	// panics in that case.
    30  	// See https://github.com/golang/go/blob/e816eb50140841c524fd07ecb4eaa078954eb47c/src/testing/testing.go#L2351
    31  	c := make(chan os.Signal, 1)
    32  	signal.Notify(c, syscall.SIGTERM)
    33  	go func() {
    34  		<-c
    35  		buf := make([]byte, 1<<24)
    36  		stacklen := runtime.Stack(buf, true)
    37  		fmt.Printf("Received SIGTERM, printing stack traces of all goroutines:\n%s", buf[:stacklen])
    38  	}()
    39  }
    40  

View as plain text