github.com/haraldrudell/parl@v0.4.176/counter/rate-counter_test.go (about)

     1  /*
     2  © 2022–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
     3  ISC License
     4  */
     5  
     6  package counter
     7  
     8  import (
     9  	"context"
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/haraldrudell/parl"
    14  	"github.com/haraldrudell/parl/g0"
    15  )
    16  
    17  func Test_newRateCounter(t *testing.T) {
    18  	messagePeriod := "period must be positive"
    19  
    20  	var err error
    21  
    22  	var counters parl.Counters
    23  	var counterCounters *Counters
    24  
    25  	// g0 nil: no counter threads allowed
    26  	var threadGroup = g0.NewGoGroup(context.Background())
    27  	counters = newCounters(threadGroup.Go())
    28  	counterCounters, _ = counters.(*Counters)
    29  
    30  	// newRateCounter zero period panic
    31  	counterCounters.AddTask(0, newRateCounter())
    32  	goError := <-threadGroup.Ch()
    33  	err = goError.Err()
    34  	if err == nil {
    35  		t.Error("RecoverInvocationPanic exp panic missing")
    36  	} else if !strings.Contains(err.Error(), messagePeriod) {
    37  		t.Errorf("RecoverInvocationPanic bad err: %q exp %q", err.Error(), messagePeriod)
    38  	}
    39  }