| 1 | package dbreads |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | // Each line carries exactly one outcome, and a full-size sealed chunk fits |
| 10 | // MaxLineBytes once base64-encoded. |
| 11 | func TestLinesAreOneOutcomeEach(t *testing.T) { |
| 12 | t.Parallel() |
| 13 | for _, l := range []Line{ |
| 14 | {Admitted: "dbr_aaaaaaaaaaaaaaaaaaaaaaaaaa"}, |
| 15 | {Picked: "uk-1"}, |
| 16 | {Chunk: 1, Data: []byte("sealed")}, |
| 17 | {Wait: WaitPickup}, |
| 18 | {Ended: EndedNotPicked}, |
| 19 | {Result: &Result{State: StateDone, LastSeq: 1}}, |
| 20 | } { |
| 21 | b, err := json.Marshal(l) |
| 22 | if err != nil { |
| 23 | t.Fatal(err) |
| 24 | } |
| 25 | var m map[string]any |
| 26 | if err := json.Unmarshal(b, &m); err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | keys := len(m) |
| 30 | if l.Chunk != 0 { |
| 31 | keys-- // a chunk line is two fields, chunk and data |
| 32 | } |
| 33 | if keys != 1 { |
| 34 | t.Fatalf("%s carries %d outcomes", b, keys) |
| 35 | } |
| 36 | } |
| 37 | full, _ := json.Marshal(Line{Chunk: 1, Data: bytes.Repeat([]byte{0xff}, ChunkBytes+SealOverhead)}) |
| 38 | if len(full)+1 > MaxLineBytes { |
| 39 | t.Fatalf("a full chunk line is %d bytes, over MaxLineBytes", len(full)) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | // A full pull chunk, sealed and base64-encoded in its line, fits |
| 44 | // MaxLineBytes (plan 3: a 1 MiB chunk's line is about 1.4 MB). |
| 45 | func TestAFullPullChunkLineFits(t *testing.T) { |
| 46 | t.Parallel() |
| 47 | full, _ := json.Marshal(Line{Chunk: 1, Data: bytes.Repeat([]byte{0xff}, PullChunkBytes+SealOverhead)}) |
| 48 | if len(full)+1 > MaxLineBytes { |
| 49 | t.Fatalf("a full pull chunk line is %d bytes", len(full)) |
| 50 | } |
| 51 | } |
| 52 | |