95 lines
4.5 KiB
Go
95 lines
4.5 KiB
Go
package domain
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func validScheduledJobInput() ScheduledJobInput {
|
|
return ScheduledJobInput{
|
|
Kind: "invitation.response_deadline",
|
|
Key: "0192f0c4-7d4e-7a3b-9c1d-2e3f4a5b6c7d",
|
|
DueAt: time.Date(2026, 10, 13, 9, 0, 0, 123456789, time.FixedZone("MSK", 3*60*60)),
|
|
Payload: []byte(` { "invitation_id" : "0192f0c4-7d4e-7a3b-9c1d-2e3f4a5b6c7d", "version": 2 } `),
|
|
}
|
|
}
|
|
|
|
func TestNewScheduledJobNormalizesValidInput(t *testing.T) {
|
|
input := validScheduledJobInput()
|
|
job, err := NewScheduledJob(input)
|
|
if err != nil {
|
|
t.Fatalf("NewScheduledJob() error = %v", err)
|
|
}
|
|
if job.Kind() != input.Kind || job.Key() != input.Key || job.MaxAttempts() != 0 {
|
|
t.Fatalf("NewScheduledJob() did not keep the identity: %+v", job)
|
|
}
|
|
// PostgreSQL keeps microseconds, so a stored job compares equal.
|
|
if want := time.Date(2026, 10, 13, 6, 0, 0, 123456000, time.UTC); !job.DueAt().Equal(want) || job.DueAt().Location() != time.UTC {
|
|
t.Fatalf("DueAt() = %s, want %s in UTC", job.DueAt(), want)
|
|
}
|
|
if want := `{"invitation_id":"0192f0c4-7d4e-7a3b-9c1d-2e3f4a5b6c7d","version":2}`; string(job.Payload()) != want {
|
|
t.Fatalf("Payload() = %s, want %s", job.Payload(), want)
|
|
}
|
|
job.Payload()[0] = '['
|
|
if job.Payload()[0] != '{' {
|
|
t.Fatal("Payload() exposed the job's internal buffer")
|
|
}
|
|
|
|
input.Payload = nil
|
|
input.MaxAttempts = MaxScheduledJobAttempts
|
|
job, err = NewScheduledJob(input)
|
|
if err != nil || string(job.Payload()) != "{}" || job.MaxAttempts() != MaxScheduledJobAttempts {
|
|
t.Fatalf("NewScheduledJob() without a payload = %s, %d, %v; want {}", job.Payload(), job.MaxAttempts(), err)
|
|
}
|
|
}
|
|
|
|
func TestNewScheduledJobRejectsInvalidInput(t *testing.T) {
|
|
prefix, suffix := `{"padding":"`, `"}`
|
|
tests := []struct {
|
|
name string
|
|
change func(*ScheduledJobInput)
|
|
}{
|
|
{name: "empty kind", change: func(input *ScheduledJobInput) { input.Kind = "" }},
|
|
{name: "uppercase kind", change: func(input *ScheduledJobInput) { input.Kind = "Invitation.deadline" }},
|
|
{name: "empty kind segment", change: func(input *ScheduledJobInput) { input.Kind = "invitation..deadline" }},
|
|
{name: "long kind", change: func(input *ScheduledJobInput) { input.Kind = strings.Repeat("k", 65) }},
|
|
{name: "empty key", change: func(input *ScheduledJobInput) { input.Key = "" }},
|
|
{name: "email as key", change: func(input *ScheduledJobInput) { input.Key = "person@example.com" }},
|
|
{name: "key with a space", change: func(input *ScheduledJobInput) { input.Key = "a b" }},
|
|
{name: "long key", change: func(input *ScheduledJobInput) { input.Key = strings.Repeat("k", 129) }},
|
|
{name: "zero due time", change: func(input *ScheduledJobInput) { input.DueAt = time.Time{} }},
|
|
{name: "negative attempts", change: func(input *ScheduledJobInput) { input.MaxAttempts = -1 }},
|
|
{name: "too many attempts", change: func(input *ScheduledJobInput) { input.MaxAttempts = MaxScheduledJobAttempts + 1 }},
|
|
{name: "array payload", change: func(input *ScheduledJobInput) { input.Payload = []byte(`["id"]`) }},
|
|
{name: "null payload", change: func(input *ScheduledJobInput) { input.Payload = []byte(`null`) }},
|
|
{name: "nested payload", change: func(input *ScheduledJobInput) { input.Payload = []byte(`{"user":{"id":"1"}}`) }},
|
|
{name: "invalid JSON", change: func(input *ScheduledJobInput) { input.Payload = []byte(`{"id":`) }},
|
|
{name: "invalid UTF-8", change: func(input *ScheduledJobInput) { input.Payload = []byte("{\"id\":\"\xff\"}") }},
|
|
{name: "NUL in a value", change: func(input *ScheduledJobInput) { input.Payload = []byte(`{"id":"a\u0000b"}`) }},
|
|
{name: "number out of range", change: func(input *ScheduledJobInput) { input.Payload = []byte(`{"n":1e400}`) }},
|
|
{name: "payload over the limit", change: func(input *ScheduledJobInput) {
|
|
input.Payload = []byte(prefix + strings.Repeat("a", MaxScheduledJobPayloadBytes-len(prefix)-len(suffix)+1) + suffix)
|
|
}},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
input := validScheduledJobInput()
|
|
test.change(&input)
|
|
if _, err := NewScheduledJob(input); !errors.Is(err, ErrInvalidScheduledJob) {
|
|
t.Fatalf("NewScheduledJob() error = %v, want ErrInvalidScheduledJob", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestNewScheduledJobAcceptsPayloadAtSizeLimit(t *testing.T) {
|
|
input := validScheduledJobInput()
|
|
prefix, suffix := `{"padding":"`, `"}`
|
|
input.Payload = []byte(prefix + strings.Repeat("a", MaxScheduledJobPayloadBytes-len(prefix)-len(suffix)) + suffix)
|
|
if _, err := NewScheduledJob(input); err != nil {
|
|
t.Fatalf("NewScheduledJob() rejected a payload at the limit: %v", err)
|
|
}
|
|
}
|