Skip to content

Commit

Permalink
more work around metric unit tests flakes
Browse files Browse the repository at this point in the history
  • Loading branch information
gabemontero committed Apr 7, 2020
1 parent 98ff88c commit 4f71470
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions pkg/metrics/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,28 @@ import (
"crypto/tls"
"crypto/x509"
"encoding/pem"
"fmt"
"io"
"io/ioutil"
"math/big"
mr "math/rand"
"net/http"
"os"
"sync"
"testing"
"time"

io_prometheus_client "github.com/prometheus/client_model/go"
"github.com/prometheus/common/expfmt"
)

var testMetricLock sync.Mutex
var port int

func init() {
testMetricLock = sync.Mutex{}
mr.Seed(time.Now().UnixNano())
port = mr.Intn(4000) + 6000
}

func TestMain(m *testing.M) {
Expand Down Expand Up @@ -85,10 +91,10 @@ func TestRun(t *testing.T) {
defer testMetricLock.Unlock()
ch := make(chan struct{})
defer close(ch)
srv := BuildServer(6789)
srv := BuildServer(port)
go RunServer(srv, ch)

resp, err := http.Get("https://localhost:6789/metrics")
resp, err := http.Get(fmt.Sprintf("https://localhost:%d/metrics", port))
if err != nil {
t.Fatalf("error requesting metrics server: %v", err)
}
Expand All @@ -103,7 +109,7 @@ func TestBinaryMetrics(t *testing.T) {
defer testMetricLock.Unlock()
ch := make(chan struct{})
defer close(ch)
srv := BuildServer(6789)
srv := BuildServer(port)
go RunServer(srv, ch)
for _, t1 := range []struct {
method func(bool)
Expand Down Expand Up @@ -158,7 +164,7 @@ func TestBinaryMetrics(t *testing.T) {
t1.method(tt.val)
}

resp, err := http.Get("https://localhost:6789/metrics")
resp, err := http.Get(fmt.Sprintf("https://localhost:%d/metrics", port))
if err != nil {
t.Fatalf("error requesting metrics server: %v", err)
}
Expand Down

0 comments on commit 4f71470

Please sign in to comment.