|
| 1 | +package resolve |
| 2 | + |
| 3 | +import ( |
| 4 | + "runtime" |
| 5 | + "runtime/debug" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + |
| 11 | + "github.com/wundergraph/astjson" |
| 12 | + "github.com/wundergraph/go-arena" |
| 13 | +) |
| 14 | + |
| 15 | +// TestCrossArenaMergeValuesCreatesShallowReferences proves that MergeValues |
| 16 | +// links *Value pointers from the source arena into the target arena's tree |
| 17 | +// without deep-copying. Resetting the source arena makes the merged values stale. |
| 18 | +// |
| 19 | +// This is the foundational invariant for AC-THREAD-04: goroutine arenas that |
| 20 | +// hold FromCache values must NOT be released before the response is fully rendered. |
| 21 | +func TestCrossArenaMergeValuesCreatesShallowReferences(t *testing.T) { |
| 22 | + old := debug.SetGCPercent(1) |
| 23 | + defer debug.SetGCPercent(old) |
| 24 | + |
| 25 | + mainArena := arena.NewMonotonicArena(arena.WithMinBufferSize(4096)) |
| 26 | + goroutineArena := arena.NewMonotonicArena(arena.WithMinBufferSize(4096)) |
| 27 | + |
| 28 | + // Parse entity data on the "goroutine" arena (simulates populateFromCache) |
| 29 | + fromCache, err := astjson.ParseBytesWithArena(goroutineArena, []byte(`{"id":"prod-1","name":"Widget"}`)) |
| 30 | + require.NoError(t, err) |
| 31 | + |
| 32 | + // Parse the target item on the main arena (simulates the response tree) |
| 33 | + item, err := astjson.ParseBytesWithArena(mainArena, []byte(`{"id":"prod-1"}`)) |
| 34 | + require.NoError(t, err) |
| 35 | + |
| 36 | + // Merge: this splices FromCache nodes into item's object tree |
| 37 | + merged, _, err := astjson.MergeValues(mainArena, item, fromCache) |
| 38 | + require.NoError(t, err) |
| 39 | + |
| 40 | + // Verify merged result contains data from both arenas |
| 41 | + mergedJSON := string(merged.MarshalTo(nil)) |
| 42 | + assert.Contains(t, mergedJSON, `"name":"Widget"`) |
| 43 | + assert.Contains(t, mergedJSON, `"id":"prod-1"`) |
| 44 | + |
| 45 | + // Force GC to stress-test pointer validity — goroutine arena is still alive |
| 46 | + runtime.GC() |
| 47 | + runtime.GC() |
| 48 | + |
| 49 | + // Values should still be valid since goroutine arena hasn't been reset |
| 50 | + postGCJSON := string(merged.MarshalTo(nil)) |
| 51 | + assert.Equal(t, mergedJSON, postGCJSON, |
| 52 | + "merged values should survive GC when goroutine arena is still alive") |
| 53 | + |
| 54 | + // Now reset the goroutine arena — simulates premature release |
| 55 | + goroutineArena.Reset() |
| 56 | + |
| 57 | + // Overwrite the freed memory with different data |
| 58 | + _, _ = astjson.ParseBytesWithArena(goroutineArena, []byte(`{"id":"STALE","name":"CORRUPTED"}`)) |
| 59 | + |
| 60 | + // The merged tree still holds pointers into the (now overwritten) goroutine arena. |
| 61 | + // This proves MergeValues is shallow — accessing the stale data may panic or |
| 62 | + // return corrupted values. |
| 63 | + staleOrPanicked := func() (result string, panicked bool) { |
| 64 | + defer func() { |
| 65 | + if r := recover(); r != nil { |
| 66 | + panicked = true |
| 67 | + } |
| 68 | + }() |
| 69 | + return string(merged.MarshalTo(nil)), false |
| 70 | + } |
| 71 | + staleJSON, panicked := staleOrPanicked() |
| 72 | + assert.True(t, panicked || staleJSON != mergedJSON, |
| 73 | + "merged values should be stale or inaccessible after goroutine arena reset — "+ |
| 74 | + "this proves MergeValues creates cross-arena shallow references") |
| 75 | + |
| 76 | + runtime.KeepAlive(mainArena) |
| 77 | + runtime.KeepAlive(goroutineArena) |
| 78 | +} |
| 79 | + |
| 80 | +// TestGoroutineArenaLifetimeWithDeferredRelease verifies the correct pattern: |
| 81 | +// goroutine arenas survive through the full resolve lifecycle and are only |
| 82 | +// released in Free(). This matches the Loader.goroutineArenas design. |
| 83 | +func TestGoroutineArenaLifetimeWithDeferredRelease(t *testing.T) { |
| 84 | + old := debug.SetGCPercent(1) |
| 85 | + defer debug.SetGCPercent(old) |
| 86 | + |
| 87 | + mainArena := arena.NewMonotonicArena(arena.WithMinBufferSize(4096)) |
| 88 | + |
| 89 | + // Simulate multiple goroutines, each with their own arena |
| 90 | + const numGoroutines = 4 |
| 91 | + goroutineArenas := make([]arena.Arena, numGoroutines) |
| 92 | + fromCacheValues := make([]*astjson.Value, numGoroutines) |
| 93 | + |
| 94 | + for i := range numGoroutines { |
| 95 | + goroutineArenas[i] = arena.NewMonotonicArena(arena.WithMinBufferSize(4096)) |
| 96 | + var err error |
| 97 | + fromCacheValues[i], err = astjson.ParseBytesWithArena( |
| 98 | + goroutineArenas[i], |
| 99 | + []byte(`{"id":"prod-`+stringFromInt(i+1)+`","name":"Product `+stringFromInt(i+1)+`"}`), |
| 100 | + ) |
| 101 | + require.NoError(t, err) |
| 102 | + } |
| 103 | + |
| 104 | + // Phase 4: merge all FromCache values into main arena tree |
| 105 | + items := make([]*astjson.Value, numGoroutines) |
| 106 | + for i := range numGoroutines { |
| 107 | + items[i], _ = astjson.ParseBytesWithArena(mainArena, []byte(`{"id":"prod-`+stringFromInt(i+1)+`"}`)) |
| 108 | + merged, _, err := astjson.MergeValues(mainArena, items[i], fromCacheValues[i]) |
| 109 | + require.NoError(t, err) |
| 110 | + items[i] = merged |
| 111 | + } |
| 112 | + |
| 113 | + // GC pressure — all arenas still alive |
| 114 | + runtime.GC() |
| 115 | + runtime.GC() |
| 116 | + |
| 117 | + // Verify all merged values are still valid (simulates response rendering) |
| 118 | + for i := range numGoroutines { |
| 119 | + json := string(items[i].MarshalTo(nil)) |
| 120 | + assert.Contains(t, json, `"name":"Product `+stringFromInt(i+1)+`"`, |
| 121 | + "merged value %d should be readable with goroutine arenas alive", i) |
| 122 | + } |
| 123 | + |
| 124 | + // Now release goroutine arenas (simulates Loader.Free()) |
| 125 | + for _, a := range goroutineArenas { |
| 126 | + a.Reset() |
| 127 | + } |
| 128 | + |
| 129 | + runtime.KeepAlive(mainArena) |
| 130 | + runtime.KeepAlive(goroutineArenas) |
| 131 | +} |
| 132 | + |
| 133 | +// Benchmark_CrossArenaGCSafety exercises the goroutine arena pattern under GC |
| 134 | +// pressure. Each iteration creates goroutine arenas, merges values, renders the |
| 135 | +// result, then releases. runtime.GC() between iterations maximizes pressure on |
| 136 | +// any dangling pointers. |
| 137 | +func Benchmark_CrossArenaGCSafety(b *testing.B) { |
| 138 | + old := debug.SetGCPercent(1) |
| 139 | + defer debug.SetGCPercent(old) |
| 140 | + |
| 141 | + entityJSON := []byte(`{"__typename":"Product","id":"prod-1","name":"Widget","price":9.99}`) |
| 142 | + itemJSON := []byte(`{"__typename":"Product","id":"prod-1"}`) |
| 143 | + |
| 144 | + b.ResetTimer() |
| 145 | + for b.Loop() { |
| 146 | + mainArena := arena.NewMonotonicArena(arena.WithMinBufferSize(4096)) |
| 147 | + goroutineArena := arena.NewMonotonicArena(arena.WithMinBufferSize(4096)) |
| 148 | + |
| 149 | + // Simulate goroutine: parse cached entity |
| 150 | + fromCache, err := astjson.ParseBytesWithArena(goroutineArena, entityJSON) |
| 151 | + if err != nil { |
| 152 | + b.Fatal(err) |
| 153 | + } |
| 154 | + |
| 155 | + // Simulate Phase 4: merge into response tree |
| 156 | + item, err := astjson.ParseBytesWithArena(mainArena, itemJSON) |
| 157 | + if err != nil { |
| 158 | + b.Fatal(err) |
| 159 | + } |
| 160 | + merged, _, err := astjson.MergeValues(mainArena, item, fromCache) |
| 161 | + if err != nil { |
| 162 | + b.Fatal(err) |
| 163 | + } |
| 164 | + |
| 165 | + // Simulate response rendering |
| 166 | + buf := merged.MarshalTo(nil) |
| 167 | + if len(buf) == 0 { |
| 168 | + b.Fatal("empty output") |
| 169 | + } |
| 170 | + |
| 171 | + // Release (correct order: goroutine arena after rendering) |
| 172 | + goroutineArena.Reset() |
| 173 | + mainArena.Reset() |
| 174 | + |
| 175 | + // GC pressure between iterations |
| 176 | + runtime.GC() |
| 177 | + } |
| 178 | +} |
0 commit comments