github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/cmd/state-svc/internal/rtwatcher/watcher_test.go (about)

     1  package rtwatcher
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/ActiveState/cli/internal/analytics/client/blackhole"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func TestWatcher_ticker(t *testing.T) {
    13  	w := &Watcher{stop: make(chan struct{}), interval: (10 * time.Millisecond)}
    14  	calls := 0
    15  	go func() {
    16  		time.Sleep(100 * time.Millisecond)
    17  		w.stop <- struct{}{}
    18  	}()
    19  	w.ticker(func() {
    20  		calls++
    21  	})
    22  	require.Equal(t, 10, calls)
    23  }
    24  
    25  func TestWatcher_check(t *testing.T) {
    26  	w := &Watcher{an: blackhole.Client{}, stop: make(chan struct{}), interval: (10 * time.Millisecond)}
    27  	entries := []entry{
    28  		{
    29  			PID:  123,
    30  			Exec: "not-running",
    31  		},
    32  		{
    33  			PID:  os.Getpid(),
    34  			Exec: os.Args[0],
    35  		},
    36  	}
    37  	w.watching = append(w.watching, entries...)
    38  	go w.ticker(w.check)
    39  
    40  	time.Sleep(10 * time.Millisecond)
    41  	w.stop <- struct{}{}
    42  
    43  	require.Len(t, w.watching, 1, "Not running process should be removed")
    44  }