MCPcopy
hub / github.com/crowdsecurity/crowdsec / TestAverageInterval

Function TestAverageInterval

pkg/exprhelpers/exprlib_test.go:713–778  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

711}
712
713func TestAverageInterval(t *testing.T) {
714 err := Init(nil)
715 require.NoError(t, err)
716
717 // Use a fixed base time to eliminate execution time variance
718 baseTime := time.Date(2023, 1, 1, 12, 0, 0, 0, time.UTC)
719
720 tests := []struct {
721 name string
722 env map[string]any
723 code string
724 want time.Duration
725 }{
726 {
727 name: "AverageInterval() test: two times with 1 second difference",
728 env: map[string]any{
729 "times": []time.Time{baseTime, baseTime.Add(time.Second)},
730 },
731 code: "AverageInterval(times)",
732 want: time.Second,
733 },
734 {
735 name: "AverageInterval() test: two times with 1 second difference (reverse order)",
736 env: map[string]any{
737 "times": []time.Time{baseTime.Add(time.Second), baseTime},
738 },
739 code: "AverageInterval(times)",
740 want: time.Second,
741 },
742 {
743 name: "AverageInterval() test: three times with varying intervals",
744 env: map[string]any{
745 "times": []time.Time{
746 baseTime,
747 baseTime.Add(2 * time.Second), // 2s gap
748 baseTime.Add(6 * time.Second), // 4s gap
749 },
750 },
751 code: "AverageInterval(times)",
752 want: 3 * time.Second, // (2s + 4s) / 2 = 3s average
753 },
754 {
755 name: "AverageInterval() test: four times with equal intervals",
756 env: map[string]any{
757 "times": []time.Time{
758 baseTime,
759 baseTime.Add(time.Hour),
760 baseTime.Add(2 * time.Hour),
761 baseTime.Add(3 * time.Hour),
762 },
763 },
764 code: "AverageInterval(times)",
765 want: time.Hour, // all intervals are 1 hour
766 },
767 }
768
769 for _, test := range tests {
770 t.Run(test.name, func(t *testing.T) {

Callers

nothing calls this directly

Calls 5

InitFunction · 0.85
GetExprOptionsFunction · 0.85
RunMethod · 0.65
AddMethod · 0.45
CompileMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…