Skip to content

Commit

Permalink
To fix test cases failure due to change in spans
Browse files Browse the repository at this point in the history
  • Loading branch information
gbuddappagari committed Aug 3, 2018
1 parent 2855e2c commit 14df170
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 33 deletions.
6 changes: 3 additions & 3 deletions wrp/messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func TestMessage(t *testing.T) {
TransactionUUID: "DEADBEEF",
Headers: []string{"Header1", "Header2"},
Metadata: map[string]string{"name": "value"},
Spans: []Money_Span{{"abc", time.Now(), time.Duration(223),}, {"dfc",time.Now(),time.Duration(224),}},
Spans: []Money_Span{{"abc", time.Date(2018, 1, 1, 12, 0, 0, 0, time.UTC), time.Duration(223),}, {"dfc",time.Date(2018, 1, 1, 12, 0, 0, 0, time.UTC),time.Duration(224),}},
Payload: []byte{1, 2, 3, 4, 0xff, 0xce},
PartnerIDs: []string{"foo"},
},
Expand Down Expand Up @@ -324,7 +324,7 @@ func TestSimpleRequestResponse(t *testing.T) {
TransactionUUID: "DEADBEEF",
Headers: []string{"Header1", "Header2"},
Metadata: map[string]string{"name": "value"},
Spans: []Money_Span{{"abc", time.Now(), time.Duration(234),}, {"dfc",time.Now(),time.Duration(224),}},
Spans: []Money_Span{{"abc", time.Date(2018, 1, 1, 12, 0, 0, 0, time.UTC), time.Duration(234),}, {"dfc",time.Date(2018, 1, 1, 12, 0, 0, 0, time.UTC),time.Duration(224),}},
Payload: []byte{1, 2, 3, 4, 0xff, 0xce},
},
}
Expand Down Expand Up @@ -546,7 +546,7 @@ func TestCRUD(t *testing.T) {
TransactionUUID: "DEADBEEF",
Headers: []string{"Header1", "Header2"},
Metadata: map[string]string{"name": "value"},
Spans: []Money_Span{{"abc", time.Now(), time.Duration(234),}, {"dfc",time.Now(),time.Duration(224),}},
Spans: []Money_Span{{"abc", time.Date(2018, 1, 1, 12, 0, 0, 0, time.UTC), time.Duration(234),}, {"dfc",time.Date(2018, 1, 1, 12, 0, 0, 0, time.UTC),time.Duration(224),}},
},
}
)
Expand Down
56 changes: 37 additions & 19 deletions wrp/wrphttp/headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"net/http"
"strconv"
"strings"

"time"
"github.com/Comcast/webpa-common/wrp"
)

Expand Down Expand Up @@ -75,23 +75,41 @@ func getBoolHeader(h http.Header, n string) *bool {
return &b
}

func getSpans(h http.Header) [][]string {
var spans [][]string

for _, value := range h[SpanHeader] {
fields := strings.Split(value, ",")
if len(fields) != 3 {
panic(fmt.Errorf("Invalid %s header: %s", SpanHeader, value))
}

for i := 0; i < len(fields); i++ {
fields[i] = strings.TrimSpace(fields[i])
}

spans = append(spans, fields)
}

return spans
func getSpans(h http.Header) []wrp.Money_Span {
var spans []wrp.Money_Span
for _, value := range h[SpanHeader]{
fields := strings.Split(value, ",")
var Name string
var Start time.Time
var Duration time.Duration
if len(fields) != 3 {
panic(fmt.Errorf("Invalid %s header: %s", SpanHeader, value))
}

for i := 0; i < len(fields); i++ {
fields[i] = strings.TrimSpace(fields[i])
switch i {
case 0:
name := fields[i]
Name = name
case 1:
start, err := strconv.ParseInt(fields[i], 10, 64)
if err != nil {
panic(err)
}
Start = time.Unix(start, 0).UTC()
case 2:
fields[i] = strings.Trim(fields[i], "ns")
duration, err := strconv.ParseInt(fields[i], 10, 64)
if err != nil {
panic(err)
}
Duration = time.Duration(duration)
}
}
spans = append(spans, wrp.Money_Span{Name, Start, Duration})
}
return spans
}

func readPayload(h http.Header, p io.Reader) ([]byte, string) {
Expand Down Expand Up @@ -203,7 +221,7 @@ func AddMessageHeaders(h http.Header, m *wrp.Message) {
}

for _, s := range m.Spans {
h.Add(SpanHeader, strings.Join(s, ","))
h.Add(SpanHeader, (s.Name+","+strconv.FormatInt(s.Start.Unix(),10)+","+s.Duration.String()))
}

if len(m.Accept) > 0 {
Expand Down
18 changes: 7 additions & 11 deletions wrp/wrphttp/headers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strconv"
"strings"
"testing"

"time"
"github.com/Comcast/webpa-common/wrp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
Expand All @@ -23,6 +23,7 @@ func testNewMessageFromHeadersSuccess(t *testing.T) {
expectedStatus int64 = 928
expectedRequestDeliveryResponse int64 = 1
expectedIncludeSpans bool = true
expectedSpans []wrp.Money_Span = []wrp.Money_Span{{"foo", time.Date(2018, 1, 1, 12, 0, 0, 0, time.UTC), time.Duration(223)},{"bar", time.Date(2018, 1, 2, 12, 0, 0, 0, time.UTC), time.Duration(211)},}

testData = []struct {
header http.Header
Expand Down Expand Up @@ -56,10 +57,7 @@ func testNewMessageFromHeadersSuccess(t *testing.T) {
StatusHeader: []string{strconv.FormatInt(expectedStatus, 10)},
RequestDeliveryResponseHeader: []string{strconv.FormatInt(expectedRequestDeliveryResponse, 10)},
IncludeSpansHeader: []string{strconv.FormatBool(expectedIncludeSpans)},
SpanHeader: []string{
"foo, bar, moo",
"goo, gar, hoo",
},
SpanHeader: []string{expectedSpans[0].Name+","+strconv.FormatInt(expectedSpans[0].Start.Unix(),10)+","+expectedSpans[0].Duration.String(),expectedSpans[1].Name+","+strconv.FormatInt(expectedSpans[1].Start.Unix(),10)+","+expectedSpans[1].Duration.String(),},
AcceptHeader: []string{"application/json"},
PathHeader: []string{"/foo/bar"},
},
Expand All @@ -72,10 +70,7 @@ func testNewMessageFromHeadersSuccess(t *testing.T) {
Status: &expectedStatus,
RequestDeliveryResponse: &expectedRequestDeliveryResponse,
IncludeSpans: &expectedIncludeSpans,
Spans: [][]string{
{"foo", "bar", "moo"},
{"goo", "gar", "hoo"},
},
Spans: expectedSpans,
Accept: "application/json",
Path: "/foo/bar",
},
Expand Down Expand Up @@ -225,6 +220,7 @@ func TestAddMessageHeaders(t *testing.T) {
expectedStatus int64 = 123
expectedRequestDeliveryResponse int64 = 2
expectedIncludeSpans bool = true
expectedSpans []wrp.Money_Span = []wrp.Money_Span{{"foo", time.Date(2018, 2, 1, 12, 0, 0, 0, time.UTC), time.Duration(211)}}

testData = []struct {
message wrp.Message
Expand All @@ -247,7 +243,7 @@ func TestAddMessageHeaders(t *testing.T) {
Status: &expectedStatus,
RequestDeliveryResponse: &expectedRequestDeliveryResponse,
IncludeSpans: &expectedIncludeSpans,
Spans: [][]string{{"foo", "bar", "graar"}},
Spans: expectedSpans,
Accept: "application/json",
Path: "/foo/bar",
},
Expand All @@ -259,7 +255,7 @@ func TestAddMessageHeaders(t *testing.T) {
StatusHeader: []string{strconv.FormatInt(expectedStatus, 10)},
RequestDeliveryResponseHeader: []string{strconv.FormatInt(expectedRequestDeliveryResponse, 10)},
IncludeSpansHeader: []string{strconv.FormatBool(expectedIncludeSpans)},
SpanHeader: []string{"foo,bar,graar"},
SpanHeader: []string{expectedSpans[0].Name+","+strconv.FormatInt(expectedSpans[0].Start.Unix(),10)+","+expectedSpans[0].Duration.String()},
AcceptHeader: []string{"application/json"},
PathHeader: []string{"/foo/bar"},
},
Expand Down

0 comments on commit 14df170

Please sign in to comment.