Repository navigation
Expand file tree
/
Copy pathcached_test.go
More file actions
581 lines (496 loc) · 13.9 KB
/
Copy pathcached_test.go
File metadata and controls
581 lines (496 loc) · 13.9 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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
package miya
import (
"sync"
"testing"
)
// TestStringBuilderPool tests the StringBuilderPool functions
func TestStringBuilderPool(t *testing.T) {
t.Run("GetStringBuilder returns a builder", func(t *testing.T) {
sb := GetStringBuilder()
if sb == nil {
t.Fatal("GetStringBuilder returned nil")
}
// Write something to verify it works
sb.WriteString("hello")
if sb.String() != "hello" {
t.Errorf("Expected 'hello', got '%s'", sb.String())
}
PutStringBuilder(sb)
})
t.Run("PutStringBuilder resets the builder", func(t *testing.T) {
sb := GetStringBuilder()
sb.WriteString("test data")
PutStringBuilder(sb)
// Get another builder (might be the same one from pool)
sb2 := GetStringBuilder()
if sb2.Len() != 0 {
t.Errorf("Expected empty builder, got length %d", sb2.Len())
}
PutStringBuilder(sb2)
})
t.Run("Concurrent access is safe", func(t *testing.T) {
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func(n int) {
defer wg.Done()
sb := GetStringBuilder()
sb.WriteString("concurrent test")
_ = sb.String()
PutStringBuilder(sb)
}(i)
}
wg.Wait()
})
}
// TestContextPool tests the ContextPool functions
func TestContextPool(t *testing.T) {
t.Run("GetContext returns a context", func(t *testing.T) {
ctx := GetContext()
if ctx == nil {
t.Fatal("GetContext returned nil")
}
// Verify it works
ctx.Set("key", "value")
val, ok := ctx.Get("key")
if !ok || val != "value" {
t.Errorf("Context not working correctly")
}
PutContext(ctx)
})
t.Run("PutContext handles nil", func(t *testing.T) {
// Should not panic
PutContext(nil)
})
t.Run("Concurrent access is safe", func(t *testing.T) {
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func(n int) {
defer wg.Done()
ctx := GetContext()
ctx.Set("key", n)
PutContext(ctx)
}(i)
}
wg.Wait()
})
}
// TestCachedTemplate tests the CachedTemplate functionality
func TestCachedTemplate(t *testing.T) {
t.Run("NewCachedTemplate creates a wrapper", func(t *testing.T) {
env := NewEnvironment()
tmpl, err := env.FromString("Hello {{ name }}!")
if err != nil {
t.Fatalf("Failed to create template: %v", err)
}
cached := NewCachedTemplate(tmpl)
if cached == nil {
t.Fatal("NewCachedTemplate returned nil")
}
if cached.Template != tmpl {
t.Error("CachedTemplate does not wrap original template")
}
})
t.Run("RenderCached renders correctly", func(t *testing.T) {
env := NewEnvironment()
tmpl, err := env.FromString("Hello {{ name }}!")
if err != nil {
t.Fatalf("Failed to create template: %v", err)
}
cached := NewCachedTemplate(tmpl)
ctx := NewContextFrom(map[string]interface{}{"name": "World"})
result, err := cached.RenderCached(ctx)
if err != nil {
t.Fatalf("RenderCached failed: %v", err)
}
if result != "Hello World!" {
t.Errorf("Expected 'Hello World!', got '%s'", result)
}
})
t.Run("RenderCached handles concurrent calls", func(t *testing.T) {
env := NewEnvironment()
tmpl, err := env.FromString("Value: {{ n }}")
if err != nil {
t.Fatalf("Failed to create template: %v", err)
}
cached := NewCachedTemplate(tmpl)
var wg sync.WaitGroup
for i := 0; i < 50; i++ {
wg.Add(1)
go func(n int) {
defer wg.Done()
ctx := NewContextFrom(map[string]interface{}{"n": n})
_, err := cached.RenderCached(ctx)
if err != nil {
t.Errorf("RenderCached failed: %v", err)
}
}(i)
}
wg.Wait()
})
}
// TestRenderContext tests the renderContext helper
func TestRenderContext(t *testing.T) {
t.Run("reset clears builder and visited map", func(t *testing.T) {
rc := &renderContext{
builder: GetStringBuilder(),
visited: make(map[string]bool),
}
rc.builder.WriteString("some content")
rc.visited["key1"] = true
rc.visited["key2"] = true
rc.reset()
if rc.builder.Len() != 0 {
t.Errorf("Builder not reset, length: %d", rc.builder.Len())
}
if len(rc.visited) != 0 {
t.Errorf("Visited map not cleared, length: %d", len(rc.visited))
}
PutStringBuilder(rc.builder)
})
}
// TestSlicePool tests the SlicePool functionality
func TestSlicePool(t *testing.T) {
t.Run("GetStringSlice returns a slice", func(t *testing.T) {
slice := GlobalSlicePool.GetStringSlice()
if slice == nil {
t.Fatal("GetStringSlice returned nil")
}
if len(slice) != 0 {
t.Errorf("Expected empty slice, got length %d", len(slice))
}
// Use it
slice = append(slice, "a", "b", "c")
if len(slice) != 3 {
t.Errorf("Expected length 3, got %d", len(slice))
}
GlobalSlicePool.PutStringSlice(slice)
})
t.Run("PutStringSlice resets the slice", func(t *testing.T) {
slice := GlobalSlicePool.GetStringSlice()
slice = append(slice, "x", "y", "z")
GlobalSlicePool.PutStringSlice(slice)
// Get another slice
slice2 := GlobalSlicePool.GetStringSlice()
if len(slice2) != 0 {
t.Errorf("Expected empty slice, got length %d", len(slice2))
}
GlobalSlicePool.PutStringSlice(slice2)
})
t.Run("GetIntSlice returns a slice", func(t *testing.T) {
slice := GlobalSlicePool.GetIntSlice()
if slice == nil {
t.Fatal("GetIntSlice returned nil")
}
if len(slice) != 0 {
t.Errorf("Expected empty slice, got length %d", len(slice))
}
// Use it
slice = append(slice, 1, 2, 3, 4, 5)
if len(slice) != 5 {
t.Errorf("Expected length 5, got %d", len(slice))
}
GlobalSlicePool.PutIntSlice(slice)
})
t.Run("PutIntSlice resets the slice", func(t *testing.T) {
slice := GlobalSlicePool.GetIntSlice()
slice = append(slice, 10, 20, 30)
GlobalSlicePool.PutIntSlice(slice)
// Get another slice
slice2 := GlobalSlicePool.GetIntSlice()
if len(slice2) != 0 {
t.Errorf("Expected empty slice, got length %d", len(slice2))
}
GlobalSlicePool.PutIntSlice(slice2)
})
t.Run("Concurrent access is safe", func(t *testing.T) {
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(2)
go func() {
defer wg.Done()
s := GlobalSlicePool.GetStringSlice()
s = append(s, "test")
GlobalSlicePool.PutStringSlice(s)
}()
go func() {
defer wg.Done()
s := GlobalSlicePool.GetIntSlice()
s = append(s, 42)
GlobalSlicePool.PutIntSlice(s)
}()
}
wg.Wait()
})
}
// TestFastStringBuilder tests the FastStringBuilder functionality
func TestFastStringBuilder(t *testing.T) {
t.Run("NewFastStringBuilder creates builder with capacity", func(t *testing.T) {
fsb := NewFastStringBuilder(100)
if fsb == nil {
t.Fatal("NewFastStringBuilder returned nil")
}
if fsb.Len() != 0 {
t.Errorf("Expected length 0, got %d", fsb.Len())
}
})
t.Run("WriteString appends strings", func(t *testing.T) {
fsb := NewFastStringBuilder(64)
fsb.WriteString("Hello")
fsb.WriteString(" ")
fsb.WriteString("World")
if fsb.String() != "Hello World" {
t.Errorf("Expected 'Hello World', got '%s'", fsb.String())
}
if fsb.Len() != 11 {
t.Errorf("Expected length 11, got %d", fsb.Len())
}
})
t.Run("WriteByte appends bytes", func(t *testing.T) {
fsb := NewFastStringBuilder(64)
fsb.WriteString("AB")
err := fsb.WriteByte('C')
if err != nil {
t.Errorf("WriteByte returned error: %v", err)
}
if fsb.String() != "ABC" {
t.Errorf("Expected 'ABC', got '%s'", fsb.String())
}
})
t.Run("Reset clears the buffer", func(t *testing.T) {
fsb := NewFastStringBuilder(64)
fsb.WriteString("some content")
fsb.Reset()
if fsb.Len() != 0 {
t.Errorf("Expected length 0 after reset, got %d", fsb.Len())
}
if fsb.String() != "" {
t.Errorf("Expected empty string after reset, got '%s'", fsb.String())
}
})
t.Run("Handles large content", func(t *testing.T) {
fsb := NewFastStringBuilder(16) // Start small
for i := 0; i < 1000; i++ {
fsb.WriteString("x")
}
if fsb.Len() != 1000 {
t.Errorf("Expected length 1000, got %d", fsb.Len())
}
})
}
// TestFastStringBuilderPool tests the FastStringBuilder pool
func TestFastStringBuilderPool(t *testing.T) {
t.Run("GetFastStringBuilder returns a builder", func(t *testing.T) {
fsb := GetFastStringBuilder()
if fsb == nil {
t.Fatal("GetFastStringBuilder returned nil")
}
fsb.WriteString("test")
PutFastStringBuilder(fsb)
})
t.Run("PutFastStringBuilder resets the builder", func(t *testing.T) {
fsb := GetFastStringBuilder()
fsb.WriteString("content")
PutFastStringBuilder(fsb)
fsb2 := GetFastStringBuilder()
if fsb2.Len() != 0 {
t.Errorf("Expected empty builder, got length %d", fsb2.Len())
}
PutFastStringBuilder(fsb2)
})
t.Run("Large builders are not returned to pool", func(t *testing.T) {
fsb := GetFastStringBuilder()
// Write more than 64KB
largeContent := make([]byte, 65*1024)
for i := range largeContent {
largeContent[i] = 'x'
}
fsb.WriteString(string(largeContent))
// This should not panic and should not return to pool
PutFastStringBuilder(fsb)
})
t.Run("Concurrent access is safe", func(t *testing.T) {
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
defer wg.Done()
fsb := GetFastStringBuilder()
fsb.WriteString("concurrent test")
_ = fsb.String()
PutFastStringBuilder(fsb)
}()
}
wg.Wait()
})
}
// TestTemplateCache tests the LRU template cache
func TestTemplateCache(t *testing.T) {
t.Run("NewTemplateCache creates empty cache", func(t *testing.T) {
cache := NewTemplateCache(10)
if cache == nil {
t.Fatal("NewTemplateCache returned nil")
}
if len(cache.cache) != 0 {
t.Errorf("Expected empty cache, got %d items", len(cache.cache))
}
})
t.Run("Put and Get work correctly", func(t *testing.T) {
cache := NewTemplateCache(10)
env := NewEnvironment()
tmpl, _ := env.FromString("Hello {{ name }}")
cache.Put("key1", tmpl)
retrieved, exists := cache.Get("key1")
if !exists {
t.Fatal("Expected to find template in cache")
}
if retrieved != tmpl {
t.Error("Retrieved template doesn't match stored template")
}
})
t.Run("Get returns false for missing keys", func(t *testing.T) {
cache := NewTemplateCache(10)
_, exists := cache.Get("nonexistent")
if exists {
t.Error("Expected exists to be false for missing key")
}
})
t.Run("LRU eviction works", func(t *testing.T) {
cache := NewTemplateCache(3)
env := NewEnvironment()
tmpl1, _ := env.FromString("Template 1")
tmpl2, _ := env.FromString("Template 2")
tmpl3, _ := env.FromString("Template 3")
tmpl4, _ := env.FromString("Template 4")
cache.Put("key1", tmpl1)
cache.Put("key2", tmpl2)
cache.Put("key3", tmpl3)
// Access key1 to make it recently used
cache.Get("key1")
// Add key4, should evict key2 (least recently used)
cache.Put("key4", tmpl4)
// key2 should be evicted
_, exists := cache.Get("key2")
if exists {
t.Error("key2 should have been evicted")
}
// key1 should still exist
_, exists = cache.Get("key1")
if !exists {
t.Error("key1 should still exist")
}
})
t.Run("Put updates existing key", func(t *testing.T) {
cache := NewTemplateCache(10)
env := NewEnvironment()
tmpl1, _ := env.FromString("Original")
tmpl2, _ := env.FromString("Updated")
cache.Put("key", tmpl1)
cache.Put("key", tmpl2)
retrieved, _ := cache.Get("key")
if retrieved != tmpl2 {
t.Error("Template should have been updated")
}
})
t.Run("Concurrent access is safe", func(t *testing.T) {
cache := NewTemplateCache(100)
env := NewEnvironment()
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(2)
go func(n int) {
defer wg.Done()
tmpl, _ := env.FromString("Template")
cache.Put(string(rune('a'+n%26)), tmpl)
}(i)
go func(n int) {
defer wg.Done()
cache.Get(string(rune('a' + n%26)))
}(i)
}
wg.Wait()
})
}
// TestCachedEnvironment tests the CachedEnvironment functionality
func TestCachedEnvironment(t *testing.T) {
t.Run("NewCachedEnvironment creates environment", func(t *testing.T) {
env := NewCachedEnvironment()
if env == nil {
t.Fatal("NewCachedEnvironment returned nil")
}
if env.Environment == nil {
t.Error("Environment is nil")
}
if env.templateCache == nil {
t.Error("templateCache is nil")
}
})
t.Run("NewCachedEnvironment accepts options", func(t *testing.T) {
env := NewCachedEnvironment(WithAutoEscape(true))
if env == nil {
t.Fatal("NewCachedEnvironment returned nil")
}
})
t.Run("FromStringCached compiles and caches", func(t *testing.T) {
env := NewCachedEnvironment()
source := "Hello {{ name }}!"
// First call - should compile
tmpl1, err := env.FromStringCached(source)
if err != nil {
t.Fatalf("FromStringCached failed: %v", err)
}
// Second call - should return cached
tmpl2, err := env.FromStringCached(source)
if err != nil {
t.Fatalf("FromStringCached failed: %v", err)
}
// Should be the same template instance
if tmpl1 != tmpl2 {
t.Error("Expected same template instance from cache")
}
})
t.Run("FromStringCached returns error for invalid template", func(t *testing.T) {
env := NewCachedEnvironment()
_, err := env.FromStringCached("{% if %}")
if err == nil {
t.Error("Expected error for invalid template")
}
})
t.Run("Cached template renders correctly", func(t *testing.T) {
env := NewCachedEnvironment()
tmpl, err := env.FromStringCached("Hello {{ name }}!")
if err != nil {
t.Fatalf("FromStringCached failed: %v", err)
}
ctx := NewContextFrom(map[string]interface{}{"name": "World"})
result, err := tmpl.Render(ctx)
if err != nil {
t.Fatalf("Render failed: %v", err)
}
if result != "Hello World!" {
t.Errorf("Expected 'Hello World!', got '%s'", result)
}
})
t.Run("Concurrent FromStringCached is safe", func(t *testing.T) {
env := NewCachedEnvironment()
var wg sync.WaitGroup
sources := []string{
"Template A: {{ a }}",
"Template B: {{ b }}",
"Template C: {{ c }}",
}
for i := 0; i < 100; i++ {
wg.Add(1)
go func(n int) {
defer wg.Done()
source := sources[n%len(sources)]
_, err := env.FromStringCached(source)
if err != nil {
t.Errorf("FromStringCached failed: %v", err)
}
}(i)
}
wg.Wait()
})
}