(t *testing.T)
| 17 | ) |
| 18 | |
| 19 | func 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, |
nothing calls this directly
no test coverage detected