cloudflare/cloudflared
Publicmirrored from https://github.com/cloudflare/cloudflaredAvailable
origin/metrics_test.go
121lines · modecode
| 1 | package origin |
| 2 | |
| 3 | import ( |
| 4 | "strconv" |
| 5 | "sync" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/stretchr/testify/assert" |
| 9 | ) |
| 10 | |
| 11 | // can only be called once |
| 12 | var m = NewTunnelMetrics() |
| 13 | |
| 14 | func TestConcurrentRequestsSingleTunnel(t *testing.T) { |
| 15 | routines := 20 |
| 16 | var wg sync.WaitGroup |
| 17 | wg.Add(routines) |
| 18 | for i := 0; i < routines; i++ { |
| 19 | go func() { |
| 20 | m.incrementRequests("0") |
| 21 | wg.Done() |
| 22 | }() |
| 23 | } |
| 24 | wg.Wait() |
| 25 | assert.Len(t, m.concurrentRequests, 1) |
| 26 | assert.Equal(t, uint64(routines), m.concurrentRequests["0"]) |
| 27 | assert.Len(t, m.maxConcurrentRequests, 1) |
| 28 | assert.Equal(t, uint64(routines), m.maxConcurrentRequests["0"]) |
| 29 | |
| 30 | wg.Add(routines / 2) |
| 31 | for i := 0; i < routines/2; i++ { |
| 32 | go func() { |
| 33 | m.decrementConcurrentRequests("0") |
| 34 | wg.Done() |
| 35 | }() |
| 36 | } |
| 37 | wg.Wait() |
| 38 | assert.Equal(t, uint64(routines-routines/2), m.concurrentRequests["0"]) |
| 39 | assert.Equal(t, uint64(routines), m.maxConcurrentRequests["0"]) |
| 40 | } |
| 41 | |
| 42 | func TestConcurrentRequestsMultiTunnel(t *testing.T) { |
| 43 | m.concurrentRequests = make(map[string]uint64) |
| 44 | m.maxConcurrentRequests = make(map[string]uint64) |
| 45 | tunnels := 20 |
| 46 | var wg sync.WaitGroup |
| 47 | wg.Add(tunnels) |
| 48 | for i := 0; i < tunnels; i++ { |
| 49 | go func(i int) { |
| 50 | // if we have j < i, then tunnel 0 won't have a chance to call incrementRequests |
| 51 | for j := 0; j < i+1; j++ { |
| 52 | id := strconv.Itoa(i) |
| 53 | m.incrementRequests(id) |
| 54 | } |
| 55 | wg.Done() |
| 56 | }(i) |
| 57 | } |
| 58 | wg.Wait() |
| 59 | |
| 60 | assert.Len(t, m.concurrentRequests, tunnels) |
| 61 | assert.Len(t, m.maxConcurrentRequests, tunnels) |
| 62 | for i := 0; i < tunnels; i++ { |
| 63 | id := strconv.Itoa(i) |
| 64 | assert.Equal(t, uint64(i+1), m.concurrentRequests[id]) |
| 65 | assert.Equal(t, uint64(i+1), m.maxConcurrentRequests[id]) |
| 66 | } |
| 67 | |
| 68 | wg.Add(tunnels) |
| 69 | for i := 0; i < tunnels; i++ { |
| 70 | go func(i int) { |
| 71 | for j := 0; j < i+1; j++ { |
| 72 | id := strconv.Itoa(i) |
| 73 | m.decrementConcurrentRequests(id) |
| 74 | } |
| 75 | wg.Done() |
| 76 | }(i) |
| 77 | } |
| 78 | wg.Wait() |
| 79 | |
| 80 | assert.Len(t, m.concurrentRequests, tunnels) |
| 81 | assert.Len(t, m.maxConcurrentRequests, tunnels) |
| 82 | for i := 0; i < tunnels; i++ { |
| 83 | id := strconv.Itoa(i) |
| 84 | assert.Equal(t, uint64(0), m.concurrentRequests[id]) |
| 85 | assert.Equal(t, uint64(i+1), m.maxConcurrentRequests[id]) |
| 86 | } |
| 87 | |
| 88 | } |
| 89 | |
| 90 | func TestRegisterServerLocation(t *testing.T) { |
| 91 | tunnels := 20 |
| 92 | var wg sync.WaitGroup |
| 93 | wg.Add(tunnels) |
| 94 | for i := 0; i < tunnels; i++ { |
| 95 | go func(i int) { |
| 96 | id := strconv.Itoa(i) |
| 97 | m.registerServerLocation(id, "LHR") |
| 98 | wg.Done() |
| 99 | }(i) |
| 100 | } |
| 101 | wg.Wait() |
| 102 | for i := 0; i < tunnels; i++ { |
| 103 | id := strconv.Itoa(i) |
| 104 | assert.Equal(t, "LHR", m.oldServerLocations[id]) |
| 105 | } |
| 106 | |
| 107 | wg.Add(tunnels) |
| 108 | for i := 0; i < tunnels; i++ { |
| 109 | go func(i int) { |
| 110 | id := strconv.Itoa(i) |
| 111 | m.registerServerLocation(id, "AUS") |
| 112 | wg.Done() |
| 113 | }(i) |
| 114 | } |
| 115 | wg.Wait() |
| 116 | for i := 0; i < tunnels; i++ { |
| 117 | id := strconv.Itoa(i) |
| 118 | assert.Equal(t, "AUS", m.oldServerLocations[id]) |
| 119 | } |
| 120 | |
| 121 | } |
| 122 | |