@@ -13,7 +13,6 @@ import (
1313 "os"
1414 "path/filepath"
1515 "reflect"
16- "regexp"
1716 "sort"
1817 "strconv"
1918 "strings"
@@ -368,18 +367,6 @@ func TestRaftSSTableSideloadingInline(t *testing.T) {
368367 v1 , v2 := raftlog .EntryEncodingStandardWithAC , raftlog .EntryEncodingSideloadedWithAC
369368 rangeID := roachpb .RangeID (1 )
370369
371- type testCase struct {
372- // Entry passed into maybeInlineSideloadedRaftCommand and the entry
373- // after having (perhaps) been modified.
374- thin , fat raftpb.Entry
375- // Populate the raft entry cache and sideload storage before running the test.
376- setup func (* raftentry.Cache , SideloadStorage )
377- // If nonempty, the error expected from maybeInlineSideloadedRaftCommand.
378- expErr string
379- // If nonempty, a regex that the recorded trace span must match.
380- expTrace string
381- }
382-
383370 sstFat := kvserverpb.ReplicatedEvalResult_AddSSTable {
384371 Data : []byte ("foo" ),
385372 CRC32 : 0 , // not checked
@@ -388,91 +375,84 @@ func TestRaftSSTableSideloadingInline(t *testing.T) {
388375 CRC32 : 0 , // not checked
389376 }
390377
391- putOnDisk := func (ec * raftentry.Cache , ss SideloadStorage ) {
392- if err := ss .Put (context .Background (), 5 , 6 , sstFat .Data ); err != nil {
393- t .Fatal (err )
394- }
378+ putOnDisk := func (_ * raftentry.Cache , ss SideloadStorage ) {
379+ require .NoError (t , ss .Put (context .Background (), 5 , 6 , sstFat .Data ))
395380 }
396381
397- testCases := map [string ]testCase {
382+ for _ , test := range []struct {
383+ name string
384+ // Entry passed into maybeInlineSideloadedRaftCommand and the entry
385+ // after having (perhaps) been modified.
386+ thin , fat raftpb.Entry
387+ // Populate the raft entry cache and sideload storage before running the test.
388+ setup func (* raftentry.Cache , SideloadStorage )
389+ // If nonempty, the error expected from maybeInlineSideloadedRaftCommand.
390+ expErr string
391+ // If nonempty, a regex that the recorded trace span must match.
392+ expTrace string
393+ }{
398394 // Plain old v1 Raft command without payload. Don't touch.
399- "v1-no-payload" : { thin : mkEnt (v1 , 5 , 6 , & sstThin ), fat : mkEnt (v1 , 5 , 6 , & sstThin )},
395+ { name : "v1-no-payload" , thin : mkEnt (v1 , 5 , 6 , & sstThin ), fat : mkEnt (v1 , 5 , 6 , & sstThin )},
400396 // With payload, but command is v1. Don't touch. Note that the
401397 // first of the two shouldn't happen in practice or we have a
402398 // huge problem once we try to apply this entry.
403- "v1-slim-with-payload" : { thin : mkEnt (v1 , 5 , 6 , & sstThin ), fat : mkEnt (v1 , 5 , 6 , & sstThin )},
404- "v1-with-payload" : { thin : mkEnt (v1 , 5 , 6 , & sstFat ), fat : mkEnt (v1 , 5 , 6 , & sstFat )},
399+ { name : "v1-slim-with-payload" , thin : mkEnt (v1 , 5 , 6 , & sstThin ), fat : mkEnt (v1 , 5 , 6 , & sstThin )},
400+ { name : "v1-with-payload" , thin : mkEnt (v1 , 5 , 6 , & sstFat ), fat : mkEnt (v1 , 5 , 6 , & sstFat )},
405401 // v2 with payload, but payload is AWOL. This would be fatal in practice.
406- "v2-with-payload-missing-file" : {
402+ {
403+ name : "v2-with-payload-missing-file" ,
407404 thin : mkEnt (v2 , 5 , 6 , & sstThin ), fat : mkEnt (v2 , 5 , 6 , & sstThin ),
408405 expErr : "not found" ,
409406 },
410407 // v2 with payload that's actually there. The request we'll see in
411408 // practice.
412- "v2-with-payload-with-file-no-cache" : {
409+ {
410+ name : "v2-with-payload-with-file-no-cache" ,
413411 thin : mkEnt (v2 , 5 , 6 , & sstThin ), fat : mkEnt (v2 , 5 , 6 , & sstFat ),
414412 setup : putOnDisk , expTrace : "inlined entry not cached" ,
415413 },
416- "v2-with-payload-with-file-with-cache" : {
414+ {
415+ name : "v2-with-payload-with-file-with-cache" ,
417416 thin : mkEnt (v2 , 5 , 6 , & sstThin ), fat : mkEnt (v2 , 5 , 6 , & sstFat ),
418417 setup : func (ec * raftentry.Cache , ss SideloadStorage ) {
419418 putOnDisk (ec , ss )
420419 ec .Add (rangeID , []raftpb.Entry {mkEnt (v2 , 5 , 6 , & sstFat )}, true )
421420 }, expTrace : "using cache hit" ,
422421 },
423- "v2-fat-without-file" : {
422+ {
423+ name : "v2-fat-without-file" ,
424424 thin : mkEnt (v2 , 5 , 6 , & sstFat ), fat : mkEnt (v2 , 5 , 6 , & sstFat ),
425425 setup : func (ec * raftentry.Cache , ss SideloadStorage ) {},
426426 expTrace : "already inlined" ,
427427 },
428- }
429-
430- runOne := func (k string , test testCase ) {
431- ctx , getRecAndFinish := tracing .ContextWithRecordingSpan (
432- context .Background (), tracing .NewTracer (), "test-recording" )
433- defer getRecAndFinish ()
434-
435- eng := storage .NewDefaultInMemForTesting ()
436- defer eng .Close ()
437- ss := newTestingSideloadStorage (eng )
438- ec := raftentry .NewCache (1024 ) // large enough
439- if test .setup != nil {
440- test .setup (ec , ss )
441- }
428+ } {
429+ t .Run (test .name , func (t * testing.T ) {
430+ ctx , getRecAndFinish := tracing .ContextWithRecordingSpan (
431+ context .Background (), tracing .NewTracer (), "test-recording" )
432+ defer getRecAndFinish ()
442433
443- thinCopy := * (protoutil .Clone (& test .thin ).(* raftpb.Entry ))
444- newEnt , err := MaybeInlineSideloadedRaftCommand (ctx , rangeID , thinCopy , ss , ec )
445- if err != nil {
446- if test .expErr == "" || ! testutils .IsError (err , test .expErr ) {
447- t .Fatalf ("%s: %+v" , k , err )
434+ eng := storage .NewDefaultInMemForTesting ()
435+ defer eng .Close ()
436+ ss := newTestingSideloadStorage (eng )
437+ ec := raftentry .NewCache (1024 ) // large enough
438+ if test .setup != nil {
439+ test .setup (ec , ss )
448440 }
449- } else if test .expErr != "" {
450- t .Fatalf ("%s: success, but expected error: %s" , k , test .expErr )
451- } else {
452- mustEntryEq (t , thinCopy , test .thin )
453- }
454-
455- if newEnt == nil {
456- newEnt = & thinCopy
457- }
458- mustEntryEq (t , * newEnt , test .fat )
459441
460- if dump := getRecAndFinish ().String (); test .expTrace != "" {
461- if ok , err := regexp .MatchString (test .expTrace , dump ); err != nil {
462- t .Fatalf ("%s: %+v" , k , err )
463- } else if ! ok {
464- t .Fatalf ("%s: expected trace matching:\n %s\n \n but got\n %s" , k , test .expTrace , dump )
442+ thinCopy := * (protoutil .Clone (& test .thin ).(* raftpb.Entry ))
443+ newEnt , err := MaybeInlineSideloadedRaftCommand (ctx , rangeID , thinCopy , ss , ec )
444+ if want := test .expErr ; want != "" {
445+ require .ErrorContains (t , err , want )
446+ } else {
447+ require .NoError (t , err )
448+ mustEntryEq (t , thinCopy , test .thin )
465449 }
466- }
467- }
450+ mustEntryEq (t , newEnt , test .fat )
468451
469- keys := make ([]string , 0 , len (testCases ))
470- for k := range testCases {
471- keys = append (keys , k )
472- }
473- sort .Strings (keys )
474- for _ , k := range keys {
475- runOne (k , testCases [k ])
452+ if want := test .expTrace ; want != "" {
453+ require .Contains (t , getRecAndFinish ().String (), want )
454+ }
455+ })
476456 }
477457}
478458
0 commit comments