- Add members.bounce_at (unix time of the last counted bounce) with an additive migration; existing counts get 0, so they are treated as old. - RecordHardBounce restarts the count at 1 when the last bounce is older than BounceWindow (30 days); a disabled member stays disabled. - Tests for the window boundary, the restart and the migration of an old database.
76 lines
2.2 KiB
Go
76 lines
2.2 KiB
Go
package store
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestHardBouncesCountOnlyWithinTheWindow(t *testing.T) {
|
|
s := newTestStore(t)
|
|
now := time.Unix(1_900_000_000, 0)
|
|
s.Now = func() time.Time { return now }
|
|
l, _ := s.CreateList("dev", "example.org", "Dev")
|
|
s.AddMember(l.ID, "a@x.com", "")
|
|
ms, _ := s.Members(l.ID)
|
|
id := ms[0].ID
|
|
count := func() int {
|
|
t.Helper()
|
|
m, err := s.MemberByID(id)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return m.BounceCount
|
|
}
|
|
|
|
s.RecordHardBounce(id, 3)
|
|
now = now.Add(BounceWindow) // exactly the window: still recent
|
|
s.RecordHardBounce(id, 3)
|
|
if n := count(); n != 2 {
|
|
t.Fatalf("a bounce within the window adds up: %d", n)
|
|
}
|
|
now = now.Add(BounceWindow + time.Second) // the last bounce is now too old
|
|
if disabled, err := s.RecordHardBounce(id, 3); disabled || err != nil {
|
|
t.Fatalf("an old count must not disable: %v %v", disabled, err)
|
|
}
|
|
if n := count(); n != 1 {
|
|
t.Fatalf("a bounce after a quiet window restarts the count: %d", n)
|
|
}
|
|
now = now.Add(time.Hour)
|
|
s.RecordHardBounce(id, 3)
|
|
if disabled, _ := s.RecordHardBounce(id, 3); !disabled {
|
|
t.Fatal("three recent bounces disable")
|
|
}
|
|
// A disabled member stays disabled, even when a late bounce restarts the count.
|
|
now = now.Add(2 * BounceWindow)
|
|
if disabled, _ := s.RecordHardBounce(id, 3); !disabled {
|
|
t.Fatal("only re-adding re-enables a member")
|
|
}
|
|
}
|
|
|
|
func TestMigrationAddsTheBounceTimeColumn(t *testing.T) {
|
|
path := t.TempDir() + "/old.db"
|
|
s, _ := Open(path)
|
|
l, _ := s.CreateList("dev", "example.org", "Dev")
|
|
s.AddMember(l.ID, "a@x.com", "")
|
|
s.db.Exec(`UPDATE members SET bounce_count=2`)
|
|
if _, err := s.db.Exec(`ALTER TABLE members DROP COLUMN bounce_at`); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s.Close()
|
|
s2, err := Open(path)
|
|
if err != nil {
|
|
t.Fatalf("migrate: %v", err)
|
|
}
|
|
defer s2.Close()
|
|
ms, err := s2.Members(l.ID)
|
|
if err != nil || len(ms) != 1 || ms[0].BounceCount != 2 {
|
|
t.Fatalf("members survive the migration: %+v %v", ms, err)
|
|
}
|
|
// An old count has no date: it is older than the window, so the next bounce restarts it.
|
|
if disabled, err := s2.RecordHardBounce(ms[0].ID, 3); disabled || err != nil {
|
|
t.Fatalf("%v %v", disabled, err)
|
|
}
|
|
if m, _ := s2.MemberByID(ms[0].ID); m.BounceCount != 1 {
|
|
t.Fatalf("count: %d", m.BounceCount)
|
|
}
|
|
}
|