Skip to content

Commit

Permalink
Merge pull request #83 from mailgun/cclark/dev
Browse files Browse the repository at this point in the history
PIP-1303: support parsing rfc822 dataetime without day and run go fmt
  • Loading branch information
Takumi2008 authored Jun 4, 2021
2 parents 31f653f + b4ef3ea commit fced42d
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 16 deletions.
24 changes: 21 additions & 3 deletions clock/rfc822.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,35 @@ func ParseRFC822Time(s string) (Time, error) {
if err == nil {
return t, nil
}
if parseErr, ok := err.(*ParseError); !ok || parseErr.LayoutElem != "MST" {
if parseErr, ok := err.(*ParseError); !ok || (parseErr.LayoutElem != "MST" && parseErr.LayoutElem != "Mon") {
return Time{}, parseErr
}
if t, err = Parse("Mon, 2 Jan 2006 15:04:05 -0700", s); err == nil {
return t, nil
}
if parseErr, ok := err.(*ParseError); !ok || parseErr.LayoutElem != "" {
if parseErr, ok := err.(*ParseError); !ok || (parseErr.LayoutElem != "" && parseErr.LayoutElem != "Mon") {
return Time{}, parseErr
}
if t, err = Parse("Mon, 2 Jan 2006 15:04:05 -0700 (MST)", s); err == nil {
return t, nil
return t, err
}
if parseErr, ok := err.(*ParseError); !ok || parseErr.LayoutElem != "Mon" {
return Time{}, err
}
if t, err = Parse("2 Jan 2006 15:04:05 MST", s); err == nil {
return t, err
}
if parseErr, ok := err.(*ParseError); !ok || parseErr.LayoutElem != "MST" {
return Time{}, parseErr
}
if t, err = Parse("2 Jan 2006 15:04:05 -0700", s); err == nil {
return t, err
}
if parseErr, ok := err.(*ParseError); !ok || parseErr.LayoutElem != "" {
return Time{}, parseErr
}
if t, err = Parse("2 Jan 2006 15:04:05 -0700 (MST)", s); err == nil {
return t, err
}
return Time{}, err
}
Expand Down
28 changes: 24 additions & 4 deletions clock/rfc822_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,33 @@ func TestRFC822UnmarshalingError(t *testing.T) {
outError: `parsing time "Thu, 29 Aug 2019 11:20:07" as "Mon, 2 Jan 2006 15:04:05 -0700": cannot parse "" as "-0700"`,
}, {
inEncoded: `{"ts": "foo"}`,
outError: `parsing time "foo" as "Mon, 2 Jan 2006 15:04:05 MST": cannot parse "foo" as "Mon"`,
outError: `parsing time "foo" as "2 Jan 2006 15:04:05 MST": cannot parse "foo" as "2"`,
}, {
inEncoded: `{"ts": 42}`,
outError: "invalid syntax",
}} {
var ts testStruct
err := json.Unmarshal([]byte(tc.inEncoded), &ts)
assert.EqualError(t, err, tc.outError)
t.Run(tc.inEncoded, func(t *testing.T) {
var ts testStruct
err := json.Unmarshal([]byte(tc.inEncoded), &ts)
assert.EqualError(t, err, tc.outError)
})
}
}

func TestParseRFC822Time(t *testing.T) {
for _, tt := range []struct {
rfc822Time string
}{
{"Thu, 3 Jun 2021 12:01:05 MST"},
{"Thu, 3 Jun 2021 12:01:05 -0700"},
{"Thu, 3 Jun 2021 12:01:05 -0700 (MST)"},
{"2 Jun 2021 17:06:41 GMT"},
{"2 Jun 2021 17:06:41 -0700"},
{"2 Jun 2021 17:06:41 -0700 (MST)"},
} {
t.Run(tt.rfc822Time, func(t *testing.T) {
_, err := ParseRFC822Time(tt.rfc822Time)
assert.NoError(t, err)
})
}
}
2 changes: 1 addition & 1 deletion collections/lru_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func TestLRUCacheMap(t *testing.T) {
assert.Equal(t, 5, count)
assert.Equal(t, 4, cache.Size())

for _, item := range []int{1,2,4,5} {
for _, item := range []int{1, 2, 4, 5} {
v, ok := cache.Get(fmt.Sprintf("%d", item))
require.True(t, ok)
assert.Equal(t, item, v.(int))
Expand Down
1 change: 0 additions & 1 deletion discovery/memberlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ func (m *MemberList) Close(ctx context.Context) error {
case err := <-errCh:
return err
}
return nil
}

func (m *MemberList) GetPeers(_ context.Context) ([]Peer, error) {
Expand Down
7 changes: 3 additions & 4 deletions errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,16 @@ import (
// New also records the stack trace at the depth specified.
func NewWithDepth(message string, depth int) error {
return &fundamental{
msg: message,
msg: message,
CallStack: stack.New(depth),
}
}


// New returns an error with the supplied message.
// New also records the stack trace at the point it was called.
func New(message string) error {
return &fundamental{
msg: message,
msg: message,
CallStack: stack.New(1),
}
}
Expand All @@ -124,7 +123,7 @@ func New(message string) error {
// Errorf also records the stack trace at the point it was called.
func Errorf(format string, args ...interface{}) error {
return &fundamental{
msg: fmt.Sprintf(format, args...),
msg: fmt.Sprintf(format, args...),
CallStack: stack.New(1),
}
}
Expand Down
1 change: 0 additions & 1 deletion testutil/until.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,3 @@ func UntilConnect(t TestingT, a int, d time.Duration, addr string) {
t.Errorf("never connected to TCP server at '%s' after %d attempts", addr, a)
t.FailNow()
}

4 changes: 2 additions & 2 deletions testutil/until_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestUntilPass(t *testing.T) {
if r.Method == http.MethodPost {
// Sleep some rand amount to time to simulate some
// async process happening on the server
time.Sleep(time.Duration(rand.Intn(10))*time.Millisecond)
time.Sleep(time.Duration(rand.Intn(10)) * time.Millisecond)
// Set the value
value = r.FormValue("value")
} else {
Expand Down Expand Up @@ -67,4 +67,4 @@ func TestUntilPass(t *testing.T) {

assert.Equal(t, "batch job completed\n", string(b))
})
}
}

0 comments on commit fced42d

Please sign in to comment.