fork of https://github.com/sourcegraph/zoekt
1package server
2
3import (
4 "testing"
5
6 "github.com/sourcegraph/zoekt"
7)
8
9func TestSamplingStream(t *testing.T) {
10 nonZeroStats := zoekt.Stats{
11 ContentBytesLoaded: 10,
12 }
13 filesEvent := &zoekt.SearchResult{
14 Files: make([]zoekt.FileMatch, 10),
15 Stats: nonZeroStats,
16 }
17 fileEvents := func(n int) []*zoekt.SearchResult {
18 res := make([]*zoekt.SearchResult, n)
19 for i := range n {
20 res[i] = filesEvent
21 }
22 return res
23 }
24 statsEvent := &zoekt.SearchResult{
25 Stats: nonZeroStats,
26 }
27 statsEvents := func(n int) []*zoekt.SearchResult {
28 res := make([]*zoekt.SearchResult, n)
29 for i := range n {
30 res[i] = statsEvent
31 }
32 return res
33 }
34 cases := []struct {
35 events []*zoekt.SearchResult
36 beforeFlushCount int
37 afterFlushCount int
38 }{
39 // These test cases assume that the sampler only forwards
40 // every 100 stats-only event. In case the sampling logic
41 // changes, these tests are not valuable.
42 {nil, 0, 0},
43 {fileEvents(1), 1, 1},
44 {fileEvents(2), 2, 2},
45 {fileEvents(200), 200, 200},
46 {append(fileEvents(1), statsEvents(1)...), 1, 2},
47 {append(fileEvents(1), statsEvents(2)...), 1, 2},
48 {append(fileEvents(1), statsEvents(99)...), 1, 2},
49 {append(fileEvents(1), statsEvents(100)...), 2, 2},
50 {statsEvents(500), 5, 5},
51 {statsEvents(501), 5, 6},
52 }
53
54 for _, tc := range cases {
55 count := 0
56 ss := newSamplingSender(zoekt.SenderFunc(func(*zoekt.SearchResult) {
57 count += 1
58 }))
59
60 for _, event := range tc.events {
61 ss.Send(event)
62 }
63 if count != tc.beforeFlushCount {
64 t.Fatalf("expected %d events, got %d", tc.beforeFlushCount, count)
65 }
66 ss.Flush()
67
68 if count != tc.afterFlushCount {
69 t.Fatalf("expected %d events, got %d", tc.afterFlushCount, count)
70 }
71 }
72}