fix default range value for shhext_requestMessages (#934)

* fix default range value for shhext_requestMessages

* use t.Run to run scenarions

* fix case when To - 24 hours is negative
This commit is contained in:
Andrea Franz 2018-05-10 16:47:54 +02:00 committed by GitHub
parent a933885806
commit 6c469299b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 9 deletions

View File

@ -54,10 +54,18 @@ type MessagesRequest struct {
func (r *MessagesRequest) setDefaults(now time.Time) {
// set From and To defaults
if r.From == 0 && r.To == 0 {
r.From = uint32(now.UTC().Add(-24 * time.Hour).Unix())
if r.To == 0 {
r.To = uint32(now.UTC().Unix())
}
if r.From == 0 {
oneDay := uint32(86400) // -24 hours
if r.To < oneDay {
r.From = 0
} else {
r.From = r.To - oneDay
}
}
}
// -----

View File

@ -0,0 +1,49 @@
package shhext
import (
"fmt"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestMessagesRequest_setDefaults(t *testing.T) {
daysAgo := func(now time.Time, days int) uint32 {
return uint32(now.UTC().Add(-24 * time.Hour * time.Duration(days)).Unix())
}
tnow := time.Now()
now := uint32(tnow.UTC().Unix())
yesterday := daysAgo(tnow, 1)
scenarios := []struct {
given *MessagesRequest
expected *MessagesRequest
}{
{
&MessagesRequest{From: 0, To: 0},
&MessagesRequest{From: yesterday, To: now},
},
{
&MessagesRequest{From: 1, To: 0},
&MessagesRequest{From: uint32(1), To: now},
},
{
&MessagesRequest{From: 0, To: yesterday},
&MessagesRequest{From: daysAgo(tnow, 2), To: yesterday},
},
// 100 - 1 day would be invalid, so we set From to 0
{
&MessagesRequest{From: 0, To: 100},
&MessagesRequest{From: 0, To: 100},
},
}
for i, s := range scenarios {
t.Run(fmt.Sprintf("Scenario %d", i), func(t *testing.T) {
s.given.setDefaults(tnow)
require.Equal(t, s.expected, s.given)
})
}
}

View File

@ -128,13 +128,6 @@ func (s *ShhExtSuite) TestWaitMessageExpired() {
}
}
func (s *ShhExtSuite) TestRequestMessagesDefaults() {
r := MessagesRequest{}
r.setDefaults(time.Now())
s.NotZero(r.From)
s.InEpsilon(uint32(time.Now().UTC().Unix()), r.To, 1.0)
}
func (s *ShhExtSuite) TestRequestMessages() {
var err error