From 14df170d990154ef50299a4d9ee0e461d0ba7051 Mon Sep 17 00:00:00 2001 From: Gayathri Date: Wed, 1 Aug 2018 18:27:48 +0530 Subject: [PATCH] To fix test cases failure due to change in spans --- wrp/messages_test.go | 6 ++-- wrp/wrphttp/headers.go | 56 ++++++++++++++++++++++++------------- wrp/wrphttp/headers_test.go | 18 +++++------- 3 files changed, 47 insertions(+), 33 deletions(-) diff --git a/wrp/messages_test.go b/wrp/messages_test.go index b54e6288..4528df8f 100644 --- a/wrp/messages_test.go +++ b/wrp/messages_test.go @@ -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"}, }, @@ -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}, }, } @@ -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),}}, }, } ) diff --git a/wrp/wrphttp/headers.go b/wrp/wrphttp/headers.go index 670417bd..870166b9 100644 --- a/wrp/wrphttp/headers.go +++ b/wrp/wrphttp/headers.go @@ -7,7 +7,7 @@ import ( "net/http" "strconv" "strings" - + "time" "github.com/Comcast/webpa-common/wrp" ) @@ -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) { @@ -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 { diff --git a/wrp/wrphttp/headers_test.go b/wrp/wrphttp/headers_test.go index 24e217e6..ae8a835a 100644 --- a/wrp/wrphttp/headers_test.go +++ b/wrp/wrphttp/headers_test.go @@ -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" @@ -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 @@ -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"}, }, @@ -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", }, @@ -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 @@ -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", }, @@ -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"}, },