-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathasync_buffer_test.go
More file actions
353 lines (301 loc) · 8.52 KB
/
async_buffer_test.go
File metadata and controls
353 lines (301 loc) · 8.52 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package main
import (
"context"
"path/filepath"
"testing"
"time"
"os"
"io"
"github.com/unxed/f4/vfs"
"github.com/unxed/vtui"
"github.com/unxed/f4/piecetable"
)
func TestAsyncBuffer_LoadingCycle(t *testing.T) {
vtui.FrameManager.Init(vtui.NewSilentScreenBuf())
content := []byte("This is a test file content for async buffer.")
tmp := filepath.Join(t.TempDir(), "test.txt")
v := vfs.NewOSVFS(t.TempDir())
wc, _ := v.Create(context.Background(), tmp)
wc.Write(content)
wc.Close()
f, _ := v.Open(context.Background(), tmp)
// Create buffer with very small chunks (10 bytes) to trigger multi-chunk logic
buf := NewAsyncBuffer(context.Background(), f)
buf.chunkSize = 10
defer buf.Close()
// 1. Initial read should return ErrLoading
data, err := buf.Read(0, 5)
if err != piecetable.ErrLoading {
t.Errorf("Expected ErrLoading, got %v", err)
}
if data != nil {
t.Error("Data should be nil when loading")
}
// 2. Process tasks (wait for the fetch goroutine to post and for us to run the task)
success := false
deadline := time.Now().Add(1 * time.Second)
for time.Now().Before(deadline) {
select {
case task := <-vtui.FrameManager.TaskChan:
task()
default:
time.Sleep(10 * time.Millisecond)
}
// Check if data is now available
data, err = buf.Read(0, 5)
if err == nil {
success = true
break
}
}
if !success {
t.Fatalf("Read failed after fetch: %v", err)
}
// 3. Verify data content
data, err = buf.Read(0, 5)
if err != nil {
t.Errorf("Read failed after fetch: %v", err)
}
if string(data) != "This " {
t.Errorf("Wrong data: expected 'This ', got %q", string(data))
}
}
func TestAsyncBuffer_BoundaryRead(t *testing.T) {
vtui.FrameManager.Init(vtui.NewSilentScreenBuf())
// Content: 0123456789ABCDEFGHIJ (20 bytes)
content := []byte("0123456789ABCDEFGHIJ")
tmp := filepath.Join(t.TempDir(), "boundary.txt")
os.WriteFile(tmp, content, 0644)
v := vfs.NewOSVFS(t.TempDir())
f, _ := v.Open(context.Background(), tmp)
// Chunk size 10.
buf := NewAsyncBuffer(context.Background(), f)
buf.chunkSize = 10
defer buf.Close()
// 1. Read spanning across chunk 0 and chunk 1: "89AB"
// Indices 8, 9 (Chunk 0) and 10, 11 (Chunk 1)
for {
_, err := buf.Read(8, 4)
if err == piecetable.ErrLoading {
task := <-vtui.FrameManager.TaskChan
task()
continue
}
break
}
data, _ := buf.Read(8, 4)
if string(data) != "89AB" {
t.Errorf("Boundary read failed: expected '89AB', got %q", string(data))
}
}
func TestAsyncBuffer_PartialChunkAtEOF(t *testing.T) {
vtui.FrameManager.Init(vtui.NewSilentScreenBuf())
content := []byte("Short") // 5 bytes
tmp := filepath.Join(t.TempDir(), "eof.txt")
os.WriteFile(tmp, content, 0644)
v := vfs.NewOSVFS(t.TempDir())
f, _ := v.Open(context.Background(), tmp)
buf := NewAsyncBuffer(context.Background(), f)
buf.chunkSize = 100 // Chunk is larger than file
defer buf.Close()
for {
_, err := buf.Read(0, 5)
if err == piecetable.ErrLoading {
task := <-vtui.FrameManager.TaskChan
task()
continue
}
break
}
data, _ := buf.Read(0, 5)
if string(data) != "Short" {
t.Errorf("EOF chunk failed: expected 'Short', got %q", string(data))
}
}
func TestAsyncBuffer_ConcurrentAccess(t *testing.T) {
vtui.FrameManager.Init(vtui.NewSilentScreenBuf())
// Create a decent sized file
content := make([]byte, 1024*1024) // 1MB
for i := range content { content[i] = byte(i % 256) }
tmp := filepath.Join(t.TempDir(), "concurrent.bin")
os.WriteFile(tmp, content, 0644)
v := vfs.NewOSVFS(t.TempDir())
f, _ := v.Open(context.Background(), tmp)
buf := NewAsyncBuffer(context.Background(), f)
buf.chunkSize = 64 * 1024 // 64KB chunks
defer buf.Close()
// Spin up a worker to constantly pump the UI task queue (simulating fm.Run)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func() {
for {
select {
case <-ctx.Done(): return
case task := <-vtui.FrameManager.TaskChan:
task()
}
}
}()
// Fire 50 concurrent reads across different overlapping chunks
done := make(chan bool)
for i := 0; i < 50; i++ {
go func(offset int) {
for retries := 0; retries < 100; retries++ {
_, err := buf.Read(offset, 100)
if err == nil { break }
if err != piecetable.ErrLoading {
t.Errorf("Unexpected error: %v", err)
break
}
time.Sleep(5 * time.Millisecond)
}
done <- true
}(i * 10000) // Stagger offsets
}
// Wait for all goroutines
timeout := time.After(3 * time.Second)
for i := 0; i < 50; i++ {
select {
case <-done:
case <-timeout:
t.Fatal("Concurrency test deadlocked or timed out")
}
}
}
func TestAsyncBuffer_CancellationMidFetch(t *testing.T) {
vtui.FrameManager.Init(vtui.NewSilentScreenBuf())
v := vfs.NewOSVFS(t.TempDir())
tmp := filepath.Join(t.TempDir(), "cancel.txt")
os.WriteFile(tmp, []byte("some content"), 0644)
f, _ := v.Open(context.Background(), tmp)
ctx, cancel := context.WithCancel(context.Background())
buf := NewAsyncBuffer(ctx, f)
buf.chunkSize = 100
// 1. Trigger fetch
_, err := buf.Read(0, 5)
if err != piecetable.ErrLoading { t.Fatal("Expected ErrLoading") }
// 2. Cancel context while fetch is (presumably) in flight
cancel()
// 3. Pump tasks - the fetch result should be ignored because of b.ctx.Err()
timeout := time.After(100 * time.Millisecond)
Loop:
for {
select {
case task := <-vtui.FrameManager.TaskChan:
task()
case <-timeout:
break Loop
}
}
// 4. Verification: data should NOT be in 'loaded' map
buf.mu.Lock()
if len(buf.loaded) > 0 {
t.Error("Data was loaded into buffer after context cancellation")
}
buf.mu.Unlock()
}
func TestAsyncBuffer_RedundantFetchPrevention(t *testing.T) {
// Tests that the 'fetching' map correctly prevents multiple goroutines
// for the same chunk index.
vtui.FrameManager.Init(vtui.NewSilentScreenBuf())
v := vfs.NewOSVFS(t.TempDir())
tmp := filepath.Join(t.TempDir(), "redundant.txt")
os.WriteFile(tmp, make([]byte, 1000), 0644)
f, _ := v.Open(context.Background(), tmp)
buf := NewAsyncBuffer(context.Background(), f)
buf.chunkSize = 100
defer buf.Close()
// Trigger 10 simultaneous reads for the same offset
for i := 0; i < 10; i++ {
go func() {
_, _ = buf.Read(0, 5)
}()
}
// Give goroutines time to start
time.Sleep(10 * time.Millisecond)
buf.mu.Lock()
if len(buf.fetching) > 1 {
t.Errorf("Expected at most 1 in-flight fetch for chunk 0, got %d", len(buf.fetching))
}
buf.mu.Unlock()
}
func TestAsyncBuffer_ContextRace(t *testing.T) {
// Simulates the scenario where a context is cancelled exactly when data arrives.
vtui.FrameManager.Init(vtui.NewSilentScreenBuf())
content := []byte("Race test content")
v := vfs.NewOSVFS(t.TempDir())
tmp := filepath.Join(t.TempDir(), "race.txt")
os.WriteFile(tmp, content, 0644)
f, _ := v.Open(context.Background(), tmp)
for i := 0; i < 100; i++ {
ctx, cancel := context.WithCancel(context.Background())
buf := NewAsyncBuffer(ctx, f)
buf.chunkSize = 5
// Start fetch
go func() {
_, _ = buf.Read(0, 5)
}()
// Immediate cancel to hit the race window in fetchChunk
cancel()
// Pump tasks
timeout := time.After(10 * time.Millisecond)
loop:
for {
select {
case task := <-vtui.FrameManager.TaskChan:
task()
case <-timeout:
break loop
}
}
buf.Close()
}
// If no panic or deadlock occurred in 100 iterations, the mutex/PostTask logic is likely sound.
}
type mockErrorFile struct {
vfs.ReadAtCloser
errToReturn error
}
func (m *mockErrorFile) ReadAt(ctx context.Context, p []byte, off int64) (int, error) {
return 0, m.errToReturn
}
func TestAsyncBuffer_ErrorRecovery(t *testing.T) {
vtui.FrameManager.Init(vtui.NewSilentScreenBuf())
f := &mockErrorFile{errToReturn: io.ErrUnexpectedEOF}
// Manual Size() for mock
buf := &AsyncBuffer{
file: f,
size: 100,
ctx: context.Background(),
loaded: make(map[int][]byte),
fetching: make(map[int]bool),
chunkSize: 10,
}
// 1. Trigger read that fails
_, err := buf.Read(0, 5)
if err != piecetable.ErrLoading { t.Fatal("Should report loading") }
// 2. Process tasks to handle the failure
timeout := time.After(200 * time.Millisecond)
Loop:
for {
select {
case task := <-vtui.FrameManager.TaskChan:
task()
case <-timeout:
break Loop
}
}
// 3. Verify 'fetching' state was cleared so we can retry
buf.mu.Lock()
isFetching := buf.fetching[0]
buf.mu.Unlock()
if isFetching {
t.Error("Fetching flag was not cleared after read error")
}
// 4. Fix the error in mock and retry
f.errToReturn = nil
_, err = buf.Read(0, 5)
if err != piecetable.ErrLoading {
t.Fatal("Should trigger fetch again after error recovery")
}
}