Skip to content

Commit

Permalink
refactor(xignite): add error logs (#530)
Browse files Browse the repository at this point in the history
  • Loading branch information
dakimura authored Dec 13, 2021
1 parent 71a4005 commit 0f59ea9
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 17 deletions.
16 changes: 15 additions & 1 deletion contrib/xignitefeeder/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,23 @@ func (c *DefaultClient) GetRealTimeQuotes(identifiers []string) (response GetQuo
if err != nil {
return response, err
}

log.Debug(fmt.Sprintf("[Xignite API] Delay(sec) in GetQuotes response= %f", response.DelaySec))

// log not-successful responses
if len(identifiers) != len(response.ArrayOfEquityQuote) {
log.Error(fmt.Sprintf("The len(ArrayOfEquityQuotes) returned by GetQuotes API is different "+
"from len(identifiers) requested. returned=%d, requested=%d, error response=%v",
len(response.ArrayOfEquityQuote), len(identifiers), response))
return response, nil
}

for i, equityQuote := range response.ArrayOfEquityQuote {
if equityQuote.Outcome != "Success" {
log.Error(fmt.Sprintf("GetQuotes API returned an error. identifier=%s, response=%v",
identifiers[i], equityQuote))
}
}

return response, nil
}

Expand Down
81 changes: 66 additions & 15 deletions contrib/xignitefeeder/api/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"io/ioutil"
"net/http"
"reflect"
"testing"
"time"
)
Expand Down Expand Up @@ -47,23 +48,73 @@ func NewMockClient(t *testing.T, expectedResponse interface{}) *http.Client {
}
}

func TestDefaultAPIClient_GetRealTimeQuotes_Success(t *testing.T) {
func TestDefaultClient_GetRealTimeQuotes(t *testing.T) {
t.Parallel()
// --- given ---
SUT := &DefaultClient{
// return "Outcome: Success" response body
httpClient: NewMockClient(t, GetQuotesResponse{ArrayOfEquityQuote: []EquityQuote{{Outcome: "Success"}}}),
token: DummyXigniteToken}

// --- when ---
got, err := SUT.GetRealTimeQuotes([]string{"hoge"})

// --- then ---
if err != nil {
t.Fatalf("Error should be nil. Err = %v", err)
}
if got.ArrayOfEquityQuote[0].Outcome != "Success" {
t.Errorf("Outcome = %v, want %v", got.ArrayOfEquityQuote[0].Outcome, "Success")
tests := []struct {
name string
httpClient *http.Client
identifiers []string
wantResponse GetQuotesResponse
wantErr bool
}{
{
name: "Success",
httpClient: NewMockClient(t, GetQuotesResponse{ArrayOfEquityQuote: []EquityQuote{{Outcome: "Success"}}}),
identifiers: []string{"foo"},
wantResponse: GetQuotesResponse{ArrayOfEquityQuote: []EquityQuote{{Outcome: "Success"}}},
wantErr: false,
},
{
name: "SystemError",
httpClient: NewMockClient(t, GetQuotesResponse{
ArrayOfEquityQuote: []EquityQuote{
{
Outcome: "SystemError",
Message: "An unexpected error occurred.",
},
},
}),
identifiers: []string{"foo"},
wantResponse: GetQuotesResponse{ArrayOfEquityQuote: []EquityQuote{
{Outcome: "SystemError", Message: "An unexpected error occurred."},
}},
wantErr: false,
},
{
name: "3 identifiers are requested but only 2 equity quotes are returned",
httpClient: NewMockClient(t, GetQuotesResponse{
ArrayOfEquityQuote: []EquityQuote{
{Outcome: "Success", Message: "Success1"},
{Outcome: "SystemError", Message: "An unexpected error occurred."},
},
}),
identifiers: []string{"foo", "bar", "fizz"},
wantResponse: GetQuotesResponse{ArrayOfEquityQuote: []EquityQuote{
{Outcome: "Success", Message: "Success1"},
{Outcome: "SystemError", Message: "An unexpected error occurred."},
}},
wantErr: false,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

c := &DefaultClient{
httpClient: tt.httpClient,
token: DummyXigniteToken,
}
gotResponse, err := c.GetRealTimeQuotes(tt.identifiers)
if (err != nil) != tt.wantErr {
t.Errorf("GetRealTimeQuotes() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(gotResponse, tt.wantResponse) {
t.Errorf("GetRealTimeQuotes() gotResponse = %v, want %v", gotResponse, tt.wantResponse)
}
})
}
}

Expand Down
2 changes: 2 additions & 0 deletions contrib/xignitefeeder/api/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
// GetQuotesResponse is a response model for Get Quotes endpoint
type GetQuotesResponse struct {
DelaySec float32 `json:"Delay"`
Message string `json:"Message"`
ExceptionMessage string `json:"ExceptionMessage"`
ArrayOfEquityQuote []EquityQuote `json:"ArrayOfEquityQuote"`
}

Expand Down
2 changes: 1 addition & 1 deletion contrib/xignitefeeder/symbols/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (m ManagerImpl) UpdateSymbols() {

// if ListSymbols API returns an error, don't update the target symbols
if err != nil || resp.Outcome != "Success" {
log.Warn("err=%v, API response=%v", err, resp)
log.Error(fmt.Sprintf("err=%v, List Symbols API response=%v", err, resp))
return
}

Expand Down

0 comments on commit 0f59ea9

Please sign in to comment.