package dbreads import ( "bytes" "encoding/json" "testing" ) // Each line carries exactly one outcome, and a full-size sealed chunk fits // MaxLineBytes once base64-encoded. func TestLinesAreOneOutcomeEach(t *testing.T) { t.Parallel() for _, l := range []Line{ {Admitted: "dbr_aaaaaaaaaaaaaaaaaaaaaaaaaa"}, {Picked: "uk-1"}, {Chunk: 1, Data: []byte("sealed")}, {Wait: WaitPickup}, {Ended: EndedNotPicked}, {Result: &Result{State: StateDone, LastSeq: 1}}, } { b, err := json.Marshal(l) if err != nil { t.Fatal(err) } var m map[string]any if err := json.Unmarshal(b, &m); err != nil { t.Fatal(err) } keys := len(m) if l.Chunk != 0 { keys-- // a chunk line is two fields, chunk and data } if keys != 1 { t.Fatalf("%s carries %d outcomes", b, keys) } } full, _ := json.Marshal(Line{Chunk: 1, Data: bytes.Repeat([]byte{0xff}, ChunkBytes+SealOverhead)}) if len(full)+1 > MaxLineBytes { t.Fatalf("a full chunk line is %d bytes, over MaxLineBytes", len(full)) } } // A full pull chunk, sealed and base64-encoded in its line, fits // MaxLineBytes (plan 3: a 1 MiB chunk's line is about 1.4 MB). func TestAFullPullChunkLineFits(t *testing.T) { t.Parallel() full, _ := json.Marshal(Line{Chunk: 1, Data: bytes.Repeat([]byte{0xff}, PullChunkBytes+SealOverhead)}) if len(full)+1 > MaxLineBytes { t.Fatalf("a full pull chunk line is %d bytes", len(full)) } }