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:
parent
a933885806
commit
6c469299b6
|
@ -54,10 +54,18 @@ type MessagesRequest struct {
|
||||||
|
|
||||||
func (r *MessagesRequest) setDefaults(now time.Time) {
|
func (r *MessagesRequest) setDefaults(now time.Time) {
|
||||||
// set From and To defaults
|
// set From and To defaults
|
||||||
if r.From == 0 && r.To == 0 {
|
if r.To == 0 {
|
||||||
r.From = uint32(now.UTC().Add(-24 * time.Hour).Unix())
|
|
||||||
r.To = uint32(now.UTC().Unix())
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// -----
|
// -----
|
||||||
|
|
|
@ -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)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
|
@ -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() {
|
func (s *ShhExtSuite) TestRequestMessages() {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue