Debian Codegraph @

make_test.go

raw
1 package desync
2
3 import (
4 "bytes"
5 "context"
6 "crypto/rand"
7 "crypto/sha512"
8 "fmt"
9 "os"
10 "path/filepath"
11 "testing"
12
13 "github.com/stretchr/testify/require"
14 )
15
16 func TestParallelChunking(t *testing.T) {
17 null := make([]byte, 4*ChunkSizeMaxDefault)
18 rand1 := make([]byte, 4*ChunkSizeMaxDefault)
19 rand.Read(rand1)
20 rand2 := make([]byte, 4*ChunkSizeMaxDefault)
21 rand.Read(rand2)
22
23 tests := map[string][][]byte{
24 "random input": {rand1, rand2, rand1, rand2, rand1},
25 "leading null": {null, null, null, null, rand1, rand2},
26 "trailing null": {rand1, rand2, null, null, null, null},
27 "middle null": {rand1, null, null, null, null, rand2},
28 "spread out null": {rand1, null, null, null, rand1, null, null, null, rand2},
29 }
30
31 for name, input := range tests {
32 t.Run(name, func(t *testing.T) {
33 // Put the input data into a file for chunking
34 f := filepath.Join(t.TempDir(), "input")
35 b := join(input...)
36 require.NoError(t, os.WriteFile(f, b, 0644))
37
38 // Chunk the file single stream first to use the results as reference for
39 // the parallel chunking
40 c, err := NewChunker(bytes.NewReader(b), ChunkSizeMinDefault, ChunkSizeAvgDefault, ChunkSizeMaxDefault)
41 if err != nil {
42 t.Fatal(err)
43 }
44 var expected []IndexChunk
45 for {
46 start, buf, err := c.Next()
47 if err != nil {
48 t.Fatal(err)
49 }
50 if len(buf) == 0 {
51 break
52 }
53 id := ChunkID(sha512.Sum512_256(buf))
54 expected = append(expected, IndexChunk{Start: start, Size: uint64(len(buf)), ID: id})
55 }
56
57 // Chunk the file with the parallel chunking algorithm and different degrees of concurrency
58 for n := 1; n <= 10; n++ {
59 t.Run(fmt.Sprintf("%s, n=%d", name, n), func(t *testing.T) {
60 index, _, err := IndexFromFile(
61 context.Background(),
62 f,
63 n,
64 ChunkSizeMinDefault, ChunkSizeAvgDefault, ChunkSizeMaxDefault,
65 NewProgressBar(""),
66 )
67 if err != nil {
68 t.Fatal(err)
69 }
70
71 for i := range expected {
72 if expected[i] != index.Chunks[i] {
73 t.Fatal("chunks from parallel splitter don't match single stream chunks")
74 }
75 }
76 })
77 }
78 })
79 }
80 }
81
82 // TestIndexFromFileStats exercises the parallel chunker's early-EOF/straggler
83 // path (null-heavy inputs, multiple workers) and asserts the returned
84 // ChunkingStats. It guards the data race where IndexFromFile copied stats by
85 // value while worker goroutines were still atomically updating them: with the
86 // join in place the counters must be complete and deterministic for every
87 // worker count. Run under -race to catch a regression of the join.
88 func TestIndexFromFileStats(t *testing.T) {
89 null := make([]byte, 4*ChunkSizeMaxDefault)
90 rnd := make([]byte, 4*ChunkSizeMaxDefault)
91 rand.Read(rnd)
92
93 tests := map[string][][]byte{
94 "trailing null": {rnd, rnd, null, null, null, null},
95 "middle null": {rnd, null, null, null, null, rnd},
96 "spread out null": {rnd, null, null, null, rnd, null, null, null, rnd},
97 }
98
99 for name, input := range tests {
100 t.Run(name, func(t *testing.T) {
101 f := filepath.Join(t.TempDir(), "input")
102 require.NoError(t, os.WriteFile(f, join(input...), 0644))
103
104 for n := 2; n <= 8; n++ {
105 t.Run(fmt.Sprintf("n=%d", n), func(t *testing.T) {
106 index, stats, err := IndexFromFile(
107 context.Background(),
108 f,
109 n,
110 ChunkSizeMinDefault, ChunkSizeAvgDefault, ChunkSizeMaxDefault,
111 NewProgressBar(""),
112 )
113 require.NoError(t, err)
114 require.Equal(t, uint64(len(index.Chunks)), stats.ChunksAccepted,
115 "ChunksAccepted should equal len(index.Chunks)")
116 require.GreaterOrEqual(t, stats.ChunksProduced, stats.ChunksAccepted,
117 "ChunksProduced should be >= ChunksAccepted")
118 require.NotZero(t, stats.ChunksProduced,
119 "workers should have produced chunks")
120 })
121 }
122 })
123 }
124 }