Skip to content

Commit

Permalink
bug seems fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
ivcosla committed May 16, 2019
1 parent 2bdd5a6 commit 40f71f9
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 16 deletions.
23 changes: 10 additions & 13 deletions pkg/messaging-discovery/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
Expand Down Expand Up @@ -65,7 +64,7 @@ func (c *httpClient) Entry(ctx context.Context, publicKey cipher.PubKey) (*Entry
return nil, err
}

return nil, errors.New(message.String())
return nil, errFromString(message.String())
}

err = json.NewDecoder(resp.Body).Decode(&entry)
Expand Down Expand Up @@ -110,8 +109,7 @@ func (c *httpClient) SetEntry(ctx context.Context, e *Entry) error {
if err != nil {
return err
}

return errors.New(httpResponse.String())
return errFromString(httpResponse.Message)
}
return nil
}
Expand All @@ -130,11 +128,11 @@ func (c *httpClient) UpdateEntry(ctx context.Context, sk cipher.SecKey, e *Entry

for {
err = c.SetEntry(ctx, e)
if err != nil && err != ErrValidationWrongSequence {
e.Sequence--
return err
}
if err == ErrValidationWrongSequence {
if err != nil {
if err != ErrValidationWrongSequence {
e.Sequence--
return err
}
rE, entryErr := c.Entry(ctx, e.Static)
if entryErr != nil {
return err
Expand All @@ -148,10 +146,9 @@ func (c *httpClient) UpdateEntry(ctx context.Context, sk cipher.SecKey, e *Entry
if err != nil {
return err
}
continue
}
if err == nil {
return nil
}
return nil
}
}

Expand Down Expand Up @@ -181,7 +178,7 @@ func (c *httpClient) AvailableServers(ctx context.Context) ([]*Entry, error) {
return nil, err
}

return nil, errors.New(message.String())
return nil, errFromString(message.String())
}

err = json.NewDecoder(resp.Body).Decode(&entries)
Expand Down
8 changes: 8 additions & 0 deletions pkg/messaging-discovery/client/client_mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,14 @@ func TestNewMockUpdateEntriesEndpoint(t *testing.T) {
e.Server.Address = "different one"
},
},
{
name: "udpate retries on wrong sequence",
responseShouldError: false,
secretKey: sk,
entryPreHook: func(entry *client.Entry) {
entry.Sequence = 3
},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
Expand Down
24 changes: 24 additions & 0 deletions pkg/messaging-discovery/client/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,32 @@ var (
ErrValidationNoClientOrServer = NewEntryValidationError("entry has neither client or server field")
ErrValidationWrongSequence = NewEntryValidationError("sequence field of new entry is not sequence of old entry + 1")
ErrValidationWrongTime = NewEntryValidationError("previous entry timestamp is not set before current entry timestamp")

errReverseMap = map[string]error{
ErrKeyNotFound.Error(): ErrKeyNotFound,
ErrUnexpected.Error(): ErrUnexpected,
ErrUnauthorized.Error(): ErrUnauthorized,
ErrBadInput.Error(): ErrBadInput,
ErrValidationNonZeroSequence.Error(): ErrValidationNonZeroSequence,
ErrValidationNilEphemerals.Error(): ErrValidationNilEphemerals,
ErrValidationNilKeys.Error(): ErrValidationNilKeys,
ErrValidationNonNilEphemerals.Error(): ErrValidationNonNilEphemerals,
ErrValidationNoSignature.Error(): ErrValidationNoSignature,
ErrValidationNoVersion.Error(): ErrValidationNoVersion,
ErrValidationNoClientOrServer.Error(): ErrValidationNoClientOrServer,
ErrValidationWrongSequence.Error(): ErrValidationWrongSequence,
ErrValidationWrongTime.Error(): ErrValidationWrongTime,
}
)

func errFromString(s string) error {
err, ok := errReverseMap[s]
if !ok {
return ErrUnexpected
}
return err
}

// EntryValidationError represents transient error caused by invalid
// data in Entry
type EntryValidationError struct {
Expand Down
11 changes: 9 additions & 2 deletions pkg/messaging-discovery/client/http_message.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package client

import (
"errors"
"fmt"
"net/http"
)

// Exposed Http messages
var (
MsgEntrySet = HTTPMessage{Code: http.StatusOK, Message: "wrote a new entry"}
MsgEntryUpdated = HTTPMessage{Code: http.StatusOK, Message: "wrote new entry iteration"}
MsgEntrySet = HTTPMessage{Code: http.StatusOK, Message: "wrote a new entry"}
MsgEntryUpdated = HTTPMessage{Code: http.StatusOK, Message: "wrote new entry iteration"}
)

// HTTPMessage represents a message to be returned as an http response
Expand All @@ -20,3 +21,9 @@ type HTTPMessage struct {
func (h HTTPMessage) String() string {
return fmt.Sprintf("status code: %d. message: %s", h.Code, h.Message)
}

// ToError returns an error representing the httpMessage for error comparisons, this is preferred for this type instead
// of implementing error
func (h HTTPMessage) ToError() error {
return errors.New(h.String())
}
25 changes: 24 additions & 1 deletion pkg/messaging-discovery/client/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,30 @@ func (m *mockClient) UpdateEntry(ctx context.Context, sk cipher.SecKey, e *Entry
return err
}

return m.SetEntry(ctx, e)
for {
err = m.SetEntry(ctx, e)
if err != nil {
if err != ErrValidationWrongSequence {
e.Sequence--
return err
}
rE, entryErr := m.Entry(ctx, e.Static)
if entryErr != nil {
return err
}
if rE.Timestamp > e.Timestamp { // If there is a more up to date entry drop update
e.Sequence = rE.Sequence
return nil
}
e.Sequence = rE.Sequence + 1
err := e.Sign(sk)
if err != nil {
return err
}
continue
}
return nil
}
}

// AvailableServers returns all the servers that the APIClient mock has
Expand Down

0 comments on commit 40f71f9

Please sign in to comment.