-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathoptimization_test.go
More file actions
87 lines (76 loc) · 2.63 KB
/
Copy pathoptimization_test.go
File metadata and controls
87 lines (76 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package extsort_test
import (
"context"
"os"
"path/filepath"
"testing"
"github.com/lanrat/extsort"
)
// unusableTempDir returns a TempFilesDir that cannot hold files: a path below
// a regular file. Any attempt to create a temp file there fails, so a sort
// that succeeds with it never touched the disk. Counting files in a temp dir
// can't show this, because temp files are unlinked as soon as they are created.
func unusableTempDir(t *testing.T) string {
t.Helper()
file := filepath.Join(t.TempDir(), "not-a-dir")
if err := os.WriteFile(file, nil, 0o600); err != nil {
t.Fatalf("Failed to create file: %v", err)
}
return filepath.Join(file, "sub")
}
// sortTen sorts 10 records in reverse order and returns the output and the sort error.
func sortTen(config *extsort.Config) ([]val, error) {
inputChan := make(chan extsort.SortType, 10)
for i := 9; i >= 0; i-- { // reverse order to ensure sorting happens
inputChan <- val{Key: i, Order: i}
}
close(inputChan)
sort, outChan, errChan := extsort.New(inputChan, fromBytesForTest, KeyLessThan, config)
sort.Sort(context.Background())
results := make([]val, 0, 10)
for rec := range outChan {
results = append(results, rec.(val))
}
return results, <-errChan
}
// TestSingleChunkOptimization verifies that small datasets don't create temp files
func TestSingleChunkOptimization(t *testing.T) {
// Default ChunkSize (1M) holds all 10 records in one chunk
config := extsort.DefaultConfig()
config.TempFilesDir = unusableTempDir(t)
results, err := sortTen(config)
if err != nil {
t.Fatalf("Single-chunk sort tried to create a temp file: %v", err)
}
if len(results) != 10 {
t.Fatalf("Expected 10 results, got %d", len(results))
}
for i := range results {
if results[i].Key != i {
t.Errorf("Expected Key %d at position %d, got %d", i, i, results[i].Key)
}
}
}
// TestMultiChunkStillUsesTempFiles verifies that large datasets still use temp files
func TestMultiChunkStillUsesTempFiles(t *testing.T) {
// Use a very small chunk size to force multiple chunks
config := extsort.DefaultConfig()
config.ChunkSize = 2
results, err := sortTen(config)
if err != nil {
t.Fatalf("Sort error: %v", err)
}
if len(results) != 10 {
t.Fatalf("Expected 10 results, got %d", len(results))
}
for i := range results {
if results[i].Key != i {
t.Errorf("Expected Key %d at position %d, got %d", i, i, results[i].Key)
}
}
// The same sort must fail when no temp file can be created, which shows it needs one
config.TempFilesDir = unusableTempDir(t)
if _, err := sortTen(config); err == nil {
t.Fatal("Expected an error from a multi-chunk sort with an unusable TempFilesDir, got nil")
}
}