Files
windroost/p2_godot_sample/scripts/feedback_test.gd
T
wushenghua 7e1b3154d4 fix: pop tween tracking leak in FeedbackService
Second real memory-lifecycle fix from code audit (same class as the floating-
number dangling capture). FeedbackService.pop() stored each tween in the
session-long _pop_tweens dictionary but never removed the entry when the tween
finished, and a tween bound to a freed target dies before its finished signal
fires. Verified with a probe: 5 pops on freed controls left 5 dead ids.

Fix: connect the tween's finished signal to erase its own entry via the captured
int instance id (safe against dangling capture), and prune stale/invalid entries
at the start of pop() for the freed-target case. Added a regression check to
feedback_test: finished pops on live controls -> 0 tracked; freed-mid-pop ids
pruned on next pop -> <=1 tracked. 6 stages pass.

Full gate P2_CHECKS_PASS, all logs still 0 lambda-capture errors.
prototype/ untracked.
2026-11-18 03:59:26 +08:00

147 lines
5.0 KiB
GDScript

extends SceneTree
## FeedbackService evidence.
##
## Proves the presentation layer is tiered, bounded, returns to rest, and is
## reshaped by accessibility settings - without touching gameplay outcomes.
const OUTPUT_PATH := "res://artifacts/feedback_test.json"
var _problems: Array = []
var _steps: Array = []
func _init() -> void:
_run.call_deferred()
func _run() -> void:
var svc := FeedbackService.new()
root.add_child(svc)
svc.reset_counts()
# Tier mapping: small < medium < large by shake/hit-stop strength.
svc.request("click_enemy")
svc.request("shield_break")
svc.request("cast_ultimate")
_check(svc.request_count(0) == 1, "click_enemy should be small")
_check(svc.request_count(1) == 1, "shield_break should be medium")
_check(svc.request_count(2) == 1, "cast_ultimate should be large")
_record("tiers", {"small": svc.request_count(0), "medium": svc.request_count(1), "large": svc.request_count(2)})
# A pivot must shake then return exactly to its base position.
var pivot := Control.new()
pivot.position = Vector2(120, 340)
root.add_child(pivot)
svc.set_shake_target(pivot)
svc.configure(false, false) # normal motion
svc.reset_counts()
svc.request("shield_break")
_check(svc.is_shaking(), "medium event should start a shake")
# Step enough frames for the trauma to decay fully.
for i in range(200):
svc._process(0.016)
_check(not svc.is_shaking(), "shake should decay to rest")
_check(
pivot.position.is_equal_approx(Vector2(120, 340)),
"pivot must return to its exact base position, got %s" % str(pivot.position)
)
_record("shake_rest", {"base": "(120,340)", "after": str(pivot.position)})
# Reduced motion suppresses shake and hit-stop entirely.
svc.clear_all()
svc.configure(true, false)
svc.reset_counts()
var base_scale_before := Engine.time_scale
svc.request("cast_ultimate")
_check(not svc.is_shaking(), "reduced motion must suppress shake")
_check(not svc.is_hit_stop_active(), "reduced motion must suppress hit-stop")
_check(is_equal_approx(Engine.time_scale, base_scale_before), "reduced motion must not change time_scale")
_check(svc.request_count(2) == 1, "the event is still counted (feedback dropped, not event)")
_record("reduced_motion", {"shaking": svc.is_shaking(), "hit_stop": svc.is_hit_stop_active(), "time_scale": Engine.time_scale})
# Pop returns to rest and a second pop replaces the first.
svc.configure(false, false)
var btn := Button.new()
root.add_child(btn)
svc.pop(btn, 1.0)
svc.pop(btn, 1.0) # replace prior tween, must not error or stack
svc.reset_pop(btn)
_check(btn.scale.is_equal_approx(Vector2.ONE), "pop must restore scale to ONE")
_record("pop_rest", {"scale": str(btn.scale)})
# Pop tracking must not leak: a finished pop on a live control cleans up
# via its finished signal; a control freed mid-pop is pruned on next pop.
# Regression: all entries used to persist for the whole session.
svc.clear_all()
for i in range(4):
var live := Control.new()
root.add_child(live)
svc.pop(live, 1.0)
for i in range(30):
await process_frame
_check(svc.tracked_pop_count_for_test() == 0,
"finished pops on live controls must be untracked (got %d)" % svc.tracked_pop_count_for_test())
for i in range(4):
var doomed := Control.new()
root.add_child(doomed)
svc.pop(doomed, 1.0)
doomed.queue_free()
for i in range(5):
await process_frame
var keeper := Control.new()
root.add_child(keeper)
svc.pop(keeper, 1.0)
_check(svc.tracked_pop_count_for_test() <= 1,
"freed-mid-pop ids must be pruned (got %d)" % svc.tracked_pop_count_for_test())
_record("pop_no_leak", {"after_finished": 0, "after_free_then_pop": svc.tracked_pop_count_for_test()})
# clear_all restores time_scale even if a hit-stop was active.
svc.configure(false, false)
svc.request("cast_ultimate")
var during := Engine.time_scale
svc.clear_all()
_check(is_equal_approx(Engine.time_scale, 1.0), "clear_all must restore time_scale to 1.0 (was %s)" % str(during))
_record("clear_all", {"during": during, "after": Engine.time_scale})
root.remove_child(svc)
svc.free()
root.remove_child(pivot)
pivot.free()
root.remove_child(btn)
btn.free()
_finish()
func _finish() -> void:
var passed := _problems.is_empty()
var report := {
"schema": 1, "kind": "windroost_feedback_test",
"overall": "PASS" if passed else "FAIL",
"problems": _problems, "steps": _steps,
}
var artifact_dir := ProjectSettings.globalize_path("res://artifacts")
DirAccess.make_dir_recursive_absolute(artifact_dir)
var file := FileAccess.open(artifact_dir.path_join("feedback_test.json"), FileAccess.WRITE)
if file != null:
file.store_string(JSON.stringify(report, "\t", true))
file.close()
for entry in _steps:
print("FEEDBACK_STEP %s" % JSON.stringify(entry))
if passed:
print("FEEDBACK_PASS %d stages verified" % _steps.size())
else:
print("FEEDBACK_FAIL %s" % "; ".join(_problems))
quit(0 if passed else 1)
func _check(condition: bool, message: String) -> void:
if not condition:
_problems.append(message)
func _record(name: String, data: Dictionary) -> void:
var entry := data.duplicate()
entry["step"] = name
_steps.append(entry)