Skip to content

Commit

Permalink
all: add tests, imp code
Browse files Browse the repository at this point in the history
  • Loading branch information
ainar-g committed Jan 15, 2025
1 parent d4c956e commit 934e92f
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 2 deletions.
2 changes: 1 addition & 1 deletion contextutil/contextutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"time"
)

// Constructor is an interface for constructing interfaces with deadlines, e.g.
// Constructor is an interface for constructing contexts with deadlines, e.g.
// for request contexts.
type Constructor interface {
// New returns a new context based on parent as well as a cancel function.
Expand Down
24 changes: 24 additions & 0 deletions contextutil/contextutil_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package contextutil_test

import (
"context"
"testing"
"time"

"github.com/AdguardTeam/golibs/contextutil"
"github.com/stretchr/testify/assert"
)

func TestTimeoutConstructor(t *testing.T) {
const timeout = 1 * time.Minute

c := contextutil.NewTimeoutConstructor(timeout)
ctx, cancel := c.New(context.Background())
defer cancel()

dl, ok := ctx.Deadline()
assert.True(t, ok)

d := time.Until(dl)
assert.InDelta(t, timeout, d, float64(1*time.Second))
}
5 changes: 5 additions & 0 deletions testutil/sentrytest/sentrytest.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Package sentrytest contains fake implementations of interfaces for the Sentry
// module.
//
// TODO(a.garipov): Add more utilities or rename to fakesentry.
package sentrytest

import (
Expand Down Expand Up @@ -43,6 +45,9 @@ func (t *Transport) SendEvent(e *sentry.Event) {
// NewTransport returns a new *Transport all methods of which panic.
func NewTransport() (tst *Transport) {
return &Transport{
OnClose: func() {
panic(fmt.Errorf("unexpected call to sentrytest.(*Transport).Close()"))
},
OnConfigure: func(opts sentry.ClientOptions) {
panic(fmt.Errorf("unexpected call to sentrytest.(*Transport).Configure(%v)", opts))
},
Expand Down
2 changes: 1 addition & 1 deletion timeutil/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ var _ Schedule = (*CronSchedule)(nil)

// UntilNext implements the [Schedule] interface for *CronSchedule.
func (s *CronSchedule) UntilNext(now time.Time) (d time.Duration) {
return max(time.Until(s.cron.Next(now)), 0)
return max(s.cron.Next(now).Sub(now), 0)
}

// RandomizedSchedule adds a random duration to the result of the [Schedule]
Expand Down
52 changes: 52 additions & 0 deletions timeutil/schedule_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package timeutil_test

import (
"math/rand/v2"
"testing"
"time"

"github.com/AdguardTeam/golibs/timeutil"
"github.com/robfig/cron/v3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestConstSchedule(t *testing.T) {
const ivl = 5 * time.Minute
s := timeutil.NewConstSchedule(ivl)

now := time.Now()
d := s.UntilNext(now)
assert.Equal(t, ivl, d)
}

func TestCronSchedule(t *testing.T) {
p := cron.NewParser(cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow)
cs, err := p.Parse("*/5 * * * *")
require.NoError(t, err)

s := timeutil.NewCronSchedule(cs)

now := time.Now()
for range 100_000 {
d := s.UntilNext(now)
assert.InDelta(t, 5*time.Minute, d, float64(5*time.Minute))
}
}

func TestRandomizedSchedule(t *testing.T) {
const (
ivl = 5 * time.Minute
minAdd = -1 * time.Minute
maxAdd = 1 * time.Minute
)

now := time.Now()
r := rand.New(rand.NewPCG(uint64(now.Unix()), 0))
s := timeutil.NewRandomizedSchedule(timeutil.NewConstSchedule(ivl), r, minAdd, maxAdd)

for range 100_000 {
d := s.UntilNext(now)
assert.InDelta(t, ivl, d, float64(2*time.Minute))
}
}

0 comments on commit 934e92f

Please sign in to comment.