Skip to content

Commit

Permalink
[#16] .Update test
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasObenaus committed Feb 18, 2020
1 parent a214cad commit 6ffd274
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 37 deletions.
6 changes: 2 additions & 4 deletions cosmos.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,8 @@ func New(host string, options ...Option) (*Cosmos, error) {
go func() {
defer cosmos.wg.Done()
for range cosmos.errorChannel {
// consume the errors from the channel
// at the moment it is not needed to post them to the log since they are
// anyway handed over to the caller
// For debugging the following line can be uncommented
// consume the errors from the channel at the moment it is not needed to post them to the log since they are
// anyway handed over to the caller. For debugging the following line can be uncommented
// cosmos.logger.Error().Err(err).Msg("Error from connection pool received")
}
cosmos.logger.Debug().Msg("Error channel consumer closed")
Expand Down
21 changes: 12 additions & 9 deletions cosmosResponse.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,21 @@ func extractFirstError(responses []interfaces.Response) error {
// parseAttributeMap parses the given attribute map assuming that it contains
// CosmosDB specific headers.
func parseAttributeMap(attributes map[string]interface{}) (responseInformation, error) {

responseInfo := responseInformation{}
if valueStr, ok := attributes[string(headerStatusCode)]; ok {

value, err := cast.ToInt16E(valueStr)
if err != nil {
return responseInfo, errors.Wrapf(err, "Failed parsing '%s'", headerStatusCode)
}
statusCode := int(value)
responseInfo.statusCode = statusCode
responseInfo.statusDescription = statusCodeToDescription(statusCode)
// immediately return in case the header status code is missing
if _, ok := attributes[string(headerStatusCode)]; !ok {
return responseInfo, fmt.Errorf("'%s' is missing", headerStatusCode)
}

valueStr := attributes[string(headerStatusCode)]
value, err := cast.ToInt16E(valueStr)
if err != nil {
return responseInfo, errors.Wrapf(err, "Failed parsing '%s'", headerStatusCode)
}
statusCode := int(value)
responseInfo.statusCode = statusCode
responseInfo.statusDescription = statusCodeToDescription(statusCode)

if valueStr, ok := attributes[string(headerSubStatusCode)]; ok {
responseInfo.subStatusCode = int(cast.ToInt16(valueStr))
Expand Down
123 changes: 99 additions & 24 deletions cosmosResponse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,107 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/supplyon/gremcos/interfaces"
)

//func TestDetectTooManyRequestsError(t *testing.T) {
// tooManyRequests := interfaces.Response{
// RequestID: "cfe23609-abcd-efgh-ijkl-326cd091aa37",
// Status: interfaces.Status{
// Code: interfaces.StatusServerError,
// Message: "\r\n\nActivityId : 00000000-0000-0000-0000-000000000000\nExceptionType : DocumentClientException\nExceptionMessage ....",
// Attributes: map[string]interface{}{
// "x-ms-retry-after-ms": "00:00:09.0530000",
// "x-ms-substatus-code": 3200,
// "x-ms-source": "Microsoft.Azure.Documents.Client",
// "x-ms-status-code": 429,
// "x-ms-request-charge": 3779.34,
// "x-ms-total-request-charge": 3779.34,
// "x-ms-server-time-ms": 1056.2705,
// "x-ms-total-server-time-ms": 1056.2705,
// "x-ms-activity-id": "fdd08592-abcd-efgh-ijkl-97d35c2dda52",
// },
// },
// Result: interfaces.Result{},
// }
//
// err := extractError(tooManyRequests)
// assert.NoError(t, err)
//}
func TestExtractFirstError(t *testing.T) {
// GIVEN
noError := interfaces.Response{
Status: interfaces.Status{
Code: interfaces.StatusSuccess,
},
}
tooManyRequests := interfaces.Response{
Status: interfaces.Status{
Code: interfaces.StatusServerError,
Attributes: map[string]interface{}{
"x-ms-status-code": 429,
"x-ms-substatus-code": 3200,
},
},
}
responses := []interfaces.Response{noError, tooManyRequests}

// WHEN
err := extractFirstError(responses)

// THEN
assert.Error(t, err)
assert.Contains(t, err.Error(), "429")
}

func TestExtractFirstErrorNoError(t *testing.T) {
// GIVEN
noError := interfaces.Response{
Status: interfaces.Status{
Code: interfaces.StatusSuccess,
},
}
responses := []interfaces.Response{noError}

// WHEN
err := extractFirstError(responses)

// THEN
assert.NoError(t, err)
}

func TestExtractFirstErrorNoServerError(t *testing.T) {
// GIVEN
tooManyRequests := interfaces.Response{
Status: interfaces.Status{
Code: interfaces.StatusScriptEvaluationError,
Message: "ABCD",
},
}
responses := []interfaces.Response{tooManyRequests}

// WHEN
err := extractFirstError(responses)

// THEN
assert.Error(t, err)
assert.Contains(t, err.Error(), "ABCD")
}

func TestExtractFirstErrorNoAttributeMap(t *testing.T) {
// GIVEN
tooManyRequests := interfaces.Response{
Status: interfaces.Status{
Code: interfaces.StatusServerError,
Message: "ABCD",
},
}
responses := []interfaces.Response{tooManyRequests}

// WHEN
err := extractFirstError(responses)

// THEN
assert.Error(t, err)
assert.Contains(t, err.Error(), "ABCD")
}

func TestExtractFirstErrorFaultyAttributeMap(t *testing.T) {
// GIVEN
tooManyRequests := interfaces.Response{
Status: interfaces.Status{
Code: interfaces.StatusServerError,
Message: "ABCD",
Attributes: map[string]interface{}{
"x-ms-status-code": "invalid",
},
},
}
responses := []interfaces.Response{tooManyRequests}

// WHEN
err := extractFirstError(responses)

// THEN
assert.Error(t, err)
assert.Contains(t, err.Error(), "ABCD")
}

func TestParseAttributeMap(t *testing.T) {
// GIVEN
Expand Down

0 comments on commit 6ffd274

Please sign in to comment.