The grader
Tests you write yourself only find the bugs you thought of. The grader checks the spec from outside: start your server, point the grader at it, and read the report.
- 1The grader speaks plain HTTP, so it grades a server in any language.
- 2A failure shows the request and the response, so you can repeat it with curl.
- 3Concurrency checks fire 100 parallel requests at one key and count the result.
- 4Exit code 0 means every check passed. Use it as the end of each rehearsal.
I run the grader at each checkpoint. It tells me which behaviours are missing, not only that my own tests pass.
go build -o arena ./cmd/arena
./arena list
kvstore Key-value store with nested transactions./arena run kvstore --base http://localhost:8080
./arena run kvstore --base http://localhost:8080 \
--header "Authorization: Bearer my-token"| exit code | meaning | status |
|---|---|---|
| 0 | Every check passed. | Approved |
| 1 | At least one check failed. | Not approved |
| 2 | The run could not start: no server, or a wrong name or flag. | Fix the command |
One terminal runs the server. The other runs the grader.
| group | E1 checks | question it answers | example |
|---|---|---|---|
| correctness | 12 | Does the API do what the spec says? | Nested rollback undoes only the inner level. |
| status codes | 7 | Does each outcome have the right code? | A stale read gives 409 at commit. |
| validation | 5 | Does bad input fail with a 400 and no change? | A missing value is 400; an empty string is a value. |
| concurrency | 6 | Under 100 parallel requests, is anything lost or counted twice? | Parallel commits of one transaction: exactly one wins. |
Correct results come first. Then correct status codes, then bad input, then behaviour under 100 parallel requests.
arena run kvstore --base http://localhost:8080
correctness
PASS get returns what set stored
PASS set replaces the value
PASS an empty string is a value
PASS delete removes the key
PASS commit publishes the writes
PASS a transaction reads its own writes
PASS writes stay hidden until commit
PASS rollback discards the writes
PASS a delete in a transaction waits for commit
PASS nested rollback undoes only the inner level
PASS nested commit is undone by an outer rollback
PASS three levels unwind in order
status codes
PASS get of a missing key is 404
PASS delete of a missing key is 404
PASS begin is 201 and returns an id
PASS an unknown transaction is 404
PASS an ended transaction is 404
PASS a stale read gives 409 at commit
PASS two creators of one key: the second gets 409
validation
PASS malformed JSON is 400
PASS a missing value is 400
PASS a value of the wrong type is 400
PASS a key over 256 bytes is 400
PASS a key of exactly 256 bytes is fine
concurrency
PASS 100 parallel sets of one key all succeed
PASS 100 parallel deletes of one key: exactly one wins
PASS 100 parallel transactions increment one key: none is lost
PASS 100 parallel rollbacks leave the key alone
PASS 100 parallel writes in one transaction all land
PASS 100 parallel commits of one transaction: exactly one wins
30 checks: 30 passed, 0 failedAll green means the behaviours in the spec work. It does not mean that the design is good. The talk covers the design.
FAIL a stale read gives 409 at commit
want status 409, got 200
> GET /tx/H6DLVSPC65IW6ECYQHXDCBV3W2/kv/run-18-k
< 200 {"key":"run-18-k","value":"1"}
> PUT /tx/H6DLVSPC65IW6ECYQHXDCBV3W2/kv/run-18-k {"value":"2"}
< 200 {"key":"run-18-k","value":"2"}
> PUT /kv/run-18-k {"value":"other"}
< 200 {"key":"run-18-k","value":"other"}
> POST /tx/H6DLVSPC65IW6ECYQHXDCBV3W2/commit
< 200 {"id":"H6DLVSPC65IW6ECYQHXDCBV3W2"}- The first line says what the grader wanted and what it got.
- Lines with > are requests. Lines with < are responses. The grader shows the last 4 exchanges.
- In this saved run, the server failed 4 of 30 checks.
The bug: commit writes without comparing the versions it read. Every memory access is locked, so the race detector reports nothing.
The report shows the failing request and response, so I can repeat it with curl.
{Name: "100 parallel transactions increment one key: none is lost", Group: grader.Concurrency, Run: func(t *grader.T) {
key := t.Key("n")
set(t, "", key, "0")
var committed atomic.Int64
t.Parallel(parallel, func(int) {1
increment(t, key)2
committed.Add(1)
})
got := value(t, "", key)
if got != strconv.FormatInt(committed.Load(), 10)3 {
t.Failf("%d transactions committed an increment, but the key holds %s", committed.Load(), got)
}
}},- 1100 goroutines. This is the same behaviour a test with a WaitGroup checks, but over a real socket.
- 2Begin, read, write the value plus 1, commit. On 409 it waits a random time and starts again, as a real client must.
- 3The count of successful commits must equal the stored number. One lost update breaks this.
// Parallel runs fn on n goroutines that all start at once, and returns when they finish. A failure
// in any goroutine stops the check once all have ended.
func (t *T) Parallel(n int, fn func(i int)) {
t.parallel.Store(true)
defer t.parallel.Store(false)
var (
wg sync.WaitGroup
once sync.Once
first failed
hit bool
start = make(chan struct{})
)
for i := range n {
wg.Go(func() {
<-start1
defer func() {
if r := recover(); r != nil {
f, ok := r.(failed)
if !ok {
panic(r)
}
once.Do(func() { first, hit = f, true })2
}
}()
fn(i)
})
}
close(start)
wg.Wait()
if hit {
panic(first)
}
}
- 1Every goroutine waits here. close(start) releases all of them at once. A test proves that 100 requests overlap in the server.
- 2The first failure is the one the report shows. The check stops after all goroutines end.
All goroutines wait at one gate, so the 100 requests really overlap. Then the check compares the final value with the number of commits that succeeded.
// Check is one graded behaviour. Run talks to the server through t and stops the check with a
// failure through t.Status, t.Failf or t.Parallel. A check that returns has passed.
type Check struct {
Name string
Group Group1
Run func(t *T)2
}
// Exercise is a named list of checks.
type Exercise struct {
Name string
Title string
Checks []Check
}
- 1One of correctness, status codes, validation or concurrency. The report prints the groups in that order.
- 2A check that returns has passed. t.Status, t.Failx and t.Failf stop the check with a message.
- List the checks in a package for the exercise. Build every key with t.Key.
- Call grader.Register from the package's init function.
- Add a one-line file to the command that imports the package.
An exercise is a list of checks. It knows the API and nothing about any server.
| server under test | result |
|---|---|
| The reference build | All pass |
| The production version, over a real socket | All pass |
| A commit that ignores what it read: the race | Caught |
| Delete answers 200 and not 204 | Caught |
| A missing transaction answers 400 and not 404 | Caught |
| Writes reach the store before commit | Caught |
| Nothing listens on the port | Exit code 2 |
- Green checks cover the spec and not the design.
- A race can hide on one run. Run the grader 3 times.
We test the grader on broken servers. A grader that passes everything proves nothing.
0 of 5 known
Your server is in Python. Can the grader check it?
The run ends with exit code 2 and no report. What happened?
A server passes go test -race but fails the 100 parallel increments. How?
You run the grader twice against the same server. Does the second run see data from the first?
A check passes 100 increments with retries on 409. Why must the grader count commits and not requests?