@@ -9,34 +9,40 @@ import (
99 "testing"
1010)
1111
12+ const (
13+ chunkSize = 10
14+ maxPeekSize = 5
15+ smallFileThreshold = int64 (20 )
16+ )
17+
1218func TestGetAndPutBuf (t * testing.T ) {
13- chunk := NewChunk ()
14- buf , ok := chunk .GetBuf ()
15- defer chunk .PutBuf (buf )
19+ c := New ()
20+ data := []byte ("test" )
21+ buf , ok := c .GetBuf (data )
22+ defer c .PutBuf (buf )
1623
1724 require .True (t , ok )
18- require .Equal (t , defaultSize , len (* buf ))
25+ require .Equal (t , defaultSize + defaultMaxPeekSize , buf .Cap ())
26+ require .Equal (t , string (data ), buf .String ())
1927}
2028
21- func TestGetAndPutPeekBuf (t * testing.T ) {
22- chunk := NewChunk ()
23- data := []byte ("test" )
24- buf , ok := chunk .GetPeekBuf (data )
25- defer chunk .PutPeekBuf (buf )
29+ func TestGetAndPutPeekedBuf (t * testing.T ) {
30+ c := New ()
31+ window , ok := c .GetPeekedBuf ()
32+ defer c .PutPeekedBuf (window )
2633
2734 require .True (t , ok )
28- require .Equal (t , defaultSize + defaultMaxPeekSize , buf .Cap ())
29- require .Equal (t , string (data ), buf .String ())
35+ require .Equal (t , defaultSize + defaultMaxPeekSize , len (* window ))
3036}
3137
3238func TestGetSize (t * testing.T ) {
33- chunk := NewChunk ()
34- require .Equal (t , defaultSize , chunk .GetSize ())
39+ c := New ()
40+ require .Equal (t , defaultSize , c .GetSize ())
3541}
3642
3743func TestGetMaxPeekSize (t * testing.T ) {
38- chunk := NewChunk ()
39- require .Equal (t , defaultMaxPeekSize , chunk .GetMaxPeekSize ())
44+ c := New ()
45+ require .Equal (t , defaultMaxPeekSize , c .GetMaxPeekSize ())
4046}
4147
4248func TestReadChunk (t * testing.T ) {
@@ -66,12 +72,12 @@ func TestReadChunk(t *testing.T) {
6672 {
6773 name : "successful read - peek size exceeded" ,
6874 reader : strings .NewReader ("abc\n def\n ghi\n jkl\n mno\n pqr\n stu\n vwx\n yz" ),
69- expected : "abc\n def\n ghi\n jkl\n mno \n pqr \n s " ,
75+ expected : "abc\n def\n ghi\n jkl" ,
7076 },
7177 {
7278 name : "successful read - multiple lines with consecutives new lines" ,
73- reader : strings .NewReader ("abc\n def\n \n ghi \n " ),
74- expected : "abc\n def\n \n " ,
79+ reader : strings .NewReader ("abc\n def\n \n \n \n \n ghi \n jkl " ),
80+ expected : "abc\n def\n \n \n " ,
7581 },
7682 {
7783 name : "multiple lines without consecutives new lines" ,
@@ -82,11 +88,11 @@ func TestReadChunk(t *testing.T) {
8288
8389 for _ , tc := range testCases {
8490 t .Run (tc .name , func (t * testing.T ) {
85- chunk := NewChunkWithSize ( 5 , 20 , 20 )
86- reader := bufio .NewReader (tc .reader )
91+ c := New ( WithSize ( chunkSize ), WithMaxPeekSize ( maxPeekSize ), WithSmallFileThreshold ( smallFileThreshold ) )
92+ reader := bufio .NewReaderSize (tc .reader , chunkSize + maxPeekSize )
8793
8894 // Act
89- result , err := chunk .ReadChunk (reader , 0 )
95+ result , err := c .ReadChunk (reader , 0 )
9096 require .ErrorIs (t , err , tc .expectedError )
9197
9298 // Assert
@@ -95,63 +101,62 @@ func TestReadChunk(t *testing.T) {
95101 }
96102}
97103
98- func TestReadUntilSafeBoundary (t * testing.T ) {
104+ func TestGenerateChunk (t * testing.T ) {
99105 // Arrange
100106 testCases := []struct {
101107 name string
102- reader io. Reader
108+ rawData [] byte
103109 expected string
104110 }{
105111 // Current split is fine, exit early.
106112 {
107113 name : "safe original split - LF" ,
108- reader : strings . NewReader ("abc\n \n defghijklmnop \n \n qrstuvwxyz" ),
109- expected : "abc\n \n " ,
114+ rawData : [] byte ("abc\n def \n \n \n ghijklmnop \n \n qrstuvwxyz" ),
115+ expected : "abc\n def \n \ n\n " ,
110116 },
111117 {
112118 name : "safe original split - CRLF" ,
113- reader : strings . NewReader ( "a \r \n \r \n bcdefghijklmnop \n " ),
114- expected : "a \r \n \r \n " ,
119+ rawData : [] byte ( "abcdef \r \n \r \n ghijklmnop \n " ),
120+ expected : "abcdef \r \n \r \n " ,
115121 },
116- // Current split is bad, look for a better one.
122+ // Current split is bad, look for a better one
117123 {
118124 name : "safe split - LF" ,
119- reader : strings . NewReader ( "abcdefg \n hijklmnop \n \n qrstuvwxyz" ),
120- expected : "abcdefg \n hijklmnop \n \n " ,
125+ rawData : [] byte ( "abcdef \n ghi \n \n jklmnop \n \n qrstuvwxyz" ),
126+ expected : "abcdef \n ghi \n \n " ,
121127 },
122128 {
123129 name : "safe split - CRLF" ,
124- reader : strings . NewReader ( "abcdefg \r \n hijklmnop \r \n \r \n qrstuvwxyz " ),
125- expected : "abcdefg \r \n hijklmnop \r \n \r \n " ,
130+ rawData : [] byte ( "abcdef \r \n ghi \r \n \r \n jklmnopqrstuvwxyz " ),
131+ expected : "abcdef \r \n ghi \r \n \r \n " ,
126132 },
127133 {
128134 name : "safe split - blank line" ,
129- reader : strings . NewReader ( "abcdefg \n hijklmnop \ n\t \t \n qrstuvwxyz " ),
130- expected : "abcdefg \n hijklmnop \n \t \t \n " ,
135+ rawData : [] byte ( "abcdefghi \ n\t \t \n jklmnopqrstuvwxyz " ),
136+ expected : "abcdefghi \n \t \t \n " ,
131137 },
132- // Current split is bad, exhaust options.
138+ // Current split is bad, exhaust options
133139 {
134140 name : "no safe split" ,
135- reader : strings . NewReader ("abcdefg\n hijklmnopqrstuvwxyz" ),
136- expected : "abcdefg\n hijklmnopqrstuvwx " ,
141+ rawData : [] byte ("abcdefg\n hijklmnopqrstuvwxyz" ),
142+ expected : "abcdefg\n hijklmn " ,
137143 },
138144 }
139145
140146 for _ , tc := range testCases {
141147 t .Run (tc .name , func (t * testing.T ) {
142- buf := make ([]byte , 5 )
143- n , err := tc .reader .Read (buf )
148+ c := New (WithSize (chunkSize ), WithMaxPeekSize (maxPeekSize ), WithSmallFileThreshold (smallFileThreshold ))
149+ reader := bufio .NewReaderSize (bytes .NewReader (tc .rawData ), c .size + c .maxPeekSize )
150+ peekedBuf := make ([]byte , c .size + c .maxPeekSize )
151+ _ , err := reader .Read (peekedBuf )
144152 require .NoError (t , err )
145153
146154 // Act
147- chunk := NewChunkWithSize (5 , 20 , 20 )
148- reader := bufio .NewReader (tc .reader )
149- peekBuf := bytes .NewBuffer (buf [:n ])
150- err = chunk .readUntilSafeBoundary (reader , n , peekBuf )
155+ chunkStr , err := c .generateChunk (peekedBuf )
151156 require .NoError (t , err )
152157
153158 // Assert
154- require .Equal (t , tc .expected , peekBuf . String () )
159+ require .Equal (t , tc .expected , chunkStr )
155160 })
156161 }
157162}
0 commit comments