85 lines
2.5 KiB
Go
85 lines
2.5 KiB
Go
package metrics
|
|
|
|
import (
|
|
"context"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"git.asio.asia/nixevol/NixMsg/internal/store"
|
|
)
|
|
|
|
func TestSampleStoreGaugesPendingNonZero(t *testing.T) {
|
|
t.Parallel()
|
|
dir := t.TempDir()
|
|
db, err := store.Open(filepath.Join(dir, "data"), "FULL")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = db.Close() })
|
|
|
|
ctx := context.Background()
|
|
_, err = db.Write.ExecContext(ctx, `
|
|
INSERT INTO endpoints(id, name, login_hash, enabled, source, created_at)
|
|
VALUES ('alice', 'A', 'x', 1, 'admin', 1)`)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err = db.Write.ExecContext(ctx, `
|
|
INSERT INTO messages(sender_id, id, dest_kind, dest_id, send_at, keep, ttl_seconds, receipt, state, reason, created_at, content_type, body_enc, meta)
|
|
VALUES ('alice', 'm1', 'endpoint', 'bob', 100, 1, 0, 0, 'dispatched', '', 1, 'text/plain', 'utf8', '{}')`)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var seq int64
|
|
err = db.Write.QueryRowContext(ctx, `SELECT seq FROM messages WHERE id='m1'`).Scan(&seq)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err = db.Write.ExecContext(ctx, `
|
|
INSERT INTO deliveries(seq, endpoint_id, send_at, keep, state, reason, attempts, updated_at)
|
|
VALUES (?, 'bob', 100, 1, 'pending', '', 0, 1)`, seq)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err = db.Write.ExecContext(ctx, `
|
|
INSERT INTO messages(sender_id, id, dest_kind, dest_id, send_at, keep, ttl_seconds, receipt, state, reason, created_at, content_type, body_enc, meta)
|
|
VALUES ('alice', 'm2', 'endpoint', 'bob', 999999, 0, 0, 0, 'scheduled', '', 1, 'text/plain', 'utf8', '{}')`)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
reg := New()
|
|
err = SampleStoreGauges(ctx, reg, db.Read)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
SampleQueues(reg, 3, 1)
|
|
|
|
mfs, err := reg.Gatherer().Gather()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got := map[string]float64{}
|
|
for _, mf := range mfs {
|
|
switch mf.GetName() {
|
|
case "nixmsg_endpoints", "nixmsg_deliveries_pending", "nixmsg_messages_scheduled",
|
|
"nixmsg_write_queue_length", "nixmsg_password_hash_queue_length":
|
|
if len(mf.GetMetric()) > 0 {
|
|
got[mf.GetName()] = mf.GetMetric()[0].GetGauge().GetValue()
|
|
}
|
|
}
|
|
}
|
|
if got["nixmsg_endpoints"] != 1 {
|
|
t.Fatalf("endpoints=%v", got["nixmsg_endpoints"])
|
|
}
|
|
if got["nixmsg_deliveries_pending"] != 1 {
|
|
t.Fatalf("pending=%v", got["nixmsg_deliveries_pending"])
|
|
}
|
|
if got["nixmsg_messages_scheduled"] != 1 {
|
|
t.Fatalf("scheduled=%v", got["nixmsg_messages_scheduled"])
|
|
}
|
|
if got["nixmsg_write_queue_length"] != 3 || got["nixmsg_password_hash_queue_length"] != 1 {
|
|
t.Fatalf("queues=%v", got)
|
|
}
|
|
}
|