# This tests bursting and how requests above the burst limit put the limiter
# into debt.

init
rate:  2
burst: 4
read:  { perbatch: 1, perrequest: 1, perbyte: 0.1 }
write: { perbatch: 1, perrequest: 1, perbyte: 0.1 }
----
00:00:00.000

get_tenants
- 1
- 2
----
[2#1, system#1]

# Read the entire burst worth of bytes plus 0.4 which should put the limiter
# in debt by 0.4. Also record a system read. We'll verify both show up in
# metrics.

record_read
- { tenant: 2, readrequests: 1, readbytes: 24 }
- { tenant: 1, readrequests: 1, readbytes: 10 }
----
[]

# Verify that the above reads made it to the metrics.

metrics
kv_tenant_rate_limit_read_.*_admitted
----
kv_tenant_rate_limit_read_batches_admitted 2
kv_tenant_rate_limit_read_batches_admitted{tenant_id="2"} 1
kv_tenant_rate_limit_read_batches_admitted{tenant_id="system"} 1
kv_tenant_rate_limit_read_bytes_admitted 34
kv_tenant_rate_limit_read_bytes_admitted{tenant_id="2"} 24
kv_tenant_rate_limit_read_bytes_admitted{tenant_id="system"} 10
kv_tenant_rate_limit_read_requests_admitted 2
kv_tenant_rate_limit_read_requests_admitted{tenant_id="2"} 1
kv_tenant_rate_limit_read_requests_admitted{tenant_id="system"} 1

# Launch a read request which will block because we're in debt.

launch
- { id: g1, tenant: 2 }
----
[g1@2]

metrics
current_blocked
----
kv_tenant_rate_limit_current_blocked 1
kv_tenant_rate_limit_current_blocked{tenant_id="2"} 1
kv_tenant_rate_limit_current_blocked{tenant_id="system"} 0

timers
----
00:00:00.200

# Record more reads, putting the limiter further into debt.

record_read
- { tenant: 2, readrequests: 1, readbytes: 16 }
----
[g1@2]

# Note that the head of the queue will not be notified immediately but rather
# will be notified when its previous timer expires. If it were notified every
# time reads occurred it could be quite inefficient.

timers
----
00:00:00.200

# Note that the head of the queue notices the removal of readbytes and sets a
# new timer.

advance
201ms
----
00:00:00.201

timers
----
00:00:02.000

advance
1800ms
----
00:00:02.001

await
- g1
----
[]
