Files
counters/docs/testing-with-countersend.md
T
2026-11-22 07:49:06 +01:00

12 KiB

Testing migadu_counters with countersend

countersend stands in for the two services next to the daemon, so you can test the daemon on its own server:

countersend send ──POST /in, /out──▶ migadu_counters ──GET notify URLs──▶ countersend receive ──▶ one file per notification
 (plays mailqueuer)                     (daemon under test)                  (plays the web app)

Read this first

  • While the notify URLs point at receive, every notification goes to a file, including real customers' notifications. Nobody is emailed, and the daemon records those notifications as sent, so they are not sent again that day. This is harmless while no real traffic reaches the daemon. Once mailqueuer is live, test on a separate instance instead.
  • Test requests are real counts. They go to the Redis the daemon is configured for and stay on today's counters (TTL 31 days), on the mailbox row and also on its domain and organisation rows. The API has no decrement.
  • Use a test mailbox on a domain with no customer mail, so that the domain and organisation rows belong to you too.
  • Step 12 is not optional: put the real notify URLs back when you finish.

If send would make a counter reach a warning threshold or a limit, it refuses and sends nothing unless you add --allow-notify. It also prints where those notifications would go. Check that line says receive's address before adding the flag. With the real URLs, the web app emails the customer and posts to Telegram.

The commands below use /tmp/countersend, the default config path, and the test domain dimitrijedimitrijevic.com. Change the paths if yours differ. Run them as a user that can read the daemon's config, because send takes the address and token from it.

The test domain

dimitrijedimitrijevic.com is in organisation 2741. Its limits come from the CDB on the server, so check them in step 3 before relying on the numbers here:

Row In Out Out warning (80%)
Mailboxes me@, biz@, helloworld@ 1000 25 20
Other mailboxes 1000 120 96
Domain dimitrijedimitrijevic.com 1000 120 96
Organisation 2741 1000 120 96

The steps use three mailboxes, each with a 25 outgoing limit, so a test crosses a limit in a few requests. They are: me@ for most steps, biz@ in step 7 and helloworld@ in step 9.

  • These are real mailboxes. Your own mail today counts on the same counters, and a mailbox taken past 25 stays there until midnight UTC.
  • The domain and organisation share one budget of 120 outgoing per day. It covers every mailbox, and the organisation also has four other domains. The steps add 88 outgoing in all, below the 96 warning. If real mail today adds more, the domain and organisation warn too.
  • Do not take the incoming side to its limit. Incoming is 1000 at every level, so crossing it on a mailbox also crosses it on the domain and the organisation.

1. Build and copy

On your machine:

make freebsd
scp bin/countersend-freebsd-amd64 server:/tmp/countersend

2. Start the receiver and point the daemon at it

In a second terminal on the server (it runs until Ctrl-C, which prints the totals):

/tmp/countersend receive --listen 127.0.0.1:8099 --dir /var/tmp/countersend/notifications

--dir is where the files go, and it is created if missing. If the daemon is on another host, use a --listen address it can reach.

Keep a copy of the daemon's config. Then replace the URLs in its [notify] section with these, leaving user, password and timeout_seconds as they are:

[notify]
org_url          = "http://127.0.0.1:8099/api/limits?level=$LEVEL&id=$ID&flow=$FLOW&period=$PERIOD&by=$ADDRESS&count=$COUNT&limit=$LIMIT"
address_url      = "http://127.0.0.1:8099/api/limits?level=$LEVEL&id=$ID&flow=$FLOW&period=$PERIOD&by=$ADDRESS&count=$COUNT&limit=$LIMIT"
domain_url       = "http://127.0.0.1:8099/api/limits?level=$LEVEL&id=$ID&flow=$FLOW&period=$PERIOD&by=$ADDRESS&count=$COUNT&limit=$LIMIT"
warn_percent     = 80
warn_org_url     = "http://127.0.0.1:8099/api/limits/warning?level=$LEVEL&id=$ID&flow=$FLOW&period=$PERIOD&by=$ADDRESS&count=$COUNT&limit=$LIMIT&percent=$PERCENT"
warn_address_url = "http://127.0.0.1:8099/api/limits/warning?level=$LEVEL&id=$ID&flow=$FLOW&period=$PERIOD&by=$ADDRESS&count=$COUNT&limit=$LIMIT&percent=$PERCENT"
warn_domain_url  = "http://127.0.0.1:8099/api/limits/warning?level=$LEVEL&id=$ID&flow=$FLOW&period=$PERIOD&by=$ADDRESS&count=$COUNT&limit=$LIMIT&percent=$PERCENT"

Restart the daemon. SIGHUP (reload) only reloads the CDB and reopens the logs, so it does not pick up new URLs.

service migadu_counters restart

3. Read the test mailbox's limits

counterctl --address me@dimitrijedimitrijevic.com
LEVEL  ID                            IN  LIMIT  OUT  LIMIT
A      me@dimitrijedimitrijevic.com  0   1000   0    25
D      dimitrijedimitrijevic.com     0   1000   0    120
O      2741                          0   1000   0    120

The examples below assume these limits and nothing sent today. The warning threshold is 80% of the limit, rounded up, so 20 for me@. Limits of 1 and 2 have no warning. If the counts are not 0 because of your own mail today, lower each --n by that amount.

Also check biz@ and helloworld@ with counterctl --address. Each should show an outgoing limit of 25.

4. Counting works

/tmp/countersend send --route in --recipient me@dimitrijedimitrijevic.com --n 10
/tmp/countersend send --route out --auth-user me@dimitrijedimitrijevic.com --recipient someone@gmail.com --n 2

Each run ends in OK, exit code 0, and shows what moved:

POST http://localhost:5555/in  n=10 concurrency=1  trace ids: countersend-3d66700b-*
status:  202=10
timing:  1.426ms total, 7011 req/s, p50 80µs, p99 435µs
recipient me@dimitrijedimitrijevic.com, incoming
  LEVEL  ID                            BEFORE  AFTER  CHANGE  LIMIT
  A      me@dimitrijedimitrijevic.com  0       10     +10     1000
  D      dimitrijedimitrijevic.com     0       10     +10     1000
  O      2741                          0       10     +10     1000
OK

--url http://HOST:5555 sends to another daemon. To check independently, counterctl --address me@dimitrijedimitrijevic.com shows the same numbers, and this shows the whole run in the log:

grep countersend-3d66700b /var/log/migadu_counters/counting.log

send exits 1 and prints FAIL if any request is not answered 202 or if the mailbox counter does not rise in time. --wait 15s allows longer for a slow replica.

5. What must not count

Command Expected
/tmp/countersend send --route in --recipient someone@countersend.invalid domain not hosted: nothing should move, OK
/tmp/countersend send --route in --recipient support@migadu.com exempt address in this request: nothing should move, OK
/tmp/countersend send --route in --recipient me@dimitrijedimitrijevic.com --junk Changes stay +0 when count_junk = false. send does not judge this one.
/tmp/countersend send --route in --recipient not-an-address status: 400=1, FAIL, exit 1. That is the daemon correctly refusing it.

6. The 80% warning and the breach are each sent once

Files appear within a moment of the run. Mailbox outgoing is at 2 after step 4.

Command Counter New files
send --route out --auth-user me@dimitrijedimitrijevic.com --recipient someone@gmail.com --n 17 19 None
Same with --n 1 None: refused, nothing sent, exit 1. This is the guard working.
Same with --n 1 --allow-notify 20 One warning for A
Same with --n 4 24 None
Same with --n 1 --allow-notify 25 One limit for A
Same with --n 5 30 None: past the limit nothing is sent again

Only the mailbox row (A) notifies. The domain and organisation end at 30 of 120, below their warning.

ls /var/tmp/countersend/notifications
20261110T074614.022873Z-000001-warning-A-o-me@dimitrijedimitrijevic.com.json
20261110T074614.573333Z-000002-limit-A-o-me@dimitrijedimitrijevic.com.json

The name is <UTC time>-<sequence>-<limit|warning>-<level>-<flow>-<id>.json, and each notification gets its own file, so two files with the same kind, level, flow and id in one day are a repeat. Open one to check the values. count must be 20 on a warning and 25 on a limit here:

{
  "received_at": "2026-11-10T07:46:14.022873Z",
  "kind": "warning",
  "level": "A",
  "id": "me@dimitrijedimitrijevic.com",
  "flow": "o",
  "period": "d",
  "by": "me@dimitrijedimitrijevic.com",
  "count": "20",
  "limit": "25",
  "percent": "80",
  "path": "/api/limits/warning",
  "raw_query": "level=A&id=me%40dimitrijedimitrijevic.com&flow=o&...",
  "remote_addr": "127.0.0.1:55395",
  "auth_user": "weblogin"
}

auth_user is the login the daemon sent. The password is never stored.

7. Concurrent requests still notify once

Use a second mailbox with a 25 outgoing limit, and send enough to pass it in one concurrent run:

/tmp/countersend send --route out --auth-user biz@dimitrijedimitrijevic.com --recipient someone@gmail.com --n 30 --concurrency 16 --allow-notify
ls /var/tmp/countersend/notifications | grep biz@

Expected: the counters rise by exactly 30, and there is one warning and one limit file for A, biz@dimitrijedimitrijevic.com. The domain and organisation reach 60 of 120 and send nothing.

8. No repeat after a restart

service migadu_counters restart
/tmp/countersend send --route out --auth-user me@dimitrijedimitrijevic.com --recipient someone@gmail.com --n 3

Expected: the counter continues from 30 to 33 and there are no new files.

9. When the web app is unreachable

Stop receive with Ctrl-C, then take a third mailbox across its limit:

/tmp/countersend send --route out --auth-user helloworld@dimitrijedimitrijevic.com --recipient someone@gmail.com --n 25 --allow-notify

The daemon tries 5 times over about 15 s and then gives up:

grep 'failed limit' /var/log/migadu_counters/counting.log
fetch -qo - http://127.0.0.1:9095/metrics | grep 'notifications_total.*failed'

Expected: a failed limit exceeded notification ... after 5 attempts line, and the metric at 1 or more. That notification is not sent later, since delivery is at most once by design. If admin_addr is not on 127.0.0.1:9095, use yours. Start receive again afterwards.

The domain and organisation are now at 88 of 120 outgoing. Any further outgoing test passes their 96 warning.

10. Load

/tmp/countersend send --route in --recipient load@countersend.invalid --n 5000 --concurrency 32
fetch -qo - http://127.0.0.1:9095/metrics | grep -E '^migadu_counters_(queue_depth|dropped_total)'

A .invalid recipient tests the HTTP side and the queue without writing to Redis. Keep it that way here. Using a mailbox on the test domain as --recipient would add that many real counts and take the domain and organisation past their 1000 incoming limit.

Expected: all 202, queue_depth back to 0, and dropped_total absent or unchanged. A 503 in the tally means the queue was full.

11. Repeating a test on the same day

A counter already past a limit will not cross it again, so either use another mailbox or clear this one. On the Redis master, with the day in UTC (date -u +%F):

redis-cli -h MASTER -a PASSWORD DEL cnt/A/me@dimitrijedimitrijevic.com/d/2026-11-10/o ntf/A/me@dimitrijedimitrijevic.com/d/2026-11-10/o/warn ntf/A/me@dimitrijedimitrijevic.com/d/2026-11-10/o/limit

Use /i in place of /o for incoming. This also clears me@'s real mail count for today. Only delete mailbox (A) keys. The D key holds every mailbox's count on the domain, and the O/2741 key also holds the organisation's other domains.

12. When you finish

  1. Put the original [notify] section back and run service migadu_counters restart. Until the web app has GET /api/limits/warning deployed, leave warn_*_url empty.
  2. Read the config once more: no notify URL may still contain 127.0.0.1:8099.
  3. Stop receive, and delete /var/tmp/countersend/notifications when you are done reading it.
  4. What stays behind: the test counts (31 days) and the ntf/ markers (48 hours). Today me@, biz@ and helloworld@ are past their outgoing limit, and the domain and organisation 2741 have 88 of 120 outgoing and 10 of 1000 incoming used.