Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: watcher missconfiguration checks #23

Merged
merged 2 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.local
**/test_results
.vscode
10 changes: 6 additions & 4 deletions massifs/watcher/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,17 @@ func (w Watcher) ConfigString() string {
}

func ConfigDefaults(cfg *WatchConfig) error {
if cfg.Since.UnixMilli() == 0 || cfg.IDSince != "" && cfg.Horizon != 0 {
zeroTime := time.Time{}
if cfg.Since.UnixMilli() == zeroTime.UnixMilli() && cfg.IDSince == "" && cfg.Horizon == 0 {
return fmt.Errorf("provide horizon on its own or either of the since parameters.")

}
// If horizon is provided, the since values are derived

if cfg.Interval == 0 {
cfg.Interval = DefaultInterval
}

var sinceUnset bool
if cfg.Horizon == 0 {
// temporarily force a horizon
cfg.Horizon = time.Second * 30
Expand All @@ -80,9 +81,10 @@ func ConfigDefaults(cfg *WatchConfig) error {
// since defaults to now (but will get trumped by horizon if that was provided)
if cfg.Since.UnixMilli() == 0 {
cfg.Since = time.Now()
sinceUnset = true
}
// horizon trumps since
if cfg.Horizon > 0 {
// explicit horizon trumps since, and the default horizon trumps the default since
if cfg.Horizon > 0 && sinceUnset {
cfg.Since = time.Now().Add(-cfg.Horizon)
}
if cfg.IDSince == "" {
Expand Down
77 changes: 77 additions & 0 deletions massifs/watcher/watcher_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package watcher

import (
"strings"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

type checkWatchConfig func(t *testing.T, cfg WatchConfig)

func TestConfigDefaults(t *testing.T) {
hourSince := time.Now().Add(-time.Hour)

type args struct {
cfg *WatchConfig
}
tests := []struct {
name string
args args
errPrefix string
check checkWatchConfig
}{
{
name: "horizon or since options are required",
args: args{
cfg: &WatchConfig{},
},
errPrefix: "provide horizon on its own or either of the since",
},

{
name: "poll with since an hour in the past",
args: args{
cfg: &WatchConfig{
Since: hourSince,
},
},
check: func(t *testing.T, cfg WatchConfig) {
assert.Equal(t, hourSince, cfg.Since)
assert.Equal(t, time.Second, cfg.Interval)
assert.NotEqual(t, "", cfg.IDSince) // should be set to IDTimeHex
},
},

{
name: "bad hex string for idtimestamp errors",
args: args{
cfg: &WatchConfig{
IDSince: "thisisnothex",
},
},
errPrefix: "encoding/hex: invalid byte",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ConfigDefaults(tt.args.cfg)
if err != nil {
if tt.errPrefix == "" {
t.Errorf("NewWatchConfig() unexpected error = %v", err)
}
if !strings.HasPrefix(err.Error(), tt.errPrefix) {
t.Errorf("NewWatchConfig() unexpected error = %v, expected prefix: %s", err, tt.errPrefix)
}
} else {
if tt.errPrefix != "" {
t.Errorf("NewWatchConfig() expected error prefix = %s", tt.errPrefix)
}
}
if tt.check != nil {
tt.check(t, *tt.args.cfg)
}
})
}
}
Loading