- Release removes the held message and queues its deliveries in one transaction - Of several simultaneous approvals one wins, the others get 404 - Extract the thread-root computation and test its linearity without the database (fast under -race)
447 lines
14 KiB
Go
447 lines
14 KiB
Go
package store
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func msg(id, subject, body string) Message {
|
|
return Message{MessageID: id, Subject: subject, Sender: "Alice", Date: time.Unix(1_700_000_000, 0), Body: body}
|
|
}
|
|
|
|
func TestArchivePolicyDefaultsAndUpdate(t *testing.T) {
|
|
s := newTestStore(t)
|
|
l, _ := s.CreateList("dev", "example.org", "Dev")
|
|
if l.ArchivePolicy != ArchivePrivate {
|
|
t.Fatalf("default = %q", l.ArchivePolicy)
|
|
}
|
|
if err := s.SetPolicies(l.ID, PostMembers, JoinOpen, ArchivePublic); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, _ := s.GetList(l.ID)
|
|
if got.ArchivePolicy != ArchivePublic {
|
|
t.Fatalf("policy = %q", got.ArchivePolicy)
|
|
}
|
|
if err := s.SetPolicies(l.ID, PostMembers, JoinOpen, "bogus"); err == nil {
|
|
t.Fatal("invalid archive policy must be rejected")
|
|
}
|
|
}
|
|
|
|
func TestArchiveAndFetch(t *testing.T) {
|
|
s := newTestStore(t)
|
|
l, _ := s.CreateList("dev", "example.org", "Dev")
|
|
m := msg("<1@x>", "Hello", "first body")
|
|
id, created, err := s.ArchiveMessage(l.ID, m)
|
|
if err != nil || !created || id == 0 {
|
|
t.Fatalf("archive: %d %v %v", id, created, err)
|
|
}
|
|
got, err := s.GetMessage(id)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got.Subject != "Hello" || got.Body != "first body" || got.ListID != l.ID || got.ThreadID != id || got.Size != len("first body") {
|
|
t.Fatalf("got %+v", got)
|
|
}
|
|
if !got.Date.Equal(m.Date) {
|
|
t.Fatalf("date = %v", got.Date)
|
|
}
|
|
if _, err := s.GetMessage(999); !errors.Is(err, ErrNotFound) {
|
|
t.Fatalf("want ErrNotFound, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestArchiveDeduplicatesByMessageID(t *testing.T) {
|
|
s := newTestStore(t)
|
|
l, _ := s.CreateList("dev", "example.org", "Dev")
|
|
id1, _, _ := s.ArchiveMessage(l.ID, msg("<1@x>", "Hello", "a"))
|
|
id2, created, err := s.ArchiveMessage(l.ID, msg("<1@x>", "Hello", "a"))
|
|
if err != nil || created || id2 != id1 {
|
|
t.Fatalf("retry must not duplicate: %d %v %v", id2, created, err)
|
|
}
|
|
other, _ := s.CreateList("ops", "example.org", "Ops")
|
|
if _, created, _ := s.ArchiveMessage(other.ID, msg("<1@x>", "Hello", "a")); !created {
|
|
t.Fatal("same Message-ID in another list is a different message")
|
|
}
|
|
}
|
|
|
|
func TestThreading(t *testing.T) {
|
|
s := newTestStore(t)
|
|
l, _ := s.CreateList("dev", "example.org", "Dev")
|
|
root, _, _ := s.ArchiveMessage(l.ID, msg("<root@x>", "Topic", "r"))
|
|
reply := msg("<r1@x>", "Re: Topic", "r1")
|
|
reply.InReplyTo = "<root@x>"
|
|
r1, _, _ := s.ArchiveMessage(l.ID, reply)
|
|
nested := msg("<r2@x>", "Re: Topic", "r2")
|
|
nested.InReplyTo = "<r1@x>"
|
|
r2, _, _ := s.ArchiveMessage(l.ID, nested)
|
|
orphan := msg("<o@x>", "Other", "o")
|
|
orphan.InReplyTo = "<missing@x>"
|
|
o, _, _ := s.ArchiveMessage(l.ID, orphan)
|
|
|
|
for _, id := range []int64{r1, r2} {
|
|
m, _ := s.GetMessage(id)
|
|
if m.ThreadID != root {
|
|
t.Fatalf("message %d thread = %d, want %d", id, m.ThreadID, root)
|
|
}
|
|
}
|
|
if m, _ := s.GetMessage(o); m.ThreadID != o {
|
|
t.Fatalf("orphan must start its own thread, got %d", m.ThreadID)
|
|
}
|
|
th, err := s.ThreadMessages(l.ID, root)
|
|
if err != nil || len(th) != 3 {
|
|
t.Fatalf("thread: %d %v", len(th), err)
|
|
}
|
|
}
|
|
|
|
func TestThreadingIgnoresOtherLists(t *testing.T) {
|
|
s := newTestStore(t)
|
|
a, _ := s.CreateList("a", "example.org", "A")
|
|
b, _ := s.CreateList("b", "example.org", "B")
|
|
s.ArchiveMessage(a.ID, msg("<root@x>", "Topic", "r"))
|
|
reply := msg("<r@x>", "Re: Topic", "r")
|
|
reply.InReplyTo = "<root@x>"
|
|
id, _, _ := s.ArchiveMessage(b.ID, reply)
|
|
if m, _ := s.GetMessage(id); m.ThreadID != id {
|
|
t.Fatal("a parent in another list must not be used")
|
|
}
|
|
}
|
|
|
|
func TestListMessagesNewestFirstWithPaging(t *testing.T) {
|
|
s := newTestStore(t)
|
|
l, _ := s.CreateList("dev", "example.org", "Dev")
|
|
for i := 0; i < 5; i++ {
|
|
m := msg("<m"+string(rune('a'+i))+"@x>", "S"+string(rune('a'+i)), "body")
|
|
m.Date = time.Unix(1_700_000_000+int64(i), 0)
|
|
s.ArchiveMessage(l.ID, m)
|
|
}
|
|
got, total, err := s.ListMessages(l.ID, 2, 0)
|
|
if err != nil || total != 5 || len(got) != 2 {
|
|
t.Fatalf("page 1: %d %d %v", len(got), total, err)
|
|
}
|
|
if got[0].Subject != "Se" || got[1].Subject != "Sd" {
|
|
t.Fatalf("order: %s %s", got[0].Subject, got[1].Subject)
|
|
}
|
|
if got[0].Body != "" {
|
|
t.Fatal("listings must not load bodies")
|
|
}
|
|
got, _, _ = s.ListMessages(l.ID, 2, 4)
|
|
if len(got) != 1 || got[0].Subject != "Sa" {
|
|
t.Fatalf("last page: %+v", got)
|
|
}
|
|
}
|
|
|
|
func TestSearch(t *testing.T) {
|
|
s := newTestStore(t)
|
|
l, _ := s.CreateList("dev", "example.org", "Dev")
|
|
other, _ := s.CreateList("ops", "example.org", "Ops")
|
|
s.ArchiveMessage(l.ID, msg("<1@x>", "Deploy failed", "the kubernetes rollout broke"))
|
|
s.ArchiveMessage(l.ID, msg("<2@x>", "Lunch", "pizza on friday"))
|
|
s.ArchiveMessage(other.ID, msg("<3@x>", "Deploy ok", "kubernetes is fine here"))
|
|
|
|
got, total, err := s.SearchMessages(l.ID, "kubernetes", 10, 0)
|
|
if err != nil || total != 1 || len(got) != 1 || got[0].Subject != "Deploy failed" {
|
|
t.Fatalf("search: %+v %d %v", got, total, err)
|
|
}
|
|
// Subject, sender and multiple terms (AND).
|
|
if got, _, _ := s.SearchMessages(l.ID, "lunch pizza", 10, 0); len(got) != 1 {
|
|
t.Fatalf("subject+body: %+v", got)
|
|
}
|
|
if got, _, _ := s.SearchMessages(l.ID, "alice", 10, 0); len(got) != 2 {
|
|
t.Fatalf("sender: %+v", got)
|
|
}
|
|
if got, _, _ := s.SearchMessages(l.ID, "lunch kubernetes", 10, 0); len(got) != 0 {
|
|
t.Fatalf("AND semantics: %+v", got)
|
|
}
|
|
}
|
|
|
|
func TestSearchIsSafeAgainstSyntax(t *testing.T) {
|
|
s := newTestStore(t)
|
|
l, _ := s.CreateList("dev", "example.org", "Dev")
|
|
s.ArchiveMessage(l.ID, msg("<1@x>", "Hello", "world"))
|
|
for _, q := range []string{`"`, `foo AND`, `NEAR(`, `*`, `a OR b`, `col:x`, `)(`, "", " "} {
|
|
if _, _, err := s.SearchMessages(l.ID, q, 10, 0); err != nil {
|
|
t.Errorf("query %q must not error: %v", q, err)
|
|
}
|
|
}
|
|
// FTS operators are plain words: "world OR zzz" needs both terms.
|
|
if got, _, _ := s.SearchMessages(l.ID, "world OR zzz", 10, 0); len(got) != 0 {
|
|
t.Fatalf("operators must be literal: %+v", got)
|
|
}
|
|
}
|
|
|
|
func TestDeleteMessageRemovesFromSearch(t *testing.T) {
|
|
s := newTestStore(t)
|
|
l, _ := s.CreateList("dev", "example.org", "Dev")
|
|
id, _, _ := s.ArchiveMessage(l.ID, msg("<1@x>", "Secret", "leaked password"))
|
|
if err := s.DeleteMessage(id); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got, _, _ := s.SearchMessages(l.ID, "password", 10, 0); len(got) != 0 {
|
|
t.Fatalf("deleted message still searchable: %+v", got)
|
|
}
|
|
if err := s.DeleteMessage(id); !errors.Is(err, ErrNotFound) {
|
|
t.Fatalf("want ErrNotFound, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestDeleteListCleansArchiveAndIndex(t *testing.T) {
|
|
s := newTestStore(t)
|
|
l, _ := s.CreateList("dev", "example.org", "Dev")
|
|
s.ArchiveMessage(l.ID, msg("<1@x>", "Hello", "uniqueword"))
|
|
if err := s.DeleteList(l.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var n int
|
|
s.db.QueryRow(`SELECT COUNT(*) FROM archive_fts WHERE archive_fts MATCH 'uniqueword'`).Scan(&n)
|
|
if n != 0 {
|
|
t.Fatal("search index must follow cascade deletes")
|
|
}
|
|
}
|
|
|
|
func TestMigrationAddsArchiveColumn(t *testing.T) {
|
|
// A database from milestone 2 has no archive_policy column.
|
|
path := t.TempDir() + "/m2.db"
|
|
s, _ := Open(path)
|
|
s.CreateList("dev", "example.org", "Dev")
|
|
s.db.Exec(`ALTER TABLE lists DROP COLUMN archive_policy`)
|
|
s.Close()
|
|
s2, err := Open(path)
|
|
if err != nil {
|
|
t.Fatalf("migrate: %v", err)
|
|
}
|
|
defer s2.Close()
|
|
l, err := s2.FindList("dev@example.org")
|
|
if err != nil || l.ArchivePolicy != ArchivePrivate {
|
|
t.Fatalf("list: %+v %v", l, err)
|
|
}
|
|
}
|
|
|
|
func TestLongBodyIsStoredAsIs(t *testing.T) {
|
|
s := newTestStore(t)
|
|
l, _ := s.CreateList("dev", "example.org", "Dev")
|
|
body := strings.Repeat("x", 100_000)
|
|
id, _, _ := s.ArchiveMessage(l.ID, msg("<big@x>", "Big", body))
|
|
if got, _ := s.GetMessage(id); len(got.Body) != len(body) {
|
|
t.Fatalf("body len %d", len(got.Body))
|
|
}
|
|
}
|
|
|
|
func TestArchiveBatchInsertsDeduplicatesAndThreads(t *testing.T) {
|
|
s := newTestStore(t)
|
|
l, _ := s.CreateList("dev", "example.org", "Dev")
|
|
s.ArchiveMessage(l.ID, msg("<old@x>", "Already here", "x"))
|
|
|
|
reply := msg("<r@x>", "Re: Topic", "reply")
|
|
reply.InReplyTo = "<root@x>"
|
|
res, err := s.ArchiveBatch(l.ID, []Message{
|
|
msg("<root@x>", "Topic", "root"),
|
|
reply,
|
|
msg("<old@x>", "Already here", "x"), // duplicate of an existing message
|
|
msg("<root@x>", "Topic", "root"), // duplicate inside the batch
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(res) != 4 || !res[0].Created || !res[1].Created || res[2].Created || res[3].Created {
|
|
t.Fatalf("results: %+v", res)
|
|
}
|
|
if res[3].ID != res[0].ID {
|
|
t.Fatalf("a duplicate reports the id of the stored message: %+v", res)
|
|
}
|
|
r, _ := s.GetMessage(res[1].ID)
|
|
if r.ThreadID != res[0].ID {
|
|
t.Fatalf("threading inside a batch: thread %d, root %d", r.ThreadID, res[0].ID)
|
|
}
|
|
if _, total, _ := s.ListMessages(l.ID, 10, 0); total != 3 {
|
|
t.Fatalf("total = %d", total)
|
|
}
|
|
if got, _, _ := s.SearchMessages(l.ID, "reply", 10, 0); len(got) != 1 {
|
|
t.Fatal("batch inserts must be searchable")
|
|
}
|
|
}
|
|
|
|
func TestArchiveBatchIsAtomic(t *testing.T) {
|
|
s := newTestStore(t)
|
|
l, _ := s.CreateList("dev", "example.org", "Dev")
|
|
_, err := s.ArchiveBatch(l.ID, []Message{msg("<a@x>", "A", "a"), {Subject: "no id"}})
|
|
if err == nil {
|
|
t.Fatal("a message without an id must fail the batch")
|
|
}
|
|
if _, total, _ := s.ListMessages(l.ID, 10, 0); total != 0 {
|
|
t.Fatalf("nothing may be stored after a failed batch, got %d", total)
|
|
}
|
|
if res, err := s.ArchiveBatch(l.ID, nil); err != nil || len(res) != 0 {
|
|
t.Fatalf("empty batch: %v %v", res, err)
|
|
}
|
|
}
|
|
|
|
func threadOf(t *testing.T, s *Store, id int64) int64 {
|
|
t.Helper()
|
|
m, err := s.GetMessage(id)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return m.ThreadID
|
|
}
|
|
|
|
func TestRethreadFixesMessagesImportedOutOfOrder(t *testing.T) {
|
|
s := newTestStore(t)
|
|
l, _ := s.CreateList("dev", "example.org", "Dev")
|
|
// Children first, root last: at insertion time none of the parents exists.
|
|
c2 := msg("<c2@x>", "Re: Re: T", "c2")
|
|
c2.InReplyTo = "<c1@x>"
|
|
c1 := msg("<c1@x>", "Re: T", "c1")
|
|
c1.InReplyTo = "<root@x>"
|
|
id2, _, _ := s.ArchiveMessage(l.ID, c2)
|
|
id1, _, _ := s.ArchiveMessage(l.ID, c1)
|
|
root, _, _ := s.ArchiveMessage(l.ID, msg("<root@x>", "T", "root"))
|
|
other, _, _ := s.ArchiveMessage(l.ID, msg("<other@x>", "Other", "o"))
|
|
if threadOf(t, s, id1) == root {
|
|
t.Fatal("precondition: the thread is broken before rethreading")
|
|
}
|
|
|
|
changed, err := s.Rethread(l.ID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if changed != 2 {
|
|
t.Fatalf("changed = %d, want the 2 broken messages", changed)
|
|
}
|
|
for _, id := range []int64{id1, id2, root} {
|
|
if got := threadOf(t, s, id); got != root {
|
|
t.Fatalf("message %d thread = %d, want %d", id, got, root)
|
|
}
|
|
}
|
|
if threadOf(t, s, other) != other {
|
|
t.Fatal("an unrelated message keeps its own thread")
|
|
}
|
|
if again, _ := s.Rethread(l.ID); again != 0 {
|
|
t.Fatalf("rethreading twice changes nothing, got %d", again)
|
|
}
|
|
if th, _ := s.ThreadMessages(l.ID, root); len(th) != 3 {
|
|
t.Fatalf("thread has %d messages", len(th))
|
|
}
|
|
}
|
|
|
|
func TestRethreadSurvivesCyclesAndSelfReplies(t *testing.T) {
|
|
s := newTestStore(t)
|
|
l, _ := s.CreateList("dev", "example.org", "Dev")
|
|
a := msg("<a@x>", "A", "a")
|
|
a.InReplyTo = "<b@x>"
|
|
b := msg("<b@x>", "B", "b")
|
|
b.InReplyTo = "<a@x>"
|
|
self := msg("<s@x>", "Self", "s")
|
|
self.InReplyTo = "<s@x>"
|
|
ia, _, _ := s.ArchiveMessage(l.ID, a)
|
|
ib, _, _ := s.ArchiveMessage(l.ID, b)
|
|
is, _, _ := s.ArchiveMessage(l.ID, self)
|
|
|
|
done := make(chan error, 1)
|
|
go func() { _, err := s.Rethread(l.ID); done <- err }()
|
|
select {
|
|
case err := <-done:
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
case <-time.After(3 * time.Second):
|
|
t.Fatal("Rethread looped forever on a cycle")
|
|
}
|
|
if threadOf(t, s, ia) != threadOf(t, s, ib) {
|
|
t.Fatal("the two messages of a cycle share a thread")
|
|
}
|
|
if got := threadOf(t, s, ia); got != ia {
|
|
t.Fatalf("the cycle's thread is its smallest id (%d), got %d", ia, got)
|
|
}
|
|
if threadOf(t, s, is) != is {
|
|
t.Fatal("a message that replies to itself is its own thread")
|
|
}
|
|
}
|
|
|
|
func TestRethreadOnlyTouchesTheGivenList(t *testing.T) {
|
|
s := newTestStore(t)
|
|
a, _ := s.CreateList("a", "example.org", "A")
|
|
b, _ := s.CreateList("b", "example.org", "B")
|
|
child := msg("<c@x>", "Re", "c")
|
|
child.InReplyTo = "<p@x>"
|
|
cb, _, _ := s.ArchiveMessage(b.ID, child)
|
|
s.ArchiveMessage(b.ID, msg("<p@x>", "P", "p"))
|
|
// A parent with the same Message-ID in another list must not be used.
|
|
s.ArchiveMessage(a.ID, msg("<p@x>", "P in a", "p"))
|
|
|
|
if n, _ := s.Rethread(a.ID); n != 0 {
|
|
t.Fatalf("list a has nothing to fix, changed %d", n)
|
|
}
|
|
if threadOf(t, s, cb) != cb {
|
|
t.Fatal("list b must be untouched by rethreading list a")
|
|
}
|
|
if n, _ := s.Rethread(b.ID); n != 1 {
|
|
t.Fatalf("list b: changed %d", n)
|
|
}
|
|
if threadOf(t, s, cb) == cb {
|
|
t.Fatal("list b's child must now follow its parent")
|
|
}
|
|
}
|
|
|
|
func TestRethreadCycleRootIsTheSmallestIdEvenWhenEnteredFromElsewhere(t *testing.T) {
|
|
s := newTestStore(t)
|
|
l, _ := s.CreateList("dev", "example.org", "Dev")
|
|
// ids: x=1 replies to c; b=2 replies to c; c=3 replies to b. The cycle is {b, c}, entered from x at c,
|
|
// which is not its smallest id.
|
|
x := msg("<x@x>", "X", "x")
|
|
x.InReplyTo = "<c@x>"
|
|
b := msg("<b@x>", "B", "b")
|
|
b.InReplyTo = "<c@x>"
|
|
c := msg("<c@x>", "C", "c")
|
|
c.InReplyTo = "<b@x>"
|
|
ix, _, _ := s.ArchiveMessage(l.ID, x)
|
|
ib, _, _ := s.ArchiveMessage(l.ID, b)
|
|
ic, _, _ := s.ArchiveMessage(l.ID, c)
|
|
if _, err := s.Rethread(l.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := min(ib, ic)
|
|
for _, id := range []int64{ix, ib, ic} {
|
|
if got := threadOf(t, s, id); got != want {
|
|
t.Fatalf("message %d: thread %d, want %d (the smallest id of the cycle)", id, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestThreadRootsIsLinearOnLongChains(t *testing.T) {
|
|
const n = 300_000
|
|
ids := make([]int64, n)
|
|
parent := make(map[int64]int64, n)
|
|
for i := range ids {
|
|
ids[i] = int64(i + 1)
|
|
if i > 0 {
|
|
parent[int64(i+1)] = int64(i)
|
|
}
|
|
}
|
|
done := make(chan map[int64]int64, 1)
|
|
go func() { done <- threadRoots(ids, parent) }()
|
|
select {
|
|
case roots := <-done:
|
|
if roots[n] != 1 || roots[1] != 1 || roots[n/2] != 1 {
|
|
t.Fatalf("everything is in the thread of message 1: %d %d %d", roots[n], roots[1], roots[n/2])
|
|
}
|
|
case <-time.After(5 * time.Second):
|
|
t.Fatal("threadRoots is not linear: a 300000-message chain took more than 5 seconds")
|
|
}
|
|
}
|
|
|
|
func TestThreadRootsFromTheNewestEndOfAChain(t *testing.T) {
|
|
// Visiting the newest message first must give the same roots as visiting the oldest first.
|
|
parent := map[int64]int64{2: 1, 3: 2, 4: 3, 6: 5}
|
|
forward := threadRoots([]int64{1, 2, 3, 4, 5, 6}, parent)
|
|
backward := threadRoots([]int64{6, 5, 4, 3, 2, 1}, parent)
|
|
for _, id := range []int64{1, 2, 3, 4, 5, 6} {
|
|
if forward[id] != backward[id] {
|
|
t.Fatalf("order dependent: %d -> %d vs %d", id, forward[id], backward[id])
|
|
}
|
|
}
|
|
if forward[4] != 1 || forward[6] != 5 || forward[5] != 5 {
|
|
t.Fatalf("roots: %v", forward)
|
|
}
|
|
}
|