github.com/gogf/gf/v2@v2.7.4/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/gogf/gf.
     6  
     7  package gproc
     8  
     9  import (
    10  	"os"
    11  	"syscall"
    12  	"testing"
    13  	"time"
    14  
    15  	"github.com/gogf/gf/v2/test/gtest"
    16  )
    17  
    18  func Test_Signal(t *testing.T) {
    19  	go Listen()
    20  
    21  	// non shutdown signal
    22  	gtest.C(t, func(t *gtest.T) {
    23  		sigRec := make(chan os.Signal, 1)
    24  		AddSigHandler(func(sig os.Signal) {
    25  			sigRec <- sig
    26  		}, syscall.SIGUSR1, syscall.SIGUSR2)
    27  
    28  		sendSignal(syscall.SIGUSR1)
    29  		select {
    30  		case s := <-sigRec:
    31  			t.AssertEQ(s, syscall.SIGUSR1)
    32  			t.AssertEQ(false, isWaitChClosed())
    33  		case <-time.After(time.Second):
    34  			t.Error("signal SIGUSR1 handler timeout")
    35  		}
    36  
    37  		sendSignal(syscall.SIGUSR2)
    38  		select {
    39  		case s := <-sigRec:
    40  			t.AssertEQ(s, syscall.SIGUSR2)
    41  			t.AssertEQ(false, isWaitChClosed())
    42  		case <-time.After(time.Second):
    43  			t.Error("signal SIGUSR2 handler timeout")
    44  		}
    45  
    46  		sendSignal(syscall.SIGHUP)
    47  		select {
    48  		case <-sigRec:
    49  			t.Error("signal SIGHUP should not be listen")
    50  		case <-time.After(time.Millisecond * 100):
    51  		}
    52  
    53  		// multiple listen
    54  		go Listen()
    55  		go Listen()
    56  		sendSignal(syscall.SIGUSR1)
    57  		cnt := 0
    58  		timeout := time.After(time.Second)
    59  		for {
    60  			select {
    61  			case <-sigRec:
    62  				cnt++
    63  			case <-timeout:
    64  				if cnt == 0 {
    65  					t.Error("signal SIGUSR2 handler timeout")
    66  				}
    67  				if cnt != 1 {
    68  					t.Error("multi Listen() repetitive execution")
    69  				}
    70  				return
    71  			}
    72  		}
    73  	})
    74  
    75  	// test shutdown signal
    76  	gtest.C(t, func(t *gtest.T) {
    77  		sigRec := make(chan os.Signal, 1)
    78  		AddSigHandlerShutdown(func(sig os.Signal) {
    79  			sigRec <- sig
    80  		})
    81  
    82  		sendSignal(syscall.SIGTERM)
    83  		// wait the listen done
    84  		time.Sleep(time.Second)
    85  
    86  		select {
    87  		case s := <-sigRec:
    88  			t.AssertEQ(s, syscall.SIGTERM)
    89  			t.AssertEQ(true, isWaitChClosed())
    90  		case <-time.After(time.Second):
    91  			t.Error("signal SIGUSR2 handler timeout")
    92  		}
    93  	})
    94  }
    95  
    96  func sendSignal(sig os.Signal) {
    97  	signalChan <- sig
    98  }
    99  
   100  func isWaitChClosed() bool {
   101  	select {
   102  	case _, ok := <-waitChan:
   103  		return !ok
   104  	default:
   105  		return false
   106  	}
   107  }