server: auth schema, keys, sessions, connection registry (#17)
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
## Connection caps (#17, DESIGN 4.3/6.5): 4 per player (5th evicts the oldest), 16 total (17th refused).
|
||||
import std/[unittest, options]
|
||||
import mummy
|
||||
import conns, auth
|
||||
|
||||
proc dummyWs(): WebSocket =
|
||||
# Mummy's fields are private; an all-zero value is enough for registry bookkeeping tests.
|
||||
var ws: WebSocket
|
||||
ws
|
||||
|
||||
suite "total cap":
|
||||
test "16 reservations succeed, the 17th is refused":
|
||||
let c = newConns()
|
||||
for _ in 0 ..< MaxConnsTotal: check c.reserve()
|
||||
check c.pendingCount == 16
|
||||
check not c.reserve()
|
||||
check c.pendingCount == 16
|
||||
|
||||
test "a cancelled reservation gives the slot back":
|
||||
let c = newConns()
|
||||
for _ in 0 ..< MaxConnsTotal: check c.reserve()
|
||||
check not c.reserve()
|
||||
c.cancelReserve()
|
||||
check c.pendingCount == 15
|
||||
check c.reserve()
|
||||
check not c.reserve()
|
||||
|
||||
test "activated connections keep the total cap; removing one frees a slot":
|
||||
let c = newConns()
|
||||
for i in 0 ..< MaxConnsTotal:
|
||||
check c.reserve()
|
||||
discard c.activate(i mod 4, prPlayer, dummyWs())
|
||||
check c.activeCount == 16
|
||||
check c.pendingCount == 0
|
||||
check not c.reserve()
|
||||
c.remove(1)
|
||||
check c.activeCount == 15
|
||||
check c.reserve()
|
||||
|
||||
suite "per-player cap":
|
||||
test "the 5th connection of one player evicts that player's oldest":
|
||||
let c = newConns()
|
||||
var ids: seq[int]
|
||||
for _ in 0 ..< MaxConnsPerPlayer:
|
||||
check c.reserve()
|
||||
let a = c.activate(7, prPlayer, dummyWs())
|
||||
ids.add a.id
|
||||
check a.evictedId.isNone
|
||||
check c.activeCountFor(7) == 4
|
||||
check c.reserve()
|
||||
let fifth = c.activate(7, prPlayer, dummyWs())
|
||||
check fifth.evictedId == some(ids[0])
|
||||
check c.activeCountFor(7) == 4
|
||||
check c.connById(ids[0]).isNone
|
||||
check c.connById(fifth.id).isSome
|
||||
# the next one evicts the second-oldest, not a new victim at random
|
||||
check c.reserve()
|
||||
let sixth = c.activate(7, prPlayer, dummyWs())
|
||||
check sixth.evictedId == some(ids[1])
|
||||
|
||||
test "another player's connections are never evicted":
|
||||
let c = newConns()
|
||||
for _ in 0 ..< MaxConnsPerPlayer:
|
||||
check c.reserve()
|
||||
discard c.activate(1, prPlayer, dummyWs())
|
||||
let other = c.activate(2, prPlayer, dummyWs()) # player 2 has none: no eviction
|
||||
check other.evictedId.isNone
|
||||
# player 1 is at the cap: its next one evicts player 1's oldest, player 2's stays
|
||||
discard c.activate(1, prPlayer, dummyWs())
|
||||
check c.activeCountFor(1) == 4
|
||||
check c.activeCountFor(2) == 1
|
||||
|
||||
test "the GM is capped like anyone else (caps are per player, not per role)":
|
||||
let c = newConns()
|
||||
var gmIds: seq[int]
|
||||
for _ in 0 ..< MaxConnsPerPlayer:
|
||||
discard c.activate(1, prGm, dummyWs())
|
||||
for id in 1 .. MaxConnsPerPlayer: gmIds.add id
|
||||
let fifth = c.activate(1, prGm, dummyWs())
|
||||
check fifth.evictedId == some(gmIds[0])
|
||||
|
||||
suite "revocation":
|
||||
test "revokePlayer returns every live connection of that player and nothing else":
|
||||
let c = newConns()
|
||||
discard c.activate(1, prPlayer, dummyWs())
|
||||
discard c.activate(2, prPlayer, dummyWs())
|
||||
discard c.activate(1, prPlayer, dummyWs())
|
||||
check c.revokePlayer(1).len == 2
|
||||
check c.activeCountFor(1) == 0
|
||||
check c.activeCountFor(2) == 1
|
||||
check c.activeCount == 1
|
||||
check c.revokePlayer(1).len == 0
|
||||
|
||||
test "remove works by conn id; an unknown id is a no-op":
|
||||
let c = newConns()
|
||||
let a = c.activate(1, prPlayer, dummyWs())
|
||||
c.remove(a.id)
|
||||
c.remove(a.id)
|
||||
check c.activeCount == 0
|
||||
check c.connById(a.id).isNone
|
||||
|
||||
test "connForWs finds the registered socket (mummy hands the same value back)":
|
||||
let c = newConns()
|
||||
var ws: WebSocket
|
||||
let a = c.activate(1, prPlayer, ws)
|
||||
check c.connForWs(ws).get.id == a.id
|
||||
var other: WebSocket # another all-zero value still equals the first one structurally
|
||||
check c.connForWs(other).isSome
|
||||
Reference in New Issue
Block a user