countersend send plays mailqueuer against /in and /out, and countersend receive writes each notification to a file in place of the web app. send refuses a run that would notify unless given --allow-notify. The Makefile builds it in build, freebsd and build-linux. docs/testing-with-countersend.md has the steps. Also add CLAUDE.md and gitignore .claude/. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
235 lines
10 KiB
Markdown
235 lines
10 KiB
Markdown
# 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 `test@yourdomain`. Replace them with your own.
|
|
Run them as a user that can read the daemon's config, because `send` takes the address and token from it.
|
|
|
|
## 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:
|
|
|
|
```toml
|
|
[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 test@yourdomain
|
|
```
|
|
|
|
```
|
|
LEVEL ID IN LIMIT OUT LIMIT
|
|
A test@yourdomain 0 200 0 20
|
|
D yourdomain 0 200 0 20
|
|
O 1049 0 200 0 20
|
|
```
|
|
|
|
The examples below assume this mailbox: outgoing limit 20, nothing sent today. The warning threshold is 80% of the limit, rounded up, so 16 here.
|
|
Limits of 1 and 2 have no warning. Adjust `--n` for your own limits.
|
|
|
|
## 4. Counting works
|
|
|
|
```
|
|
/tmp/countersend send --route in --recipient test@yourdomain --n 10
|
|
/tmp/countersend send --route out --auth-user test@yourdomain --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 test@yourdomain, incoming
|
|
LEVEL ID BEFORE AFTER CHANGE LIMIT
|
|
A test@yourdomain 0 10 +10 200
|
|
D yourdomain 0 10 +10 200
|
|
O 1049 0 10 +10 200
|
|
OK
|
|
```
|
|
|
|
`--url http://HOST:5555` sends to another daemon. To check independently, `counterctl --address test@yourdomain` 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 test@yourdomain --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 test@yourdomain --recipient someone@gmail.com --n 13` | 15 | None |
|
|
| Same with `--n 1` | | None: refused, nothing sent, exit 1. This is the guard working. |
|
|
| Same with `--n 1 --allow-notify` | 16 | One `warning` per level |
|
|
| Same with `--n 3` | 19 | None |
|
|
| Same with `--n 1 --allow-notify` | 20 | One `limit` per level |
|
|
| Same with `--n 5` | 25 | None: past the limit nothing is sent again |
|
|
|
|
```
|
|
ls /var/tmp/countersend/notifications
|
|
```
|
|
|
|
```
|
|
20261110T074614.022873Z-000001-warning-A-o-test@yourdomain.json
|
|
20261110T074614.022878Z-000003-warning-O-o-1049.json
|
|
20261110T074614.022920Z-000002-warning-D-o-yourdomain.json
|
|
20261110T074614.573333Z-000004-limit-A-o-test@yourdomain.json
|
|
20261110T074614.573359Z-000005-limit-D-o-yourdomain.json
|
|
20261110T074614.573470Z-000006-limit-O-o-1049.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 16 on a warning and 20 on a limit here:
|
|
|
|
```json
|
|
{
|
|
"received_at": "2026-11-10T07:46:14.022873Z",
|
|
"kind": "warning",
|
|
"level": "A",
|
|
"id": "test@yourdomain",
|
|
"flow": "o",
|
|
"period": "d",
|
|
"by": "test@yourdomain",
|
|
"count": "16",
|
|
"limit": "20",
|
|
"percent": "80",
|
|
"path": "/api/limits/warning",
|
|
"raw_query": "level=A&id=test%40yourdomain&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. Your levels differ if the domain or organisation has other limits or other mail today.
|
|
|
|
## 7. Concurrent requests still notify once
|
|
|
|
Use the incoming side, with enough requests to pass the limit (the current count of 10 and limit of 200 need at least 190):
|
|
|
|
```
|
|
/tmp/countersend send --route in --recipient test@yourdomain --n 220 --concurrency 16 --allow-notify
|
|
ls /var/tmp/countersend/notifications | grep -- '-i-'
|
|
```
|
|
|
|
Expected: the counters rise by exactly 220, and there is one `warning` and one `limit` file per level.
|
|
|
|
## 8. No repeat after a restart
|
|
|
|
```
|
|
service migadu_counters restart
|
|
/tmp/countersend send --route out --auth-user test@yourdomain --recipient someone@gmail.com --n 3
|
|
```
|
|
|
|
Expected: the counter continues from where it was and there are no new files.
|
|
|
|
## 9. When the web app is unreachable
|
|
|
|
Stop `receive` with Ctrl-C, then use a second test mailbox and take it across a limit:
|
|
|
|
```
|
|
/tmp/countersend send --route out --auth-user test2@yourdomain --recipient someone@gmail.com --n 20 --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.
|
|
|
|
## 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. To load Redis as well, use the test mailbox as `--recipient`, which adds that many real counts and may need `--allow-notify`.
|
|
|
|
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 a new test 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/test@yourdomain/d/2026-11-10/o ntf/A/test@yourdomain/d/2026-11-10/o/warn ntf/A/test@yourdomain/d/2026-11-10/o/limit
|
|
```
|
|
|
|
Use `/i` in place of `/o` for incoming. Only delete mailbox (`A`) keys unless the domain and organisation are yours alone, because the `D` and `O` keys hold every mailbox's count.
|
|
|
|
## 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's real limits for the test domain are reached sooner because of them.
|