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

Function TestMedianInterval

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

Source from the content-addressed store, hash-verified

778}
779
780func TestMedianInterval(t *testing.T) {
781 err := Init(nil)
782 require.NoError(t, err)
783
784 // Use a fixed base time to eliminate execution time variance
785 baseTime := time.Date(2023, 1, 1, 12, 0, 0, 0, time.UTC)
786
787 tests := []struct {
788 name string
789 env map[string]any
790 code string
791 want time.Duration
792 wantErr bool
793 errContains string
794 }{
795 {
796 name: "MedianInterval() test: two times with 1 second difference",
797 env: map[string]any{
798 "times": []time.Time{baseTime, baseTime.Add(time.Second)},
799 },
800 code: "MedianInterval(times)",
801 want: time.Second,
802 },
803 {
804 name: "MedianInterval() test: three times - odd number of intervals",
805 env: map[string]any{
806 "times": []time.Time{
807 baseTime,
808 baseTime.Add(2 * time.Second), // 2s gap
809 baseTime.Add(5 * time.Second), // 3s gap
810 baseTime.Add(11 * time.Second), // 6s gap
811 },
812 },
813 code: "MedianInterval(times)",
814 want: 3 * time.Second, // median of [2s, 3s, 6s] = 3s
815 },
816 {
817 name: "MedianInterval() test: four times - even number of intervals",
818 env: map[string]any{
819 "times": []time.Time{
820 baseTime,
821 baseTime.Add(1 * time.Second), // 1s gap
822 baseTime.Add(3 * time.Second), // 2s gap
823 baseTime.Add(7 * time.Second), // 4s gap
824 baseTime.Add(15 * time.Second), // 8s gap
825 },
826 },
827 code: "MedianInterval(times)",
828 want: 3 * time.Second, // median of [1s, 2s, 4s, 8s] = (2s + 4s) / 2 = 3s
829 },
830 {
831 name: "MedianInterval() test: reverse order times",
832 env: map[string]any{
833 "times": []time.Time{
834 baseTime.Add(11 * time.Second),
835 baseTime.Add(5 * time.Second),
836 baseTime.Add(2 * time.Second),
837 baseTime,

Callers

nothing calls this directly

Calls 6

InitFunction · 0.85
GetExprOptionsFunction · 0.85
RunMethod · 0.65
ErrorMethod · 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…