-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgeneric_error_test.go
More file actions
217 lines (175 loc) · 5.97 KB
/
generic_error_test.go
File metadata and controls
217 lines (175 loc) · 5.97 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
package extsort_test
import (
"context"
"encoding/json"
"errors"
"strings"
"testing"
"github.com/lanrat/extsort"
)
// TestGenericSerializationError tests serialization error handling in the Generic API
func TestGenericSerializationError(t *testing.T) {
inputChan := make(chan *GenericErrorItem, 6)
// Add more items to ensure multi-chunk behavior
inputChan <- &GenericErrorItem{Key: 1, ShouldFailSerialization: false}
inputChan <- &GenericErrorItem{Key: 2, ShouldFailSerialization: true} // This will fail
inputChan <- &GenericErrorItem{Key: 3, ShouldFailSerialization: false}
inputChan <- &GenericErrorItem{Key: 4, ShouldFailSerialization: false}
inputChan <- &GenericErrorItem{Key: 5, ShouldFailSerialization: false}
inputChan <- &GenericErrorItem{Key: 6, ShouldFailSerialization: false}
close(inputChan)
// Use small chunk size to force multiple chunks
config := &extsort.Config{ChunkSize: 2} // Create 3 chunks
sort, outChan, errChan := extsort.Generic(
inputChan,
genericFromBytes,
genericToBytes, // This will return error for failing items
genericCompare,
config,
)
sort.Sort(context.Background())
// Drain output - should get fewer items due to error
outputCount := 0
for range outChan {
outputCount++
}
// Should get a SerializationError
err := <-errChan
if err == nil {
t.Fatal("Expected SerializationError but got nil")
}
var serErr *extsort.SerializationError
if !errors.As(err, &serErr) {
t.Errorf("Expected SerializationError, got %T: %v", err, err)
}
if !strings.Contains(err.Error(), "serialization failed") {
t.Errorf("Expected 'serialization failed' in error message, got: %v", err)
}
t.Logf("Successfully caught generic serialization error: %v", err)
}
// TestGenericDeserializationError tests deserialization error handling in the Generic API
func TestGenericDeserializationError(t *testing.T) {
inputChan := make(chan *GenericErrorItem, 5)
// Add more items to force multi-chunk and trigger deserialization
inputChan <- &GenericErrorItem{Key: 1, ShouldFailSerialization: false}
inputChan <- &GenericErrorItem{Key: 2, ShouldFailSerialization: false}
inputChan <- &GenericErrorItem{Key: 3, ShouldFailSerialization: false}
inputChan <- &GenericErrorItem{Key: 4, ShouldFailSerialization: false}
inputChan <- &GenericErrorItem{Key: 5, ShouldFailSerialization: false}
close(inputChan)
config := &extsort.Config{
ChunkSize: 2, // Force 3 chunks
NumWorkers: 1, // Single worker to avoid race conditions
}
sort, outChan, errChan := extsort.Generic(
inputChan,
failingGenericFromBytes, // This will always fail
genericToBytes,
genericCompare,
config,
)
sort.Sort(context.Background())
// Drain output
for range outChan {
// Should get no output due to deserialization error
}
// Should get a DeserializationError
err := <-errChan
if err == nil {
t.Fatal("Expected DeserializationError but got nil")
}
var deserErr *extsort.DeserializationError
if !errors.As(err, &deserErr) {
t.Errorf("Expected DeserializationError, got %T: %v", err, err)
}
t.Logf("Successfully caught generic deserialization error: %v", err)
}
// TestOrderedSerializationError tests gob encoding errors in Ordered API
func TestOrderedSerializationError(t *testing.T) {
// Note: It's hard to make gob encoding fail for basic types,
// so this test demonstrates the error handling path exists
inputChan := make(chan int, 2)
inputChan <- 1
inputChan <- 2
close(inputChan)
sort, outChan, errChan := extsort.Ordered(inputChan, nil)
sort.Sort(context.Background())
// Should complete successfully since int is easily serializable
outputCount := 0
for range outChan {
outputCount++
}
err := <-errChan
if err != nil {
t.Errorf("Unexpected error with basic int sorting: %v", err)
}
if outputCount != 2 {
t.Errorf("Expected 2 output items, got %d", outputCount)
}
t.Logf("Ordered API error handling infrastructure verified")
}
// TestGenericErrorPropagation tests that errors are properly propagated through all phases
func TestGenericErrorPropagation(t *testing.T) {
inputChan := make(chan *GenericErrorItem, 5)
// Add more items to force multi-chunk behavior
inputChan <- &GenericErrorItem{Key: 1, ShouldFailSerialization: false}
inputChan <- &GenericErrorItem{Key: 2, ShouldFailSerialization: true} // This will fail
inputChan <- &GenericErrorItem{Key: 3, ShouldFailSerialization: false}
inputChan <- &GenericErrorItem{Key: 4, ShouldFailSerialization: false}
inputChan <- &GenericErrorItem{Key: 5, ShouldFailSerialization: false}
close(inputChan)
config := &extsort.Config{
ChunkSize: 2, // Force multiple chunks
NumWorkers: 1, // Single worker to avoid race conditions in this test
}
sort, outChan, errChan := extsort.Generic(
inputChan,
genericFromBytes,
genericToBytes,
genericCompare,
config,
)
sort.Sort(context.Background())
// Drain output
outputCount := 0
for range outChan {
outputCount++
}
// Verify error propagation
err := <-errChan
if err == nil {
t.Fatal("Expected error to be properly propagated")
}
// Should be a wrapped SerializationError
if !strings.Contains(err.Error(), "saveChunk") {
t.Errorf("Expected error to mention 'saveChunk', got: %v", err)
}
t.Logf("Error properly propagated through phases: %v", err)
}
// Helper types and functions for generic error tests
type GenericErrorItem struct {
Key int `json:"key"`
ShouldFailSerialization bool `json:"shouldFailSerialization"`
}
func genericToBytes(item *GenericErrorItem) ([]byte, error) {
if item.ShouldFailSerialization {
return nil, errors.New("serialization failed for test")
}
return json.Marshal(item)
}
func genericFromBytes(data []byte) (*GenericErrorItem, error) {
var item GenericErrorItem
err := json.Unmarshal(data, &item)
return &item, err
}
func failingGenericFromBytes(data []byte) (*GenericErrorItem, error) {
return nil, errors.New("deserialization always fails for test")
}
func genericCompare(a, b *GenericErrorItem) int {
if a.Key < b.Key {
return -1
} else if a.Key > b.Key {
return 1
}
return 0
}