-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bugfix: close inprogress connect subsegments (#102)
* fixes race condition in centralized sampling test * connect subsegment closing more robust * add non-empty subsegment assertion to failing test * update aws_test for travis * adds additional tests
- Loading branch information
1 parent
9b87712
commit 17591b3
Showing
6 changed files
with
202 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package xray | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/credentials" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/lambda" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestClientSuccessfulConnection(t *testing.T) { | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
b := []byte(`{}`) | ||
w.WriteHeader(http.StatusOK) | ||
w.Write(b) | ||
})) | ||
|
||
svc := lambda.New(session.Must(session.NewSession(&aws.Config{ | ||
Endpoint: aws.String(ts.URL), | ||
Region: aws.String("fake-moon-1"), | ||
Credentials: credentials.NewStaticCredentials("akid", "secret", "noop")}))) | ||
|
||
ctx, root := BeginSegment(context.Background(), "Test") | ||
|
||
AWS(svc.Client) | ||
|
||
_, err := svc.ListFunctionsWithContext(ctx, &lambda.ListFunctionsInput{}) | ||
root.Close(nil) | ||
assert.NoError(t, err) | ||
|
||
s, e := TestDaemon.Recv() | ||
assert.NoError(t, e) | ||
|
||
subseg := &Segment{} | ||
assert.NotEmpty(t, s.Subsegments) | ||
assert.NoError(t, json.Unmarshal(s.Subsegments[0], &subseg)) | ||
assert.False(t, subseg.Fault) | ||
assert.NotEmpty(t, subseg.Subsegments) | ||
|
||
attemptSubseg := &Segment{} | ||
for _, sub := range subseg.Subsegments { | ||
tempSeg := &Segment{} | ||
assert.NoError(t, json.Unmarshal(sub, &tempSeg)) | ||
if tempSeg.Name == "attempt" { | ||
attemptSubseg = tempSeg | ||
break | ||
} | ||
} | ||
|
||
assert.Equal(t, "attempt", attemptSubseg.Name) | ||
assert.Zero(t, attemptSubseg.openSegments) | ||
|
||
// Connect subsegment will contain multiple child subsegments. | ||
// The subsegment should fail since the endpoint is not valid, | ||
// and should not be InProgress. | ||
connectSubseg := &Segment{} | ||
assert.NotEmpty(t, attemptSubseg.Subsegments) | ||
assert.NoError(t, json.Unmarshal(attemptSubseg.Subsegments[0], &connectSubseg)) | ||
assert.Equal(t, "connect", connectSubseg.Name) | ||
assert.False(t, connectSubseg.InProgress) | ||
assert.NotZero(t, connectSubseg.EndTime) | ||
assert.NotEmpty(t, connectSubseg.Subsegments) | ||
|
||
// Ensure that the 'connect' subsegments are completed. | ||
for _, sub := range connectSubseg.Subsegments { | ||
tempSeg := &Segment{} | ||
assert.NoError(t, json.Unmarshal(sub, &tempSeg)) | ||
assert.False(t, tempSeg.InProgress) | ||
assert.NotZero(t, tempSeg.EndTime) | ||
} | ||
} | ||
|
||
func TestClientFailedConnection(t *testing.T) { | ||
svc := lambda.New(session.Must(session.NewSession(&aws.Config{ | ||
Region: aws.String("fake-moon-1"), | ||
Credentials: credentials.NewStaticCredentials("akid", "secret", "noop")}))) | ||
|
||
ctx, root := BeginSegment(context.Background(), "Test") | ||
|
||
AWS(svc.Client) | ||
|
||
_, err := svc.ListFunctionsWithContext(ctx, &lambda.ListFunctionsInput{}) | ||
root.Close(nil) | ||
assert.Error(t, err) | ||
|
||
s, e := TestDaemon.Recv() | ||
assert.NoError(t, e) | ||
|
||
subseg := &Segment{} | ||
assert.NotEmpty(t, s.Subsegments) | ||
assert.NoError(t, json.Unmarshal(s.Subsegments[0], &subseg)) | ||
assert.True(t, subseg.Fault) | ||
// Should contain 'marshal' and 'attempt' subsegments only. | ||
assert.Len(t, subseg.Subsegments, 2) | ||
|
||
attemptSubseg := &Segment{} | ||
assert.NoError(t, json.Unmarshal(subseg.Subsegments[1], &attemptSubseg)) | ||
assert.Equal(t, "attempt", attemptSubseg.Name) | ||
assert.Zero(t, attemptSubseg.openSegments) | ||
|
||
// Connect subsegment will contain multiple child subsegments. | ||
// The subsegment should fail since the endpoint is not valid, | ||
// and should not be InProgress. | ||
connectSubseg := &Segment{} | ||
assert.NotEmpty(t, attemptSubseg.Subsegments) | ||
assert.NoError(t, json.Unmarshal(attemptSubseg.Subsegments[0], &connectSubseg)) | ||
assert.Equal(t, "connect", connectSubseg.Name) | ||
assert.False(t, connectSubseg.InProgress) | ||
assert.NotZero(t, connectSubseg.EndTime) | ||
assert.NotEmpty(t, connectSubseg.Subsegments) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters