Initialization order of the metrics.Default global must be done before other packages may start to use it.
(t *testing.T)
| 34 | |
| 35 | // Initialization order of the metrics.Default global must be done before other packages may start to use it. |
| 36 | func TestInitOrderAndDefault(t *testing.T) { |
| 37 | const ( |
| 38 | ringMetricsUpdatePeriod = time.Millisecond |
| 39 | testTimeout = 5 * time.Second |
| 40 | ) |
| 41 | |
| 42 | port := mustAvailablePort(t) |
| 43 | supportPort := mustAvailablePort(t) |
| 44 | redisPort := mustAvailablePort(t) |
| 45 | sig := make(chan os.Signal, 1) |
| 46 | done := make(chan struct{}) |
| 47 | go func() { |
| 48 | options := Options{ |
| 49 | Address: fmt.Sprintf(":%d", port), |
| 50 | SupportListener: fmt.Sprintf(":%d", supportPort), |
| 51 | EnableRuntimeMetrics: true, |
| 52 | EnableSwarm: true, |
| 53 | SwarmRedisURLs: []string{fmt.Sprintf("localhost:%d", redisPort)}, |
| 54 | EnableRatelimiters: true, |
| 55 | SwarmRedisConnMetricsInterval: ringMetricsUpdatePeriod, |
| 56 | PassiveHealthCheck: map[string]string{ |
| 57 | "period": "1m", |
| 58 | "min-requests": "10", |
| 59 | "max-drop-probability": "0.9", |
| 60 | "min-drop-probability": "0.05", |
| 61 | }, |
| 62 | } |
| 63 | |
| 64 | tornDown := make(chan struct{}) |
| 65 | if err := run(options, sig, tornDown); err != nil { |
| 66 | t.Error(err) |
| 67 | } |
| 68 | |
| 69 | <-tornDown |
| 70 | close(done) |
| 71 | }() |
| 72 | |
| 73 | to := time.After(testTimeout) |
| 74 | func() { |
| 75 | for { |
| 76 | rsp, err := http.Get(fmt.Sprintf("http://localhost:%d/metrics/swarm", supportPort)) |
| 77 | if err != nil { |
| 78 | t.Log("error making request", err) |
| 79 | } else { |
| 80 | rsp.Body.Close() |
| 81 | if rsp.StatusCode == http.StatusOK { |
| 82 | return |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | select { |
| 87 | case <-time.After(ringMetricsUpdatePeriod): |
| 88 | case <-to: |
| 89 | t.Error("test timeout") |
| 90 | return |
| 91 | } |
| 92 | } |
| 93 | }() |