-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathedge_cases_test.go
More file actions
307 lines (251 loc) · 7.1 KB
/
edge_cases_test.go
File metadata and controls
307 lines (251 loc) · 7.1 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
package zlog
import (
"io"
"os"
"strings"
"sync/atomic"
"testing"
)
// countingWriter is a test helper that counts writes
type countingWriter struct {
count *atomic.Int32
}
func (cw *countingWriter) Write(p []byte) (int, error) {
cw.count.Add(1)
return len(p), nil
}
func TestEdgeCases(t *testing.T) {
t.Run("FieldsLogDisabled", func(t *testing.T) {
logger := NewStructured()
logger.SetWriter(io.Discard)
logger.SetLevel(LevelError) // Disable Info
// This should not log
logger.Info("should not log", String("key", "value"))
})
t.Run("MessageTooLong", func(t *testing.T) {
logger := New()
logger.SetWriter(io.Discard)
// Create a very long message
longMsg := strings.Repeat("x", 300)
logger.Info(longMsg) // Should be truncated
})
t.Run("TooManyFields", func(t *testing.T) {
logger := NewStructured()
logger.SetWriter(io.Discard)
// Create 300 fields (more than 255 max)
fields := make([]Field, 300)
for i := range fields {
fields[i] = Int("key", i)
}
logger.Info("many fields", fields...)
})
t.Run("LongFieldKey", func(t *testing.T) {
logger := NewStructured()
logger.SetWriter(io.Discard)
// Create a field with very long key
longKey := strings.Repeat("k", 300)
logger.Info("test", String(longKey, "value"))
})
t.Run("LongStringValue", func(t *testing.T) {
logger := NewStructured()
logger.SetWriter(io.Discard)
// Create a very long string value
longValue := strings.Repeat("v", 70000)
logger.Info("test", String("key", longValue))
})
t.Run("LongBytesValue", func(t *testing.T) {
logger := NewStructured()
logger.SetWriter(io.Discard)
// Create very long bytes
longBytes := make([]byte, 70000)
logger.Info("test", Bytes("key", longBytes))
})
t.Run("BufferOverflow", func(t *testing.T) {
logger := NewStructured()
logger.SetWriter(io.Discard)
// Try to overflow the 1024 byte buffer
fields := make([]Field, 0)
for i := 0; i < 50; i++ {
fields = append(fields, String("very_long_key_name_here", "very_long_value_here"))
}
logger.Info("overflow test", fields...)
})
}
func TestTerminalWriterEdgeCases(t *testing.T) {
tw := &TerminalWriter{useColor: false}
t.Run("InvalidMagic", func(t *testing.T) {
data := make([]byte, 30)
_, err := tw.Write(data)
if err == nil {
t.Error("Expected error for invalid magic")
}
})
t.Run("TooShort", func(t *testing.T) {
data := make([]byte, 10)
_, err := tw.Write(data)
if err == nil {
t.Error("Expected error for too short data")
}
})
t.Run("UnknownFieldType", func(t *testing.T) {
got := tw.decodeFieldValue([]byte{}, FieldType(99))
if got != "?" {
t.Errorf("Expected ?, got %v", got)
}
})
t.Run("ShortBuffers", func(t *testing.T) {
// Test with buffers too short for the type
shortBuf := make([]byte, 2)
if got := tw.decodeFieldValue(shortBuf, FieldTypeInt); got != "?" {
t.Errorf("Expected ?, got %v for short int buffer", got)
}
if got := tw.decodeFieldValue(shortBuf, FieldTypeFloat32); got != "?" {
t.Errorf("Expected ?, got %v for short float32 buffer", got)
}
if got := tw.decodeFieldValue(shortBuf, FieldTypeFloat64); got != "?" {
t.Errorf("Expected ?, got %v for short float64 buffer", got)
}
})
t.Run("FieldValueSizeUnknown", func(t *testing.T) {
size := tw.fieldValueSize(nil, FieldType(99))
if size != 0 {
t.Errorf("Expected 0, got %v", size)
}
})
t.Run("FieldValueSizeString", func(t *testing.T) {
// Test string/bytes with valid length prefix in native byte order
buf := make([]byte, 10)
binaryEncodeUint16(buf, 5)
size := tw.fieldValueSize(buf, FieldTypeString)
if size != 7 { // 2 + 5
t.Errorf("Expected 7, got %v", size)
}
// Test with short buffer
size = tw.fieldValueSize([]byte{}, FieldTypeString)
if size != 0 {
t.Errorf("Expected 0, got %v", size)
}
})
}
func TestMMapWriterErrors(t *testing.T) {
t.Run("InvalidPath", func(t *testing.T) {
_, err := NewMMapWriter("/invalid/path/that/does/not/exist", 1024)
if err == nil {
t.Error("Expected error for invalid path")
}
})
t.Run("EmptyWrite", func(t *testing.T) {
tmpfile, _ := os.CreateTemp("", "mmap")
defer os.Remove(tmpfile.Name())
tmpfile.Close()
mw, _ := NewMMapWriter(tmpfile.Name(), 1024)
defer mw.Close()
// Write empty data
_, err := mw.Write([]byte{})
if err != nil {
t.Error("Empty write should succeed")
}
})
t.Run("WrapAround", func(t *testing.T) {
tmpfile, _ := os.CreateTemp("", "mmap")
defer os.Remove(tmpfile.Name())
tmpfile.Close()
mw, _ := NewMMapWriter(tmpfile.Name(), 100) // Very small buffer
defer mw.Close()
// Write enough to wrap around
for i := 0; i < 20; i++ {
mw.Write([]byte("test data"))
}
})
t.Run("CrossPageBoundary", func(t *testing.T) {
tmpfile, _ := os.CreateTemp("", "mmap")
defer os.Remove(tmpfile.Name())
tmpfile.Close()
pageSize := os.Getpagesize()
mw, _ := NewMMapWriter(tmpfile.Name(), int64(pageSize*2))
defer mw.Close()
// Write data that crosses page boundary
data := make([]byte, pageSize+100)
mw.Write(data)
})
t.Run("FileCreation", func(t *testing.T) {
// Try to create mmap with new file
tmpDir, _ := os.MkdirTemp("", "mmap_test")
defer os.RemoveAll(tmpDir)
newFile := tmpDir + "/new_file.log"
mw, err := NewMMapWriter(newFile, 1024)
if err != nil {
t.Errorf("Failed to create new file: %v", err)
} else {
mw.Close()
}
})
}
func TestAsyncWriterEdgeCases(t *testing.T) {
t.Run("BufferFull", func(t *testing.T) {
var writeCount atomic.Int32
// Create a custom writer that counts writes
cw := &countingWriter{
count: &writeCount,
}
aw := NewAsyncWriter(cw, 16)
defer aw.Close()
// Write many items
for i := 0; i < 100; i++ {
_, err := aw.Write([]byte("test"))
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
}
// Verify writes happened (either async or direct)
if writeCount.Load() == 0 {
t.Error("No writes occurred")
}
})
t.Run("EmptyRingBuffer", func(t *testing.T) {
rb := NewCompatRingBuffer(16)
// Try to put empty data
if !rb.Put([]byte{}) {
t.Error("Empty put should succeed")
}
// Try to get from empty buffer after consuming
data, ok := rb.Get()
if !ok || len(data) != 0 {
t.Error("Expected empty data")
}
})
}
func TestRingBufferPanic(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Error("Expected panic for non-power-of-2 size")
}
}()
// This should panic
NewCompatRingBuffer(15) // Not power of 2
}
func TestEscapeStringLongUnicode(t *testing.T) {
// Test with unicode that needs allocation
longUnicode := "hello " + strings.Repeat("世", 50) + " world" // Add spaces to trigger quoting
result := escapeString(longUnicode)
if !strings.HasPrefix(result, `"`) {
t.Error("Expected quoted string")
}
}
func TestUltimateLoggerLongMessage(t *testing.T) {
logger := NewUltimateLogger()
// Test with message longer than 200 chars
longMsg := strings.Repeat("x", 250)
logger.Info(longMsg)
logger.Debug(longMsg)
logger.Error(longMsg)
// Test wrap around
for i := 0; i < 1000000; i++ {
logger.Info("wrap test")
}
}
func TestStderrWriter(t *testing.T) {
// Just verify it doesn't panic
w := os.Stderr
w.Write([]byte("test"))
}