github.com/msales/pkg/v3@v3.24.0/clix/utils_test.go (about)

     1  package clix_test
     2  
     3  import (
     4  	"errors"
     5  	"sync"
     6  	"syscall"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/msales/pkg/v3/clix"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func Test_SplitTags(t *testing.T) {
    15  	tests := []struct {
    16  		tags []string
    17  
    18  		results []interface{}
    19  		err     error
    20  	}{
    21  		{[]string{"a=b"}, []interface{}{"a", "b"}, nil},
    22  		{[]string{"a=b", "c=d"}, []interface{}{"a", "b", "c", "d"}, nil},
    23  		{[]string{"a"}, nil, errors.New("invalid tags string")},
    24  		{[]string{"a=b", "c"}, nil, errors.New("invalid tags string")},
    25  	}
    26  
    27  	for _, tt := range tests {
    28  		res, err := clix.SplitTags(tt.tags, "=")
    29  
    30  		assert.Equal(t, res, tt.results)
    31  		assert.Equal(t, err, tt.err)
    32  	}
    33  }
    34  
    35  func TestWaitForSignals(t *testing.T) {
    36  	tests := []struct {
    37  		signal syscall.Signal
    38  	}{
    39  		{signal: syscall.SIGINT},
    40  		{signal: syscall.SIGTERM},
    41  	}
    42  
    43  	var wg sync.WaitGroup
    44  	for _, tt := range tests {
    45  		go func() {
    46  			wg.Add(1)
    47  			defer wg.Done()
    48  
    49  			select {
    50  			case s := <-clix.WaitForSignals():
    51  				assert.Equal(t, tt.signal, s)
    52  			case <-time.After(1 * time.Second):
    53  				assert.Failf(t, "", "Timeout waiting for %v", tt.signal)
    54  			}
    55  		}()
    56  
    57  		time.Sleep(time.Millisecond)
    58  
    59  		syscall.Kill(syscall.Getpid(), tt.signal)
    60  
    61  		wg.Wait()
    62  	}
    63  }