Skip to content

Commit

Permalink
create new TestDaemon for each test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
shogo82148 committed Jan 19, 2020
1 parent dc12010 commit f1811db
Show file tree
Hide file tree
Showing 12 changed files with 1,071 additions and 842 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module github.com/aws/aws-xray-sdk-go

go 1.11

require (
github.com/DATA-DOG/go-sqlmock v1.2.0
github.com/aws/aws-sdk-go v1.17.12
Expand Down
95 changes: 56 additions & 39 deletions xray/aws_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
// Copyright 2017-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

package xray

import (
Expand All @@ -20,7 +28,7 @@ func TestAWS(t *testing.T) {
// Runs a suite of tests against two different methods of registering
// handlers on an AWS client.

type test func(*testing.T, *lambda.Lambda)
type test func(context.Context, *TestDaemon, *testing.T, *lambda.Lambda)
tests := []struct {
name string
test test
Expand Down Expand Up @@ -67,23 +75,32 @@ func TestAWS(t *testing.T) {

// Run all combinations of constructors + tests.
for _, cons := range constructors {
cons := cons
t.Run(cons.name, func(t *testing.T) {
for _, test := range tests {
test := test
ctx, td := NewTestDaemon()
defer td.Close()

t.Run(test.name, func(t *testing.T) {
test.test(t, cons.constructor(fakeSession(t, test.failConn)))
session, cleanup := fakeSession(t, test.failConn)
defer cleanup()
test.test(ctx, td, t, cons.constructor(session))
})
}
})
}
}

func fakeSession(t *testing.T, failConn bool) *session.Session {
func fakeSession(t *testing.T, failConn bool) (*session.Session, func()) {
cfg := &aws.Config{
Region: aws.String("fake-moon-1"),
Credentials: credentials.NewStaticCredentials("akid", "secret", "noop"),
}

var ts *httptest.Server
if !failConn {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b := []byte(`{}`)
w.WriteHeader(http.StatusOK)
w.Write(b)
Expand All @@ -92,22 +109,28 @@ func fakeSession(t *testing.T, failConn bool) *session.Session {
}
s, err := session.NewSession(cfg)
assert.NoError(t, err)
return s
return s, func() {
if ts != nil {
ts.Close()
}
}
}

func testClientSuccessfulConnection(t *testing.T, svc *lambda.Lambda) {
TestDaemon.Reset()
ctx, root := BeginSegment(context.Background(), "Test")
func testClientSuccessfulConnection(ctx context.Context, td *TestDaemon, t *testing.T, svc *lambda.Lambda) {
ctx, root := BeginSegment(ctx, "Test")
_, err := svc.ListFunctionsWithContext(ctx, &lambda.ListFunctionsInput{})
root.Close(nil)
assert.NoError(t, err)

s, e := TestDaemon.Recv()
assert.NoError(t, e)
seg, err := td.Recv()
if !assert.NoError(t, err) {
return
}

subseg := &Segment{}
assert.NotEmpty(t, s.Subsegments)
assert.NoError(t, json.Unmarshal(s.Subsegments[0], &subseg))
var subseg *Segment
if !assert.NoError(t, json.Unmarshal(seg.Subsegments[0], &subseg)) {
return
}
assert.False(t, subseg.Fault)
assert.NotEmpty(t, subseg.Subsegments)

Expand Down Expand Up @@ -144,19 +167,21 @@ func testClientSuccessfulConnection(t *testing.T, svc *lambda.Lambda) {
}
}

func testClientFailedConnection(t *testing.T, svc *lambda.Lambda) {
TestDaemon.Reset()
ctx, root := BeginSegment(context.Background(), "Test")
func testClientFailedConnection(ctx context.Context, td *TestDaemon, t *testing.T, svc *lambda.Lambda) {
ctx, root := BeginSegment(ctx, "Test")
_, err := svc.ListFunctionsWithContext(ctx, &lambda.ListFunctionsInput{})
root.Close(nil)
assert.Error(t, err)

s, e := TestDaemon.Recv()
assert.NoError(t, e)
seg, err := td.Recv()
if !assert.NoError(t, err) {
return
}

subseg := &Segment{}
assert.NotEmpty(t, s.Subsegments)
assert.NoError(t, json.Unmarshal(s.Subsegments[0], &subseg))
var subseg *Segment
if !assert.NoError(t, json.Unmarshal(seg.Subsegments[0], &subseg)) {
return
}
assert.True(t, subseg.Fault)
// Should contain 'marshal' and 'attempt' subsegments only.
assert.Len(t, subseg.Subsegments, 2)
Expand All @@ -178,39 +203,31 @@ func testClientFailedConnection(t *testing.T, svc *lambda.Lambda) {
assert.NotEmpty(t, connectSubseg.Subsegments)
}

func testClientWithoutSegment(t *testing.T, svc *lambda.Lambda) {
Configure(Config{ContextMissingStrategy: &TestContextMissingStrategy{}})
defer ResetConfig()

ctx := context.Background()
func testClientWithoutSegment(ctx context.Context, td *TestDaemon, t *testing.T, svc *lambda.Lambda) {
_, err := svc.ListFunctionsWithContext(ctx, &lambda.ListFunctionsInput{})
assert.NoError(t, err)
}

func testAWSDataRace(t *testing.T, svc *lambda.Lambda) {
Configure(Config{ContextMissingStrategy: &TestContextMissingStrategy{},DaemonAddr: "localhost:3000"})
defer ResetConfig()

ctx, cancel := context.WithCancel(context.Background())

func testAWSDataRace(ctx context.Context, td *TestDaemon, t *testing.T, svc *lambda.Lambda) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
ctx, seg := BeginSegment(ctx, "TestSegment")

wg := sync.WaitGroup{}

var wg sync.WaitGroup
for i := 0; i < 5; i++ {
if i!=3 && i!=2{
if i != 3 && i != 2 {
wg.Add(1)
}
go func(i int) {
if i!=3 && i!=2{
time.Sleep(1)
if i != 3 && i != 2 {
time.Sleep(time.Nanosecond)
defer wg.Done()
}
_, seg := BeginSubsegment(ctx, "TestSubsegment1")
time.Sleep(1)
time.Sleep(time.Nanosecond)
seg.Close(nil)
svc.ListFunctionsWithContext(ctx, &lambda.ListFunctionsInput{})
if i== 3 || i==2{
if i == 3 || i == 2 {
cancel() // cancel context
}
}(i)
Expand Down
164 changes: 84 additions & 80 deletions xray/capture_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,91 +20,91 @@ import (
)

func TestSimpleCapture(t *testing.T) {
TestDaemon.Reset()
ctx, root := BeginSegment(context.Background(), "Test")
err := Capture(ctx, "TestService", func(ctx1 context.Context) error {
ctx = ctx1
defer root.Close(nil)
ctx, td := NewTestDaemon()
defer td.Close()

ctx, root := BeginSegment(ctx, "Test")
err := Capture(ctx, "TestService", func(context.Context) error {
root.Close(nil)
return nil
})
assert.NoError(t, err)

s, e := TestDaemon.Recv()
assert.NoError(t, e)
assert.Equal(t, "Test", s.Name)
assert.Equal(t, root.TraceID, s.TraceID)
assert.Equal(t, root.ID, s.ID)
assert.Equal(t, root.StartTime, s.StartTime)
assert.Equal(t, root.EndTime, s.EndTime)
assert.NotNil(t, s.Subsegments)
subseg := &Segment{}
assert.NoError(t, json.Unmarshal(s.Subsegments[0], &subseg))
assert.Equal(t, "TestService", subseg.Name)
}

func TestCaptureAysnc(t *testing.T) {
TestDaemon.Reset()
ctx, root := BeginSegment(context.Background(), "Test")
CaptureAsync(ctx, "TestService", func(ctx1 context.Context) error {
ctx = ctx1
return nil
})
root.Close(nil)

s, e := TestDaemon.Recv()
assert.NoError(t, e)
assert.Equal(t, "Test", s.Name)
assert.Equal(t, root.TraceID, s.TraceID)
assert.Equal(t, root.ID, s.ID)
assert.Equal(t, root.StartTime, s.StartTime)
assert.Equal(t, root.EndTime, s.EndTime)
assert.NotNil(t, s.Subsegments)
subseg := &Segment{}
assert.NoError(t, json.Unmarshal(s.Subsegments[0], &subseg))
assert.Equal(t, "TestService", subseg.Name)
seg, err := td.Recv()
if !assert.NoError(t, err) {
return
}
assert.Equal(t, "Test", seg.Name)
assert.Equal(t, root.TraceID, seg.TraceID)
assert.Equal(t, root.ID, seg.ID)
assert.Equal(t, root.StartTime, seg.StartTime)
assert.Equal(t, root.EndTime, seg.EndTime)
assert.NotNil(t, seg.Subsegments)
var subseg *Segment
if assert.NoError(t, json.Unmarshal(seg.Subsegments[0], &subseg)) {
assert.Equal(t, "TestService", subseg.Name)
}
}

func TestErrorCapture(t *testing.T) {
TestDaemon.Reset()
ctx, root := BeginSegment(context.Background(), "Test")
defaultStrategy, _ := exception.NewDefaultFormattingStrategy()
err := Capture(ctx, "ErrorService", func(ctx1 context.Context) error {
ctx, td := NewTestDaemon()
defer td.Close()

ctx, root := BeginSegment(ctx, "Test")
defaultStrategy, err := exception.NewDefaultFormattingStrategy()
if !assert.NoError(t, err) {
return
}
captureErr := Capture(ctx, "ErrorService", func(context.Context) error {
defer root.Close(nil)
return defaultStrategy.Error("MyError")
})
if !assert.Error(t, captureErr) {
return
}

s, e := TestDaemon.Recv()
assert.NoError(t, e)
subseg := &Segment{}
assert.NoError(t, json.Unmarshal(s.Subsegments[0], &subseg))
assert.Equal(t, err.Error(), subseg.Cause.Exceptions[0].Message)
seg, err := td.Recv()
if !assert.NoError(t, err) {
return
}
var subseg *Segment
if !assert.NoError(t, json.Unmarshal(seg.Subsegments[0], &subseg)) {
return
}
assert.Equal(t, captureErr.Error(), subseg.Cause.Exceptions[0].Message)
assert.Equal(t, true, subseg.Fault)
assert.Equal(t, "error", subseg.Cause.Exceptions[0].Type)
assert.Equal(t, "TestErrorCapture.func1", subseg.Cause.Exceptions[0].Stack[0].Label)
assert.Equal(t, "Capture", subseg.Cause.Exceptions[0].Stack[1].Label)
}

func TestPanicCapture(t *testing.T) {
TestDaemon.Reset()
ctx, root := BeginSegment(context.Background(), "Test")
var err error
ctx, td := NewTestDaemon()
defer td.Close()

ctx, root := BeginSegment(ctx, "Test")
var captureErr error
func() {
defer func() {
if p := recover(); p != nil {
err = errors.New(p.(string))
captureErr = errors.New(p.(string))
}
root.Close(err)
root.Close(captureErr)
}()
Capture(ctx, "PanicService", func(ctx1 context.Context) error {
_ = Capture(ctx, "PanicService", func(context.Context) error {
panic("MyPanic")
})
}()

s, e := TestDaemon.Recv()
assert.NoError(t, e)
subseg := &Segment{}
assert.NoError(t, json.Unmarshal(s.Subsegments[0], &subseg))
assert.Equal(t, err.Error(), subseg.Cause.Exceptions[0].Message)
seg, err := td.Recv()
if !assert.NoError(t, err) {
return
}
var subseg *Segment
if !assert.NoError(t, json.Unmarshal(seg.Subsegments[0], &subseg)) {
return
}
assert.Equal(t, captureErr.Error(), subseg.Cause.Exceptions[0].Message)
assert.Equal(t, "panic", subseg.Cause.Exceptions[0].Type)
assert.Equal(t, "TestPanicCapture.func1.2", subseg.Cause.Exceptions[0].Stack[0].Label)
assert.Equal(t, "Capture", subseg.Cause.Exceptions[0].Stack[1].Label)
Expand All @@ -120,7 +120,7 @@ func TestNoSegmentCapture(t *testing.T) {
err = errors.New(p.(string))
}
}()
Capture(context.Background(), "PanicService", func(ctx1 context.Context) error {
_ = Capture(context.Background(), "PanicService", func(context.Context) error {
panic("MyPanic")
})
}()
Expand All @@ -130,28 +130,32 @@ func TestNoSegmentCapture(t *testing.T) {
}

func TestCaptureAsync(t *testing.T) {
var mu sync.Mutex
TestDaemon.Reset()
ctx, root := BeginSegment(context.Background(), "Test")
CaptureAsync(ctx, "TestService", func(ctx1 context.Context) error {
mu.Lock()
defer mu.Unlock()
ctx = ctx1
defer root.Close(nil)
ctx, td := NewTestDaemon()
defer td.Close()

var wg sync.WaitGroup
wg.Add(1)
ctx, root := BeginSegment(ctx, "Test")
CaptureAsync(ctx, "TestService", func(context.Context) error {
defer wg.Done()
root.Close(nil)
return nil
})

s, e := TestDaemon.Recv()
assert.NoError(t, e)
mu.Lock() // avoid race detection
defer mu.Unlock()
assert.Equal(t, "Test", s.Name)
assert.Equal(t, root.TraceID, s.TraceID)
assert.Equal(t, root.ID, s.ID)
assert.Equal(t, root.StartTime, s.StartTime)
assert.Equal(t, root.EndTime, s.EndTime)
assert.NotNil(t, s.Subsegments)
subseg := &Segment{}
assert.NoError(t, json.Unmarshal(s.Subsegments[0], &subseg))
assert.Equal(t, "TestService", subseg.Name)
seg, err := td.Recv()
if !assert.NoError(t, err) {
return
}

wg.Wait()
assert.Equal(t, "Test", seg.Name)
assert.Equal(t, root.TraceID, seg.TraceID)
assert.Equal(t, root.ID, seg.ID)
assert.Equal(t, root.StartTime, seg.StartTime)
assert.Equal(t, root.EndTime, seg.EndTime)
assert.NotNil(t, seg.Subsegments)
var subseg *Segment
if assert.NoError(t, json.Unmarshal(seg.Subsegments[0], &subseg)) {
assert.Equal(t, "TestService", subseg.Name)
}
}
Loading

0 comments on commit f1811db

Please sign in to comment.