1+ package extsort_test
2+
3+ import (
4+ "context"
5+ "os"
6+ "testing"
7+
8+ "github.com/lanrat/extsort"
9+ )
10+
11+ // TestSingleChunkOptimization verifies that small datasets don't create temp files
12+ func TestSingleChunkOptimization (t * testing.T ) {
13+ // Small dataset that should fit in a single chunk
14+ inputChan := make (chan extsort.SortType , 10 )
15+ for i := 9 ; i >= 0 ; i -- { // reverse order to ensure sorting happens
16+ inputChan <- val {Key : i , Order : i }
17+ }
18+ close (inputChan )
19+
20+ // Create sorter with default config (ChunkSize = 1M)
21+ sort , outChan , errChan := extsort .New (inputChan , fromBytesForTest , KeyLessThan , nil )
22+
23+ // Count temp files before
24+ tempDir := os .TempDir ()
25+ beforeFiles , err := os .ReadDir (tempDir )
26+ if err != nil {
27+ t .Fatalf ("Failed to read temp dir: %v" , err )
28+ }
29+ beforeCount := len (beforeFiles )
30+
31+ // Sort
32+ sort .Sort (context .Background ())
33+
34+ // Read results
35+ results := make ([]val , 0 , 10 )
36+ for rec := range outChan {
37+ results = append (results , rec .(val ))
38+ }
39+ if err := <- errChan ; err != nil {
40+ t .Fatalf ("Sort error: %v" , err )
41+ }
42+
43+ // Count temp files after
44+ afterFiles , err := os .ReadDir (tempDir )
45+ if err != nil {
46+ t .Fatalf ("Failed to read temp dir: %v" , err )
47+ }
48+ afterCount := len (afterFiles )
49+
50+ // Verify results are sorted
51+ for i := 0 ; i < len (results ); i ++ {
52+ if results [i ].Key != i {
53+ t .Errorf ("Expected Key %d at position %d, got %d" , i , i , results [i ].Key )
54+ }
55+ }
56+
57+ // Verify no additional temp files were created
58+ if afterCount > beforeCount {
59+ t .Errorf ("Expected no temp files to be created for single chunk, but temp file count increased from %d to %d" , beforeCount , afterCount )
60+ }
61+
62+ t .Logf ("Single chunk optimization working: no temp files created for 10 items" )
63+ }
64+
65+ // TestMultiChunkStillUsesTempFiles verifies that large datasets still use temp files
66+ func TestMultiChunkStillUsesTempFiles (t * testing.T ) {
67+ // Use a very small chunk size to force multiple chunks
68+ config := extsort .DefaultConfig ()
69+ config .ChunkSize = 2 // Force multiple chunks for small dataset
70+
71+ inputChan := make (chan extsort.SortType , 10 )
72+ for i := 9 ; i >= 0 ; i -- {
73+ inputChan <- val {Key : i , Order : i }
74+ }
75+ close (inputChan )
76+
77+ sort , outChan , errChan := extsort .New (inputChan , fromBytesForTest , KeyLessThan , config )
78+ sort .Sort (context .Background ())
79+
80+ // Read results to completion
81+ results := make ([]val , 0 , 10 )
82+ for rec := range outChan {
83+ results = append (results , rec .(val ))
84+ }
85+ if err := <- errChan ; err != nil {
86+ t .Fatalf ("Sort error: %v" , err )
87+ }
88+
89+ // Verify results are sorted
90+ for i := 0 ; i < len (results ); i ++ {
91+ if results [i ].Key != i {
92+ t .Errorf ("Expected Key %d at position %d, got %d" , i , i , results [i ].Key )
93+ }
94+ }
95+
96+ t .Logf ("Multi-chunk path still working with small chunk size" )
97+ }
0 commit comments