github.com/database64128/shadowsocks-go@v1.10.2-0.20240315062903-143a773533f1/stats/collector_test.go (about) 1 package stats 2 3 import "testing" 4 5 func collect(t *testing.T, c Collector) { 6 t.Helper() 7 c.CollectTCPSession("Steve", 1024, 2048) 8 c.CollectUDPSessionUplink("Alex", 1, 3072) 9 c.CollectUDPSessionDownlink("Steve", 2, 4096) 10 c.CollectTCPSession("Alex", 5120, 6144) 11 c.CollectUDPSessionDownlink("Alex", 4, 7168) 12 c.CollectUDPSessionUplink("Steve", 8, 8192) 13 c.CollectTCPSession("Steve", 9216, 10240) 14 c.CollectUDPSessionDownlink("Alex", 16, 11264) 15 c.CollectUDPSessionDownlink("Steve", 32, 12288) 16 c.CollectUDPSessionDownlink("Alex", 64, 13312) 17 c.CollectUDPSessionUplink("Steve", 128, 14336) 18 c.CollectUDPSessionUplink("Alex", 256, 15360) 19 c.CollectUDPSessionUplink("Alex", 512, 16384) 20 c.CollectTCPSession("Steve", 17408, 18432) 21 c.CollectUDPSessionDownlink("Alex", 1024, 19456) 22 c.CollectUDPSessionUplink("Alex", 2048, 20480) 23 } 24 25 func collectNoUsername(t *testing.T, c Collector) { 26 t.Helper() 27 c.CollectTCPSession("", 1024, 2048) 28 c.CollectUDPSessionDownlink("", 1, 3072) 29 c.CollectUDPSessionUplink("", 2, 4096) 30 } 31 32 func verify(t *testing.T, s Server) { 33 t.Helper() 34 expectedServerTraffic := Traffic{ 35 DownlinkPackets: 1142, 36 DownlinkBytes: 100352, 37 UplinkPackets: 2953, 38 UplinkBytes: 114688, 39 TCPSessions: 4, 40 UDPSessions: 6, 41 } 42 expectedSteveTraffic := Traffic{ 43 DownlinkPackets: 34, 44 DownlinkBytes: 44032, 45 UplinkPackets: 136, 46 UplinkBytes: 53248, 47 TCPSessions: 3, 48 UDPSessions: 2, 49 } 50 expectedAlexTraffic := Traffic{ 51 DownlinkPackets: 1108, 52 DownlinkBytes: 56320, 53 UplinkPackets: 2817, 54 UplinkBytes: 61440, 55 TCPSessions: 1, 56 UDPSessions: 4, 57 } 58 if s.Traffic != expectedServerTraffic { 59 t.Errorf("expected server traffic %+v, got %+v", expectedServerTraffic, s.Traffic) 60 } 61 if len(s.Users) != 2 { 62 t.Fatalf("expected 2 users, got %d", len(s.Users)) 63 } 64 for _, u := range s.Users { 65 switch u.Name { 66 case "Steve": 67 if u.Traffic != expectedSteveTraffic { 68 t.Errorf("expected Steve traffic %+v, got %+v", expectedSteveTraffic, u.Traffic) 69 } 70 case "Alex": 71 if u.Traffic != expectedAlexTraffic { 72 t.Errorf("expected Alex traffic %+v, got %+v", expectedAlexTraffic, u.Traffic) 73 } 74 default: 75 t.Errorf("unexpected user %s", u.Name) 76 } 77 } 78 } 79 80 func verifyNoUsername(t *testing.T, s Server) { 81 t.Helper() 82 expectedServerTraffic := Traffic{ 83 DownlinkPackets: 1, 84 DownlinkBytes: 4096, 85 UplinkPackets: 2, 86 UplinkBytes: 6144, 87 TCPSessions: 1, 88 UDPSessions: 1, 89 } 90 if s.Traffic != expectedServerTraffic { 91 t.Errorf("expected server traffic %+v, got %+v", expectedServerTraffic, s.Traffic) 92 } 93 if len(s.Users) != 0 { 94 t.Errorf("expected zero users, got %d", len(s.Users)) 95 } 96 } 97 98 func verifyEmpty(t *testing.T, s Server) { 99 t.Helper() 100 var zero Traffic 101 if s.Traffic != zero { 102 t.Errorf("expected zero traffic, got %+v", s.Traffic) 103 } 104 for _, u := range s.Users { 105 if u.Traffic != zero { 106 t.Errorf("expected zero traffic for user %s, got %+v", u.Name, u.Traffic) 107 } 108 } 109 } 110 111 func TestServerCollector(t *testing.T) { 112 c := Config{Enabled: true}.Collector() 113 collectNoUsername(t, c) 114 verifyNoUsername(t, c.Snapshot()) 115 verifyNoUsername(t, c.SnapshotAndReset()) 116 verifyEmpty(t, c.Snapshot()) 117 collect(t, c) 118 verify(t, c.Snapshot()) 119 verify(t, c.SnapshotAndReset()) 120 verifyEmpty(t, c.Snapshot()) 121 } 122 123 func TestNoopCollector(t *testing.T) { 124 c := Config{}.Collector() 125 collectNoUsername(t, c) 126 verifyEmpty(t, c.Snapshot()) 127 verifyEmpty(t, c.SnapshotAndReset()) 128 verifyEmpty(t, c.Snapshot()) 129 collect(t, c) 130 verifyEmpty(t, c.Snapshot()) 131 verifyEmpty(t, c.SnapshotAndReset()) 132 verifyEmpty(t, c.Snapshot()) 133 }