MCPcopy Index your code
hub / github.com/coder/coder / TestStatsReporter

Function TestStatsReporter

agent/stats_internal_test.go:19–134  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

17)
18
19func TestStatsReporter(t *testing.T) {
20 t.Parallel()
21 ctx := testutil.Context(t, testutil.WaitShort)
22 logger := testutil.Logger(t)
23 fSource := newFakeNetworkStatsSource(ctx, t)
24 fCollector := newFakeCollector(t)
25 fDest := newFakeStatsDest()
26 uut := newStatsReporter(logger, fSource, fCollector)
27
28 loopErr := make(chan error, 1)
29 loopCtx, loopCancel := context.WithCancel(ctx)
30 go func() {
31 err := uut.reportLoop(loopCtx, fDest)
32 loopErr <- err
33 }()
34
35 // initial request to get duration
36 req := testutil.TryReceive(ctx, t, fDest.reqs)
37 require.NotNil(t, req)
38 require.Nil(t, req.Stats)
39 interval := time.Second * 34
40 testutil.RequireSend(ctx, t, fDest.resps, &proto.UpdateStatsResponse{ReportInterval: durationpb.New(interval)})
41
42 // call to source to set the callback and interval
43 gotInterval := testutil.TryReceive(ctx, t, fSource.period)
44 require.Equal(t, interval, gotInterval)
45
46 // callback returning netstats
47 netStats := map[netlogtype.Connection]netlogtype.Counts{
48 {
49 Proto: ipproto.TCP,
50 Src: netip.MustParseAddrPort("192.168.1.33:4887"),
51 Dst: netip.MustParseAddrPort("192.168.2.99:9999"),
52 }: {
53 TxPackets: 22,
54 TxBytes: 23,
55 RxPackets: 24,
56 RxBytes: 25,
57 },
58 }
59 fSource.callback(time.Now(), time.Now(), netStats, nil)
60
61 // collector called to complete the stats
62 gotNetStats := testutil.TryReceive(ctx, t, fCollector.calls)
63 require.Equal(t, netStats, gotNetStats)
64
65 // while we are collecting the stats, send in two new netStats to simulate
66 // what happens if we don't keep up. The stats should be accumulated.
67 netStats0 := map[netlogtype.Connection]netlogtype.Counts{
68 {
69 Proto: ipproto.TCP,
70 Src: netip.MustParseAddrPort("192.168.1.33:4887"),
71 Dst: netip.MustParseAddrPort("192.168.2.99:9999"),
72 }: {
73 TxPackets: 10,
74 TxBytes: 10,
75 RxPackets: 10,
76 RxBytes: 10,

Callers

nothing calls this directly

Calls 12

ContextFunction · 0.92
LoggerFunction · 0.92
TryReceiveFunction · 0.92
RequireSendFunction · 0.92
newFakeCollectorFunction · 0.85
newFakeStatsDestFunction · 0.85
newStatsReporterFunction · 0.85
reportLoopMethod · 0.80
callbackMethod · 0.80
NewMethod · 0.65
EqualMethod · 0.45

Tested by

no test coverage detected