| 1 |
package sturdyc_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
"time" |
| 6 |
|
| 7 |
"github.com/viccon/sturdyc" |
| 8 |
) |
| 9 |
|
| 10 |
type testCutoffCase struct { |
| 11 |
name string |
| 12 |
numEntries int |
| 13 |
percentile float64 |
| 14 |
kIndex int |
| 15 |
} |
| 16 |
|
| 17 |
func TestCutoff(t *testing.T) { |
| 18 |
t.Parallel() |
| 19 |
|
| 20 |
testCases := []testCutoffCase{ |
| 21 |
{ |
| 22 |
name: "1000 entries, 50th percentile", |
| 23 |
numEntries: 1000, |
| 24 |
percentile: 0.5, |
| 25 |
kIndex: 500, |
| 26 |
}, |
| 27 |
{ |
| 28 |
name: "1000 entries, 90th percentile", |
| 29 |
numEntries: 1000, |
| 30 |
percentile: 0.9, |
| 31 |
kIndex: 900, |
| 32 |
}, |
| 33 |
{ |
| 34 |
name: "999 entries, 16th percentile", |
| 35 |
numEntries: 999, |
| 36 |
percentile: 0.16, |
| 37 |
kIndex: 159, |
| 38 |
}, |
| 39 |
{ |
| 40 |
name: "13 entries, 2nd percentile", |
| 41 |
numEntries: 13, |
| 42 |
percentile: 0.02, |
| 43 |
kIndex: 0, |
| 44 |
}, |
| 45 |
} |
| 46 |
|
| 47 |
for _, tc := range testCases { |
| 48 |
tc := tc |
| 49 |
t.Run(tc.name, func(t *testing.T) { |
| 50 |
t.Parallel() |
| 51 |
now := time.Now() |
| 52 |
timestamps := make([]time.Time, 0, tc.numEntries) |
| 53 |
for i := 0; i < tc.numEntries; i++ { |
| 54 |
newTime := now.Add(time.Duration(i) * time.Second) |
| 55 |
timestamps = append(timestamps, newTime) |
| 56 |
} |
| 57 |
cutoff := sturdyc.FindCutoff(timestamps, tc.percentile) |
| 58 |
if cutoff != timestamps[tc.kIndex] { |
| 59 |
t.Errorf("expected cutoff to be %v, got %v", timestamps[tc.kIndex], cutoff) |
| 60 |
} |
| 61 |
}) |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
func TestCutOffSameTime(t *testing.T) { |
| 66 |
t.Parallel() |
| 67 |
now := time.Now() |
| 68 |
timestamps := make([]time.Time, 0, 100) |
| 69 |
for i := 0; i < 100; i++ { |
| 70 |
timestamps = append(timestamps, now) |
| 71 |
} |
| 72 |
|
| 73 |
// Given that we have a list where all the timestamps are the same, we |
| 74 |
// should get that same timestamp back for every percentile. |
| 75 |
cutoffOne := sturdyc.FindCutoff(timestamps, 0.1) |
| 76 |
cutoffTwo := sturdyc.FindCutoff(timestamps, 0.3) |
| 77 |
cutoffThree := sturdyc.FindCutoff(timestamps, 0.5) |
| 78 |
if cutoffOne != now { |
| 79 |
t.Errorf("expected cutoff to be %v, got %v", now, cutoffOne) |
| 80 |
} |
| 81 |
if cutoffTwo != now { |
| 82 |
t.Errorf("expected cutoff to be %v, got %v", now, cutoffTwo) |
| 83 |
} |
| 84 |
if cutoffThree != now { |
| 85 |
t.Errorf("expected cutoff to be %v, got %v", now, cutoffThree) |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
func TestCutOffTwoTimes(t *testing.T) { |
| 90 |
t.Parallel() |
| 91 |
timestamps := make([]time.Time, 0, 100) |
| 92 |
|
| 93 |
firstTime := time.Now() |
| 94 |
for i := 0; i < 50; i++ { |
| 95 |
timestamps = append(timestamps, firstTime) |
| 96 |
} |
| 97 |
|
| 98 |
secondTime := time.Now().Add(time.Second) |
| 99 |
for i := 0; i < 50; i++ { |
| 100 |
timestamps = append(timestamps, secondTime) |
| 101 |
} |
| 102 |
|
| 103 |
firstCutoff := sturdyc.FindCutoff(timestamps, 0.49) |
| 104 |
if firstCutoff != firstTime { |
| 105 |
t.Errorf("expected cutoff to be %v, got %v", firstTime, firstCutoff) |
| 106 |
} |
| 107 |
|
| 108 |
secondCutoff := sturdyc.FindCutoff(timestamps, 0.51) |
| 109 |
if secondCutoff != secondTime { |
| 110 |
t.Errorf("expected cutoff to be %v, got %v", secondTime, secondCutoff) |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
func TestReturnsEmptyTimeIfArgumentsAreInvalid(t *testing.T) { |
| 115 |
t.Parallel() |
| 116 |
|
| 117 |
values := make([]time.Time, 0, 10) |
| 118 |
for i := 0; i < 10; i++ { |
| 119 |
values = append(values, time.Now().Add(time.Duration(i)*time.Second)) |
| 120 |
} |
| 121 |
|
| 122 |
if k := sturdyc.FindCutoff(values, -1); !k.IsZero() { |
| 123 |
t.Errorf("expected zero time, got %v", k) |
| 124 |
} |
| 125 |
|
| 126 |
if k := sturdyc.FindCutoff(values, 1.1); !k.IsZero() { |
| 127 |
t.Errorf("expected zero time, got %v", k) |
| 128 |
} |
| 129 |
|
| 130 |
if k := sturdyc.FindCutoff([]time.Time{}, 0.4); !k.IsZero() { |
| 131 |
t.Errorf("expected zero time, got %v", k) |
| 132 |
} |
| 133 |
} |