feat(store): keep an in-memory newest-first message store
Provide a concurrency-safe, per-topic message store with id-based deduplication, delete, clear, and a search predicate.
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
package store
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestPutDedupeOrderDeleteClear(t *testing.T) {
|
||||
s := New()
|
||||
s.Put(Message{ID: "a", Time: 1, Topic: "alerts", Body: "old"})
|
||||
s.Put(Message{ID: "b", Time: 2, Topic: "alerts", Body: "new"})
|
||||
s.Put(Message{ID: "b", Time: 3, Topic: "alerts", Body: "dup"})
|
||||
s.Put(Message{ID: "c", Time: 2, Topic: "backups", Body: "bak"})
|
||||
list := s.List("alerts")
|
||||
if len(list) != 2 || list[0].ID != "b" || list[1].ID != "a" {
|
||||
t.Fatalf("%+v", list)
|
||||
}
|
||||
if got := s.Topics(); len(got) != 2 || got[0] != "alerts" || got[1] != "backups" {
|
||||
t.Fatalf("topics %v", got)
|
||||
}
|
||||
s.Delete("a")
|
||||
if len(s.List("alerts")) != 1 {
|
||||
t.Fatal(s.List("alerts"))
|
||||
}
|
||||
s.Clear("alerts")
|
||||
if len(s.List("alerts")) != 0 || len(s.List("backups")) != 1 {
|
||||
t.Fatal("clear")
|
||||
}
|
||||
if !Match(Message{Body: "Hello Disk"}, "DiskFull", "disk") {
|
||||
t.Fatal("match")
|
||||
}
|
||||
if Match(Message{Body: "Hello"}, "x", "nope") {
|
||||
t.Fatal("unexpected match")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user