|
| 1 | +package chunker_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + _ "embed" |
| 6 | + "errors" |
| 7 | + "io" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/nix-community/go-nix/pkg/nixpath/chunker" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | +) |
| 13 | + |
| 14 | +//go:embed simple.go |
| 15 | +var testData []byte |
| 16 | + |
| 17 | +func TestChunkers(t *testing.T) { |
| 18 | + fastCDCChunker, err := chunker.NewFastCDCChunker(bytes.NewReader(testData)) |
| 19 | + if err != nil { |
| 20 | + panic(err) |
| 21 | + } |
| 22 | + |
| 23 | + chunkers := []struct { |
| 24 | + Name string |
| 25 | + Chunker chunker.Chunker |
| 26 | + }{ |
| 27 | + { |
| 28 | + "Simple", |
| 29 | + chunker.NewSimpleChunker(bytes.NewReader(testData)), |
| 30 | + }, |
| 31 | + { |
| 32 | + "FastCDC", |
| 33 | + fastCDCChunker, |
| 34 | + }, |
| 35 | + } |
| 36 | + |
| 37 | + for _, chunker := range chunkers { |
| 38 | + t.Run(chunker.Name, func(t *testing.T) { |
| 39 | + // grab data out of the chunker. |
| 40 | + // Ensure it matches testData. |
| 41 | + |
| 42 | + var receivedData bytes.Buffer |
| 43 | + offset := uint64(0) |
| 44 | + |
| 45 | + for { |
| 46 | + chunk, err := chunker.Chunker.Next() |
| 47 | + if err != nil { |
| 48 | + if errors.Is(err, io.EOF) { |
| 49 | + break |
| 50 | + } |
| 51 | + assert.NoError(t, err, "no other error other than EOF is accepted") |
| 52 | + } |
| 53 | + // check the chunk itself looks sane |
| 54 | + assert.True(t, |
| 55 | + uint64(len(chunk.Data)) == chunk.Size, |
| 56 | + "the length of the chunk data needs to match what's written in Size", |
| 57 | + ) |
| 58 | + |
| 59 | + // check the offset is sane |
| 60 | + assert.Equal(t, offset, chunk.Offset, "recorded offset size doesn't match passed offset size") |
| 61 | + |
| 62 | + offset += chunk.Size |
| 63 | + |
| 64 | + // write the data into the receivedData buffer |
| 65 | + if _, err := receivedData.Write(chunk.Data); err != nil { |
| 66 | + panic(err) |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + // compare received chunk contents with what was passed into the chunker |
| 71 | + assert.Equal(t, testData, receivedData.Bytes()) |
| 72 | + }) |
| 73 | + } |
| 74 | +} |
0 commit comments