github.com/haraldrudell/parl@v0.4.176/counter/datapoint_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  	"strings"
    10  	"testing"
    11  	"time"
    12  
    13  	"github.com/haraldrudell/parl"
    14  )
    15  
    16  func Test_newDatapoint(t *testing.T) {
    17  	messagePeriod := "period must be positive"
    18  	value0 := uint64(1)
    19  	value1 := uint64(11)
    20  	expAverage := (value0 + value1) / 2
    21  	period := time.Second
    22  	now := time.Now()
    23  	future := now.Add(period)
    24  	tPeriod0Start := time.Now().Truncate(period)
    25  	tPeriod1Start := tPeriod0Start.Add(period)
    26  	tPeriod2Start := tPeriod0Start.Add(period)
    27  
    28  	var err error
    29  	var datapointProvider parl.Datapoint
    30  	var datapointConsumer parl.DatapointValue
    31  	var datapoint *Datapoint
    32  	var datapoint2 parl.Datapoint
    33  	var value uint64
    34  	var max uint64
    35  	var min uint64
    36  	var isValid bool
    37  	var average float64
    38  	var n uint64
    39  
    40  	_ = expAverage
    41  	_ = future
    42  	_ = tPeriod1Start
    43  	_ = tPeriod2Start
    44  	_ = datapoint2
    45  
    46  	// instantiate datapoint
    47  	datapointProvider = newDatapoint(period)
    48  	datapointConsumer = datapointProvider.(parl.DatapointValue)
    49  	datapoint = datapointProvider.(*Datapoint)
    50  	if datapoint.period != period {
    51  		t.Errorf("datapoint bad period: %s exp %s", datapoint.period, period)
    52  	}
    53  
    54  	// add first value
    55  	datapointProvider.SetValue(value0)
    56  	value, max, min, isValid, _, _ = datapointConsumer.GetDatapoint()
    57  	if value != value0 {
    58  		t.Errorf("datapoint1 bad value: %d exp %d", value, value0)
    59  	}
    60  	if max != value0 {
    61  		t.Errorf("datapoint1 bad max: %d exp %d", max, value0)
    62  	}
    63  	if min != value0 {
    64  		t.Errorf("datapoint1 bad min: %d exp %d", min, value0)
    65  	}
    66  	if datapoint.periodStart.IsZero() {
    67  		t.Error("datapoint1 periodStart zero")
    68  	}
    69  	if !isValid {
    70  		t.Error("datapoint1 isValid false")
    71  	}
    72  	if datapoint.periodStart != tPeriod0Start {
    73  		t.Errorf("datapoint1 bad periodStart: %s exp %s",
    74  			datapoint.periodStart.Format(parl.Rfc3339ns),
    75  			tPeriod0Start.Format(parl.Rfc3339ns))
    76  	}
    77  	if datapoint.isFullPeriod {
    78  		t.Error("datapoint1 isFullPeriod true")
    79  	}
    80  
    81  	// DatapointValue
    82  	value = datapointConsumer.DatapointValue()
    83  	if value != value0 {
    84  		t.Errorf("DatapointValue bad value: %d exp %d", value, value0)
    85  	}
    86  
    87  	// DatapointMax
    88  	max = datapointConsumer.DatapointMax()
    89  	if max != value0 {
    90  		t.Errorf("DatapointMax bad max: %d exp %d", max, value0)
    91  	}
    92  
    93  	// DatapointMin
    94  	min = datapointConsumer.DatapointMin()
    95  	if min != value0 {
    96  		t.Errorf("DatapointMin bad min: %d exp %d", min, value0)
    97  	}
    98  
    99  	// add second value
   100  	datapointProvider.SetValue(value1)
   101  	value, max, min, isValid, average, n = datapointConsumer.GetDatapoint()
   102  	if value != value1 {
   103  		t.Errorf("datapoint2 bad value: %d exp %d", value, value1)
   104  	}
   105  	if max != value1 {
   106  		t.Errorf("datapoint2 bad max: %d exp %d", max, value1)
   107  	}
   108  	if min != value0 {
   109  		t.Errorf("datapoint2 bad min: %d exp %d", min, value0)
   110  	}
   111  	if datapoint.periodStart.IsZero() {
   112  		t.Error("datapoint2 periodStart zero")
   113  	}
   114  	if !isValid {
   115  		t.Error("datapoint2 isValid false")
   116  	}
   117  
   118  	// end period
   119  	_ = average
   120  	_ = n
   121  
   122  	// newDatapoint zero period panic
   123  	if err = invokeNewDatapoint(); err == nil {
   124  		t.Error("RecoverInvocationPanic exp panic missing")
   125  	} else if !strings.Contains(err.Error(), messagePeriod) {
   126  		t.Errorf("RecoverInvocationPanic bad err: %q exp %q", err.Error(), messagePeriod)
   127  	}
   128  }
   129  
   130  func invokeNewDatapoint() (err error) {
   131  	defer parl.PanicToErr(&err)
   132  
   133  	newDatapoint(0)
   134  
   135  	return
   136  }