Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

maint: attempt to fix flaky integration tests #945

Merged
merged 2 commits into from
Dec 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 43 additions & 28 deletions app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/facebookgo/startstop"
"github.com/klauspost/compress/zstd"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/alexcesaro/statsd.v2"

"github.com/honeycombio/libhoney-go"
Expand Down Expand Up @@ -226,8 +227,8 @@ func TestAppIntegration(t *testing.T) {
t.Parallel()
port := 10500

var out bytes.Buffer
_, graph := newStartedApp(t, &transmission.WriterSender{W: &out}, port, nil, false)
sender := &transmission.MockSender{}
app, graph := newStartedApp(t, sender, port, nil, false)

// Send a root span, it should be sent in short order.
req := httptest.NewRequest(
Expand All @@ -243,22 +244,27 @@ func TestAppIntegration(t *testing.T) {
assert.Equal(t, http.StatusOK, resp.StatusCode)
resp.Body.Close()

time.Sleep(2 * app.Config.GetSendTickerValue())

events := sender.Events()
require.Len(t, events, 1)

assert.Equal(t, "dataset", events[0].Dataset)
assert.Equal(t, "bar", events[0].Data["foo"])
assert.Equal(t, "1", events[0].Data["trace.trace_id"])
assert.Equal(t, uint(1), events[0].Data["meta.refinery.original_sample_rate"])

err = startstop.Stop(graph.Objects(), nil)
assert.NoError(t, err)

assert.Eventually(t, func() bool {
return out.Len() > 62
}, 5*time.Second, 2*time.Millisecond)
assert.Equal(t, `{"data":{"foo":"bar","meta.refinery.original_sample_rate":1,"trace.trace_id":"1"},"dataset":"dataset"}`+"\n", out.String())
}

func TestAppIntegrationWithNonLegacyKey(t *testing.T) {
// Parallel integration tests need different ports!
t.Parallel()
port := 10600

var out bytes.Buffer
a, graph := newStartedApp(t, &transmission.WriterSender{W: &out}, port, nil, false)
sender := &transmission.MockSender{}
a, graph := newStartedApp(t, sender, port, nil, false)
a.IncomingRouter.SetEnvironmentCache(time.Second, func(s string) (string, error) { return "test", nil })
a.PeerRouter.SetEnvironmentCache(time.Second, func(s string) (string, error) { return "test", nil })

Expand All @@ -276,23 +282,27 @@ func TestAppIntegrationWithNonLegacyKey(t *testing.T) {
assert.Equal(t, http.StatusOK, resp.StatusCode)
resp.Body.Close()

// Wait for span to be sent.
time.Sleep(2 * a.Config.GetSendTickerValue())
events := sender.Events()
require.Len(t, events, 1)

assert.Equal(t, "dataset", events[0].Dataset)
assert.Equal(t, "bar", events[0].Data["foo"])
assert.Equal(t, "1", events[0].Data["trace.trace_id"])
assert.Equal(t, uint(1), events[0].Data["meta.refinery.original_sample_rate"])

err = startstop.Stop(graph.Objects(), nil)
assert.NoError(t, err)

// Wait for span to be sent.
assert.Eventually(t, func() bool {
return out.Len() > 62
}, 5*time.Second, 2*time.Millisecond)
assert.Equal(t, `{"data":{"foo":"bar","meta.refinery.original_sample_rate":1,"trace.trace_id":"1"},"dataset":"dataset"}`+"\n", out.String())
}

func TestAppIntegrationWithUnauthorizedKey(t *testing.T) {
// Parallel integration tests need different ports!
t.Parallel()
port := 10700

var out bytes.Buffer
a, graph := newStartedApp(t, &transmission.WriterSender{W: &out}, port, nil, false)
sender := &transmission.MockSender{}
a, graph := newStartedApp(t, sender, port, nil, false)
a.IncomingRouter.SetEnvironmentCache(time.Second, func(s string) (string, error) { return "test", nil })
a.PeerRouter.SetEnvironmentCache(time.Second, func(s string) (string, error) { return "test", nil })

Expand Down Expand Up @@ -416,15 +426,15 @@ func TestPeerRouting(t *testing.T) {

func TestHostMetadataSpanAdditions(t *testing.T) {
t.Parallel()
port := 14000

var out bytes.Buffer
_, graph := newStartedApp(t, &transmission.WriterSender{W: &out}, 14000, nil, true)
hostname, _ := os.Hostname()
sender := &transmission.MockSender{}
app, graph := newStartedApp(t, sender, port, nil, true)

// Send a root span, it should be sent in short order.
req := httptest.NewRequest(
"POST",
"http://localhost:14000/1/batch/dataset",
fmt.Sprintf("http://localhost:%d/1/batch/dataset", port),
strings.NewReader(`[{"data":{"foo":"bar","trace.trace_id":"1"}}]`),
)
req.Header.Set("X-Honeycomb-Team", legacyAPIKey)
Expand All @@ -435,15 +445,20 @@ func TestHostMetadataSpanAdditions(t *testing.T) {
assert.Equal(t, http.StatusOK, resp.StatusCode)
resp.Body.Close()

err = startstop.Stop(graph.Objects(), nil)
assert.NoError(t, err)
time.Sleep(2 * app.Config.GetSendTickerValue())

assert.Eventually(t, func() bool {
return out.Len() > 62
}, 5*time.Second, 2*time.Millisecond)
events := sender.Events()
require.Len(t, events, 1)

assert.Equal(t, "dataset", events[0].Dataset)
assert.Equal(t, "bar", events[0].Data["foo"])
assert.Equal(t, "1", events[0].Data["trace.trace_id"])
assert.Equal(t, uint(1), events[0].Data["meta.refinery.original_sample_rate"])
hostname, _ := os.Hostname()
assert.Equal(t, hostname, events[0].Data["meta.refinery.local_hostname"])

expectedSpan := `{"data":{"foo":"bar","meta.refinery.local_hostname":"%s","meta.refinery.original_sample_rate":1,"trace.trace_id":"1"},"dataset":"dataset"}` + "\n"
assert.Equal(t, fmt.Sprintf(expectedSpan, hostname), out.String())
err = startstop.Stop(graph.Objects(), nil)
assert.NoError(t, err)
}

func TestEventsEndpoint(t *testing.T) {
Expand Down