github.com/wangyougui/gf/v2@v2.6.5/os/gproc/gproc_z_signal_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/wangyougui/gf.
     6  
     7  package gproc
     8  
     9  import (
    10  	"github.com/wangyougui/gf/v2/test/gtest"
    11  	"os"
    12  	"syscall"
    13  	"testing"
    14  	"time"
    15  )
    16  
    17  func Test_Signal(t *testing.T) {
    18  	go Listen()
    19  
    20  	// non shutdown signal
    21  	gtest.C(t, func(t *gtest.T) {
    22  		sigRec := make(chan os.Signal, 1)
    23  		AddSigHandler(func(sig os.Signal) {
    24  			sigRec <- sig
    25  		}, syscall.SIGUSR1, syscall.SIGUSR2)
    26  
    27  		sendSignal(syscall.SIGUSR1)
    28  		select {
    29  		case s := <-sigRec:
    30  			t.AssertEQ(s, syscall.SIGUSR1)
    31  			t.AssertEQ(false, isWaitChClosed())
    32  		case <-time.After(time.Second):
    33  			t.Error("signal SIGUSR1 handler timeout")
    34  		}
    35  
    36  		sendSignal(syscall.SIGUSR2)
    37  		select {
    38  		case s := <-sigRec:
    39  			t.AssertEQ(s, syscall.SIGUSR2)
    40  			t.AssertEQ(false, isWaitChClosed())
    41  		case <-time.After(time.Second):
    42  			t.Error("signal SIGUSR2 handler timeout")
    43  		}
    44  
    45  		sendSignal(syscall.SIGHUP)
    46  		select {
    47  		case <-sigRec:
    48  			t.Error("signal SIGHUP should not be listen")
    49  		case <-time.After(time.Millisecond * 100):
    50  		}
    51  
    52  		// multiple listen
    53  		go Listen()
    54  		go Listen()
    55  		sendSignal(syscall.SIGUSR1)
    56  		cnt := 0
    57  		timeout := time.After(time.Second)
    58  		for {
    59  			select {
    60  			case <-sigRec:
    61  				cnt++
    62  			case <-timeout:
    63  				if cnt == 0 {
    64  					t.Error("signal SIGUSR2 handler timeout")
    65  				}
    66  				if cnt != 1 {
    67  					t.Error("multi Listen() repetitive execution")
    68  				}
    69  				return
    70  			}
    71  		}
    72  	})
    73  
    74  	// test shutdown signal
    75  	gtest.C(t, func(t *gtest.T) {
    76  		sigRec := make(chan os.Signal, 1)
    77  		AddSigHandlerShutdown(func(sig os.Signal) {
    78  			sigRec <- sig
    79  		})
    80  
    81  		sendSignal(syscall.SIGTERM)
    82  		select {
    83  		case s := <-sigRec:
    84  			t.AssertEQ(s, syscall.SIGTERM)
    85  			t.AssertEQ(true, isWaitChClosed())
    86  		case <-time.After(time.Second):
    87  			t.Error("signal SIGUSR2 handler timeout")
    88  		}
    89  	})
    90  }
    91  
    92  func sendSignal(sig os.Signal) {
    93  	signalChan <- sig
    94  }
    95  
    96  func isWaitChClosed() bool {
    97  	select {
    98  	case _, ok := <-waitChan:
    99  		return !ok
   100  	default:
   101  		return false
   102  	}
   103  }