Skip to content

Commit 17793fc

Browse files
committed
Add test that flushed query param is processed even with empty data
1 parent 36b882d commit 17793fc

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

apm-lambda-extension/extension/http_server_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,3 +258,54 @@ func Test_handleIntakeV2EventsNoQueryParam(t *testing.T) {
258258
<-dataChannel
259259
assert.Equal(t, 202, resp.StatusCode)
260260
}
261+
262+
func Test_handleIntakeV2EventsQueryParamEmptyData(t *testing.T) {
263+
body := []byte(``)
264+
265+
AgentDoneSignal = make(chan struct{})
266+
267+
// Create apm server and handler
268+
apmServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
269+
}))
270+
defer apmServer.Close()
271+
272+
// Create extension config and start the server
273+
dataChannel := make(chan AgentData, 100)
274+
config := extensionConfig{
275+
apmServerUrl: apmServer.URL,
276+
dataReceiverServerPort: ":1234",
277+
dataReceiverTimeoutSeconds: 15,
278+
}
279+
280+
StartHttpServer(dataChannel, &config)
281+
defer agentDataServer.Close()
282+
283+
hosts, _ := net.LookupHost("localhost")
284+
url := "http://" + hosts[0] + ":1234/intake/v2/events?flushed=true"
285+
286+
// Create a request to send to the extension
287+
req, err := http.NewRequest("POST", url, bytes.NewReader(body))
288+
if err != nil {
289+
t.Logf("Could not create request")
290+
}
291+
292+
// Send the request to the extension
293+
client := &http.Client{}
294+
go func() {
295+
_, err := client.Do(req)
296+
if err != nil {
297+
t.Logf("Error fetching %s, [%v]", agentDataServer.Addr, err)
298+
t.Fail()
299+
}
300+
}()
301+
302+
timer := time.NewTimer(1 * time.Second)
303+
defer timer.Stop()
304+
305+
select {
306+
case <-AgentDoneSignal:
307+
case <-timer.C:
308+
t.Log("Timed out waiting for server to send FuncDone signal")
309+
t.Fail()
310+
}
311+
}

apm-lambda-extension/extension/route_handlers.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,14 @@ func handleIntakeV2Events(agentDataChan chan AgentData) func(w http.ResponseWrit
9393
return
9494
}
9595

96-
agentData := AgentData{
97-
Data: rawBytes,
98-
ContentEncoding: r.Header.Get("Content-Encoding"),
96+
if len(rawBytes) > 0 {
97+
agentData := AgentData{
98+
Data: rawBytes,
99+
ContentEncoding: r.Header.Get("Content-Encoding"),
100+
}
101+
log.Println("Adding agent data to buffer to be sent to apm server")
102+
agentDataChan <- agentData
99103
}
100-
log.Println("Adding agent data to buffer to be sent to apm server")
101-
agentDataChan <- agentData
102104

103105
if len(r.URL.Query()["flushed"]) > 0 && r.URL.Query()["flushed"][0] == "true" {
104106
AgentDoneSignal <- struct{}{}

0 commit comments

Comments
 (0)