MCPcopy
hub / github.com/flosch/pongo2 / TestBugCycleSharedState

Function TestBugCycleSharedState

pongo2_issues_test.go:32–80  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

30}
31
32func TestBugCycleSharedState(t *testing.T) {
33 // Bug: tagCycleNode.idx was stored on the AST node, which is shared
34 // across all concurrent executions of the same parsed template.
35 // This caused two problems:
36 // 1. Data race: concurrent idx++ without synchronization
37 // 2. Semantic bug: one execution's cycle position affected another,
38 // producing wrong output (e.g., "bcabca" instead of "abcabc")
39 //
40 // Each template execution must have independent cycle state.
41
42 tpl, err := pongo2.FromString(`{% for i in items %}{% cycle "a" "b" "c" %}{% endfor %}`)
43 if err != nil {
44 t.Fatalf("failed to parse template: %v", err)
45 }
46
47 ctx := pongo2.Context{"items": []int{1, 2, 3, 4, 5, 6}}
48 const expected = "abcabc"
49
50 // First: verify sequential executions produce consistent results.
51 // With shared state on the node, the second execution would start
52 // where the first left off (idx=6), producing wrong output.
53 for i := 0; i < 5; i++ {
54 result, err := tpl.Execute(ctx)
55 if err != nil {
56 t.Fatalf("execution %d: unexpected error: %v", i, err)
57 }
58 if result != expected {
59 t.Errorf("execution %d: got %q, want %q", i, result, expected)
60 }
61 }
62
63 // Second: verify concurrent executions are also correct.
64 var wg sync.WaitGroup
65 for i := 0; i < 20; i++ {
66 wg.Add(1)
67 go func() {
68 defer wg.Done()
69 result, err := tpl.Execute(ctx)
70 if err != nil {
71 t.Errorf("concurrent: unexpected error: %v", err)
72 return
73 }
74 if result != expected {
75 t.Errorf("concurrent: got %q, want %q", result, expected)
76 }
77 }()
78 }
79 wg.Wait()
80}
81
82func TestBugIfchangedSharedState(t *testing.T) {
83 // Bug: tagIfchangedNode.lastValues and lastContent were stored on the

Callers

nothing calls this directly

Calls 2

FromStringMethod · 0.80
ExecuteMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…