Skip to content

Commit

Permalink
Bump go-sdk-events to 3.4.0 and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
keelerm84 committed Jul 25, 2024
1 parent 2ffccd5 commit cd1834a
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 28 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ require (
github.com/launchdarkly/go-configtypes v1.2.0
github.com/launchdarkly/go-jsonstream/v3 v3.0.0
github.com/launchdarkly/go-sdk-common/v3 v3.1.0
github.com/launchdarkly/go-sdk-events/v3 v3.2.0
github.com/launchdarkly/go-sdk-events/v3 v3.4.0
github.com/launchdarkly/go-server-sdk-consul/v3 v3.0.0
github.com/launchdarkly/go-server-sdk-dynamodb/v4 v4.0.0
github.com/launchdarkly/go-server-sdk-evaluation/v3 v3.0.0
Expand Down
43 changes: 35 additions & 8 deletions internal/events/event-relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/launchdarkly/ld-relay/v8/internal/credential"
"github.com/launchdarkly/ld-relay/v8/internal/util"

"github.com/launchdarkly/ld-relay/v8/config"
"github.com/launchdarkly/ld-relay/v8/internal/basictypes"
Expand Down Expand Up @@ -178,7 +179,10 @@ func TestVerbatimEventHandlers(t *testing.T) {
assert.Equal(t, e.analyticsPath, r.Request.URL.Path)
assert.Equal(t, e.authKey, r.Request.Header.Get("Authorization"))
assert.Equal(t, strconv.Itoa(CurrentEventsSchemaVersion), r.Request.Header.Get(EventSchemaHeader))
assert.Equal(t, eventPayloadForVerbatimOnly, string(r.Body))

uncompressed, err := util.DecompressGzipData(r.Body)
require.NoError(t, err)
assert.Equal(t, eventPayloadForVerbatimOnly, string(uncompressed))
})
})
}
Expand Down Expand Up @@ -208,16 +212,33 @@ func TestVerbatimEventHandlers(t *testing.T) {
helpers.RequireValue(t, p.requestsCh, time.Second),
helpers.RequireValue(t, p.requestsCh, time.Second),
}
sort.Slice(received, func(i, j int) bool { return string(received[i].Body) < string(received[j].Body) })

type receivedPayload struct {
Request *http.Request
UncompressedBody string
}

receivedPayloads := make([]receivedPayload, 0, len(received))

for _, r := range received {
uncompressed, err := util.DecompressGzipData([]byte(r.Body))
require.NoError(t, err)
receivedPayloads = append(receivedPayloads, receivedPayload{Request: r.Request, UncompressedBody: string(uncompressed)})
}

sort.Slice(receivedPayloads, func(i, j int) bool {
return receivedPayloads[i].UncompressedBody < receivedPayloads[j].UncompressedBody
})

for _, r := range receivedPayloads {
assert.Equal(t, "POST", r.Request.Method)
assert.Equal(t, e.analyticsPath, r.Request.URL.Path)
assert.Equal(t, e.authKey, r.Request.Header.Get("Authorization"))
}
assert.Equal(t, "3", received[0].Request.Header.Get(EventSchemaHeader))
assert.Equal(t, `["fake-event-v3-1","fake-event-v3-2","fake-event-v3-3"]`, string(received[0].Body))
assert.Equal(t, "4", received[1].Request.Header.Get(EventSchemaHeader))
assert.Equal(t, `["fake-event-v4-1","fake-event-v4-2"]`, string(received[1].Body))
assert.Equal(t, "3", receivedPayloads[0].Request.Header.Get(EventSchemaHeader))
assert.Equal(t, `["fake-event-v3-1","fake-event-v3-2","fake-event-v3-3"]`, receivedPayloads[0].UncompressedBody)
assert.Equal(t, "4", receivedPayloads[1].Request.Header.Get(EventSchemaHeader))
assert.Equal(t, `["fake-event-v4-1","fake-event-v4-2"]`, receivedPayloads[1].UncompressedBody)
})
})
}
Expand Down Expand Up @@ -246,7 +267,10 @@ func TestSummarizingEventHandlers(t *testing.T) {
assert.Equal(t, e.analyticsPath, r.Request.URL.Path)
assert.Equal(t, e.authKey, r.Request.Header.Get("Authorization"))
assert.Equal(t, strconv.Itoa(CurrentEventsSchemaVersion), r.Request.Header.Get(EventSchemaHeader))
m.In(t).Assert(r.Body, m.JSONStrEqual(summarizeEventsParams.expectedEventsJSON))

uncompressed, err := util.DecompressGzipData(r.Body)
require.NoError(t, err)
m.In(t).Assert(uncompressed, m.JSONStrEqual(summarizeEventsParams.expectedEventsJSON))
})
})
}
Expand All @@ -270,7 +294,10 @@ func TestDiagnosticEventForwarding(t *testing.T) {
assert.Equal(t, e.diagnosticPath, r.Request.URL.Path)
assert.Equal(t, "fake-auth", r.Request.Header.Get("Authorization"))
assert.Equal(t, "fake-user-agent", r.Request.Header.Get("User-Agent"))
assert.Equal(t, eventPayloadForVerbatimOnly, string(r.Body))

uncompressed, err := util.DecompressGzipData([]byte(r.Body))
require.NoError(t, err)
assert.Equal(t, eventPayloadForVerbatimOnly, string(uncompressed))
})
})
}
Expand Down
47 changes: 38 additions & 9 deletions internal/events/event_publisher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/launchdarkly/ld-relay/v8/config"
"github.com/launchdarkly/ld-relay/v8/internal/httpconfig"
"github.com/launchdarkly/ld-relay/v8/internal/util"

"github.com/launchdarkly/go-sdk-common/v3/ldlog"
"github.com/launchdarkly/go-sdk-common/v3/ldlogtest"
Expand Down Expand Up @@ -45,7 +46,10 @@ func TestHTTPEventPublisherSimple(t *testing.T) {
assert.Equal(t, "/bulk", r.Request.URL.Path)
assert.Equal(t, string(testSDKKey), r.Request.Header.Get("Authorization"))
assert.Equal(t, strconv.Itoa(CurrentEventsSchemaVersion), r.Request.Header.Get(EventSchemaHeader))
m.In(t).Assert(r.Body, m.JSONStrEqual(`["hello", "hello again"]`))

uncompressed, err := util.DecompressGzipData([]byte(r.Body))
assert.NoError(t, err)
m.In(t).Assert(uncompressed, m.JSONStrEqual(`["hello", "hello again"]`))
})
}

Expand Down Expand Up @@ -77,19 +81,28 @@ func TestHTTPEventPublisherMultiQueuesWithMetadata(t *testing.T) {
assert.Equal(t, string(testSDKKey), r0.Request.Header.Get("Authorization"))
assert.Equal(t, "3", r0.Request.Header.Get(EventSchemaHeader))
assert.Equal(t, "a", r0.Request.Header.Get(TagsHeader))
m.In(t).Assert(r0.Body, m.JSONStrEqual(`["also this"]`))

uncompressed0, err := util.DecompressGzipData(r0.Body)
assert.NoError(t, err)
m.In(t).Assert(uncompressed0, m.JSONStrEqual(`["also this"]`))

assert.Equal(t, "/bulk", received[0].Request.URL.Path)
assert.Equal(t, string(testSDKKey), r1.Request.Header.Get("Authorization"))
assert.Equal(t, strconv.Itoa(CurrentEventsSchemaVersion), r1.Request.Header.Get(EventSchemaHeader))
assert.Equal(t, "a", r1.Request.Header.Get(TagsHeader))
m.In(t).Assert(r1.Body, m.JSONStrEqual(`["hello", "hello again"]`))

uncompressed1, err := util.DecompressGzipData(r1.Body)
assert.NoError(t, err)
m.In(t).Assert(uncompressed1, m.JSONStrEqual(`["hello", "hello again"]`))

assert.Equal(t, "/bulk", r2.Request.URL.Path)
assert.Equal(t, string(testSDKKey), r2.Request.Header.Get("Authorization"))
assert.Equal(t, strconv.Itoa(CurrentEventsSchemaVersion), r2.Request.Header.Get(EventSchemaHeader))
assert.Equal(t, "b", r2.Request.Header.Get(TagsHeader))
m.In(t).Assert(r2.Body, m.JSONStrEqual(`["ok", "thanks"]`))

uncompressed2, err := util.DecompressGzipData(r2.Body)
assert.NoError(t, err)
m.In(t).Assert(uncompressed2, m.JSONStrEqual(`["ok", "thanks"]`))
})
}

Expand All @@ -107,7 +120,10 @@ func TestHTTPEventPublisherOptionURIPath(t *testing.T) {
assert.Equal(t, "/special-path", r.Request.URL.Path)
assert.Equal(t, string(testSDKKey), r.Request.Header.Get("Authorization"))
assert.Equal(t, strconv.Itoa(CurrentEventsSchemaVersion), r.Request.Header.Get(EventSchemaHeader))
m.In(t).Assert(r.Body, m.JSONStrEqual(`["hello"]`))

uncompressed, err := util.DecompressGzipData(r.Body)
assert.NoError(t, err)
m.In(t).Assert(uncompressed, m.JSONStrEqual(`["hello"]`))
})
}

Expand All @@ -130,7 +146,10 @@ func TestHTTPPublisherAutomaticFlush(t *testing.T) {
publisher.Publish(EventPayloadMetadata{}, json.RawMessage(`"hello"`))
r := helpers.RequireValue(t, requestsCh, time.Second)
assert.Equal(t, "/bulk", r.Request.URL.Path)
m.In(t).Assert(r.Body, m.JSONStrEqual(`["hello"]`))

uncompressed, err := util.DecompressGzipData(r.Body)
assert.NoError(t, err)
m.In(t).Assert(uncompressed, m.JSONStrEqual(`["hello"]`))
assert.Equal(t, strconv.Itoa(CurrentEventsSchemaVersion), r.Request.Header.Get(EventSchemaHeader))
})
}
Expand Down Expand Up @@ -162,7 +181,11 @@ func TestHTTPEventPublisherCapacity(t *testing.T) {
r := helpers.RequireValue(t, requestsCh, time.Second)
assert.Equal(t, "/bulk", r.Request.URL.Path)
assert.Equal(t, strconv.Itoa(CurrentEventsSchemaVersion), r.Request.Header.Get(EventSchemaHeader))
m.In(t).Assert(r.Body, m.JSONStrEqual(`["hello"]`))

uncompressed, err := util.DecompressGzipData(r.Body)
assert.NoError(t, err)

m.In(t).Assert(uncompressed, m.JSONStrEqual(`["hello"]`))
})
}

Expand All @@ -182,10 +205,16 @@ func TestHTTPEventPublisherErrorRetry(t *testing.T) {
timeStart := time.Now()
publisher.Flush()
req1 := helpers.RequireValue(t, requestsCh, time.Second*5)
uncompressed1, err := util.DecompressGzipData(req1.Body)
assert.NoError(t, err)

req2 := helpers.RequireValue(t, requestsCh, time.Second*5)
uncompressed2, err := util.DecompressGzipData(req2.Body)
assert.NoError(t, err)

elapsed := time.Since(timeStart)
assert.Equal(t, []byte(`["hello"]`), req1.Body)
assert.Equal(t, req1.Body, req2.Body)
assert.Equal(t, []byte(`["hello"]`), uncompressed1)
assert.Equal(t, uncompressed1, uncompressed2)
assert.GreaterOrEqual(t, int64(elapsed), int64(time.Second))

// There were two failures, so it should not have retried again after that (should not reach successHandler)
Expand Down
32 changes: 26 additions & 6 deletions internal/events/summarizing-relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/launchdarkly/ld-relay/v8/config"
"github.com/launchdarkly/ld-relay/v8/internal/basictypes"
st "github.com/launchdarkly/ld-relay/v8/internal/sharedtest"
"github.com/launchdarkly/ld-relay/v8/internal/util"

ldevents "github.com/launchdarkly/go-sdk-events/v3"
helpers "github.com/launchdarkly/go-test-helpers/v3"
Expand Down Expand Up @@ -56,7 +57,10 @@ func TestSummarizeEvents(t *testing.T) {
p.dispatcher.flush()

payload := expectSummarizedPayload(t, p.requestsCh)
m.In(t).Assert(payload, m.JSONStrEqual(ep.expectedEventsJSON))

uncompressed, err := util.DecompressGzipData([]byte(payload))
require.NoError(t, err)
m.In(t).Assert(uncompressed, m.JSONStrEqual(ep.expectedEventsJSON))
})
})
}
Expand Down Expand Up @@ -97,12 +101,18 @@ func TestSummarizingRelayProcessesEventsSeparatelyForDifferentTags(t *testing.T)
assert.Equal(t, "tags1", request1.Request.Header.Get(TagsHeader))
assert.Equal(t, "tags2", request2.Request.Header.Get(TagsHeader))

m.In(t).Assert(json.RawMessage(request1.Body), m.JSONArray().Should(m.ItemsInAnyOrder(
decompressedBody1, err := util.DecompressGzipData(request1.Body)
assert.NoError(t, err)

decompressedBody2, err := util.DecompressGzipData(request2.Body)
assert.NoError(t, err)

m.In(t).Assert(json.RawMessage(decompressedBody1), m.JSONArray().Should(m.ItemsInAnyOrder(
m.MapIncluding(m.KV("kind", m.Equal("index"))),
m.MapIncluding(m.KV("kind", m.Equal("custom")), m.KV("key", m.Equal("eventkey1a"))),
m.MapIncluding(m.KV("kind", m.Equal("custom")), m.KV("key", m.Equal("eventkey1b"))),
)))
m.In(t).Assert(json.RawMessage(request2.Body), m.JSONArray().Should(m.ItemsInAnyOrder(
m.In(t).Assert(json.RawMessage(decompressedBody2), m.JSONArray().Should(m.ItemsInAnyOrder(
m.MapIncluding(m.KV("kind", m.Equal("index"))),
m.MapIncluding(m.KV("kind", m.Equal("custom")), m.KV("key", m.Equal("eventkey2"))),
)))
Expand Down Expand Up @@ -149,11 +159,17 @@ func TestSummarizingRelayPeriodicallyClosesInactiveEventProcessors(t *testing.T)
assert.Equal(t, "tags1", request1a.Request.Header.Get(TagsHeader))
assert.Equal(t, "tags2", request2.Request.Header.Get(TagsHeader))

m.In(t).Assert(json.RawMessage(request1a.Body), m.JSONArray().Should(m.ItemsInAnyOrder(
uncompressedBody1a, err := util.DecompressGzipData(request1a.Body)
assert.NoError(t, err)

uncompressedBody2, err := util.DecompressGzipData(request2.Body)
assert.NoError(t, err)

m.In(t).Assert(json.RawMessage(uncompressedBody1a), m.JSONArray().Should(m.ItemsInAnyOrder(
m.MapIncluding(m.KV("kind", m.Equal("index"))),
m.MapIncluding(m.KV("kind", m.Equal("custom")), m.KV("key", m.Equal("eventkey1a"))),
)))
m.In(t).Assert(json.RawMessage(request2.Body), m.JSONArray().Should(m.ItemsInAnyOrder(
m.In(t).Assert(json.RawMessage(uncompressedBody2), m.JSONArray().Should(m.ItemsInAnyOrder(
m.MapIncluding(m.KV("kind", m.Equal("index"))),
m.MapIncluding(m.KV("kind", m.Equal("custom")), m.KV("key", m.Equal("eventkey2"))),
)))
Expand All @@ -165,8 +181,12 @@ func TestSummarizingRelayPeriodicallyClosesInactiveEventProcessors(t *testing.T)
p.dispatcher.flush()

request1b := expectSummarizedPayloadRequest(t, p.requestsCh)

uncompressedBody1b, err := util.DecompressGzipData(request1b.Body)
assert.NoError(t, err)

assert.Equal(t, "tags1", request1b.Request.Header.Get(TagsHeader))
m.In(t).Assert(json.RawMessage(request1b.Body), m.JSONArray().Should(m.ItemsInAnyOrder(
m.In(t).Assert(json.RawMessage(uncompressedBody1b), m.JSONArray().Should(m.ItemsInAnyOrder(
m.MapIncluding(m.KV("kind", m.Equal("index"))),
m.MapIncluding(m.KV("kind", m.Equal("custom")), m.KV("key", m.Equal("eventkey1b"))),
)))
Expand Down
11 changes: 9 additions & 2 deletions internal/relayenv/env_context_impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

"github.com/launchdarkly/ld-relay/v8/internal/sdkauth"
"github.com/launchdarkly/ld-relay/v8/internal/util"

"github.com/launchdarkly/ld-relay/v8/internal/credential"

Expand Down Expand Up @@ -355,7 +356,10 @@ func TestMetricsAreExportedForEnvironment(t *testing.T) {
select {
case req := <-requestsCh:
mockLog.Loggers.Infof("received metrics events: %s", req.Body)
data := ldvalue.Parse(req.Body)
uncompressed, err := util.DecompressGzipData(req.Body)
require.NoError(t, err)

data := ldvalue.Parse(uncompressed)
event := data.GetByIndex(0)
if !event.IsNull() {
conns := event.GetByKey("connections")
Expand Down Expand Up @@ -456,7 +460,10 @@ func TestEventDispatcherIsCreatedIfSendEventsIsTrueAndNotInOfflineMode(t *testin
// Because the event schema version is >= 3, the event data should be forwarded verbatim with no processing.
eventPost := helpers.RequireValue(t, requestsCh, time.Second)
require.Equal(t, string(st.EnvMain.Config.SDKKey), eventPost.Request.Header.Get("Authorization"))
require.Equal(t, string(body), string(eventPost.Body))

decodedBody, err := util.DecompressGzipData(eventPost.Body)
require.NoError(t, err)
require.Equal(t, string(body), string(decodedBody))
})
}

Expand Down
12 changes: 12 additions & 0 deletions internal/util/util.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package util

import (
"bytes"
"compress/gzip"
"encoding/json"
"fmt"
"io"
"net/url"
)

Expand Down Expand Up @@ -36,3 +39,12 @@ func RedactURL(inputURL string) string {
}
return inputURL
}

func DecompressGzipData(data []byte) ([]byte, error) {
reader, err := gzip.NewReader(bytes.NewReader(data))
if err != nil {
return data, err
}

return io.ReadAll(reader)
}
7 changes: 6 additions & 1 deletion relay/autoconfig_key_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/launchdarkly/ld-relay/v8/internal/envfactory"
"github.com/launchdarkly/ld-relay/v8/internal/util"

"github.com/launchdarkly/ld-relay/v8/internal/sdkauth"

Expand Down Expand Up @@ -73,7 +74,11 @@ func verifyEventSummarizingRelay(t *testing.T, p autoConfTestParams, url string,

gotReq := helpers.RequireValue(t, p.eventRequestsCh, time.Second*5)
assert.Equal(p.t, authKey.GetAuthorizationHeaderValue(), gotReq.Request.Header.Get("Authorization"))
eventsValue := ldvalue.Parse(gotReq.Body)

decompressBody, err := util.DecompressGzipData(gotReq.Body)
assert.NoError(t, err)

eventsValue := ldvalue.Parse(decompressBody)
assert.Equal(p.t, "summary", eventsValue.GetByIndex(eventsValue.Count()-1).GetByKey("kind").StringValue())
}

Expand Down
5 changes: 4 additions & 1 deletion relay/endpoints_events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/launchdarkly/ld-relay/v8/internal/browser"
"github.com/launchdarkly/ld-relay/v8/internal/events"
st "github.com/launchdarkly/ld-relay/v8/internal/sharedtest"
"github.com/launchdarkly/ld-relay/v8/internal/util"

ct "github.com/launchdarkly/go-configtypes"
helpers "github.com/launchdarkly/go-test-helpers/v3"
Expand All @@ -35,7 +36,9 @@ type relayEventsTestParams struct {

func (p relayEventsTestParams) requirePublishedEvent(t *testing.T, data []byte) publishedEvent {
event := helpers.RequireValue(t, p.publishedEvents, time.Second*3)
assert.JSONEq(t, string(data), string(event.data))
uncompressed, err := util.DecompressGzipData(event.data)
assert.NoError(t, err)
assert.JSONEq(t, string(data), string(uncompressed))
return event
}

Expand Down

0 comments on commit cd1834a

Please sign in to comment.