perf(execution): consolidate full checks at commit boundaries

This commit is contained in:
阿白叔 committed 2026-10-19 09:41:41 +08:00
1 parent 196c84b0d3
commit 0714c7753f
4 files changed
+241 -24

No files matched your search

@@ -1,5 +1,6 @@
import ast
from pathlib import Path
import re
import subprocess
import pytest
@@ -229,6 +230,103 @@ Require QP_App.TransactionCount=0,"identity failure must precede CAD writes"
''', after=change)
def _batch_readback_fixture(routines, *, fault=''):
# Exercise real restore/finish/guards, replacing only the external open and
# command transport with an in-memory CAD-shaped application.
routines = re.sub(r'(?ms)^Function QPReadCommand\([^\n]*\n.*?^End Function', '''Function QPReadCommand(ByVal Path)
Set QPReadCommand=Command
End Function''', routines)
routines = re.sub(r'(?ms)^Sub QPEnsureProject\([^\n]*\n.*?^End Sub', '''Sub QPEnsureProject(ByRef App, ByVal TargetPath)
App.ProjectPath=TargetPath
App.CloseCount=0
QP_OwnedProject=True
QP_OwnedProjectPath=TargetPath
''' + ('Set Attr=Other.FindAttribute("Value"): Attr.Value="DRIFT"' if fault == 'after_reopen' else '') + '''
End Sub''', routines)
injection = 'FullChecks=FullChecks+1\n'
if fault == 'before_save':
injection += 'If FullChecks=1 Then Other.FindAttribute("Value").Value="DRIFT"\n'
return routines.replace('Sub QPVerifyUntouched(ByRef Records)\n',
'Sub QPVerifyUntouched(ByRef Records)\n' + injection)
@pytest.mark.parametrize('fault', ['', 'before_save', 'after_reopen'])
def test_owned_batch_checks_full_state_at_save_and_disk_boundaries(tmp_path, fault):
run_vbs(tmp_path, 'part_number', '''
Dim FullChecks
FullChecks=0
Command.Add "project_path",QPProjectPath()
QP_BatchMode=True: QP_SessionMode=True
QP_SessionToken=String(32,"b")
QP_OwnedProject=True: QP_OwnedProjectPath=QPProjectPath()
QPRestoreAfterOperatorReplace Command,SnapshotPath
Require FullChecks=0,"full scans repeated before the commit boundary"
On Error Resume Next
QPFinishBatchItem
Failure=Err.Number
Err.Clear
On Error GoTo 0
''' + ('''
Require Failure=0,"valid batch failed"
Require FullChecks=2,"expected one full check before save and one after reopen"
Require QP_App.SaveCalled,"valid batch did not save"
Require InStr(QPReadUtf8(QP_Fso.BuildPath(QP_Root,"project-session-result.tsv")),"saved_ok")>0,"no verified result"
''' if not fault else '''
Require Failure<>0,"unselected drift passed the commit guard"
Require Not QP_Fso.FileExists(QP_Fso.BuildPath(QP_Root,"project-session-result.tsv")),"failure published a completion"
Require QP_App.SaveCalled=''' + ('True' if fault == 'after_reopen' else 'False') + ''',"wrong save boundary"
Require QP_BatchSaveCommitted=''' + ('True' if fault == 'after_reopen' else 'False') + ''',"saved uncertainty marker is wrong"
'''), after='''
Dim Other
Set Other=New ValueComponent
Other.UID="unselected": Other.Refdes="R2": Other.Orientation=0: Other.Scale=1
Set Ignored=AddValueAttr(Other,"Value","SAFE")
QP_App.Components.Add Other,True
QPWriteUtf8 SnapshotPath,QPReadUtf8(SnapshotPath) & QPUntouchedComponent(Other)
''', routines_transform=lambda routines: _batch_readback_fixture(routines, fault=fault))
@pytest.mark.parametrize('refuse_close', [False, True])
def test_verified_disk_recovery_closes_without_saving_or_reopening(tmp_path, refuse_close):
run_vbs(tmp_path, 'part_number', '''
Command.Add "recovery_part_name","NEW_NAME"
Command.Add "recovery_part_label","NEW_LABEL"
Command.Add "project_path",QPProjectPath()
QPRestoreAfterOperatorReplace Command,SnapshotPath
QPWriteUtf8 QP_Fso.BuildPath(QP_Root,"status.tsv"),"job_id" & vbTab & "state" & vbTab & "code" & vbTab & "message" & vbCrLf
QP_BatchMode=True
QP_OwnedProject=True: QP_OwnedProjectPath=QPProjectPath()
QP_App.RefuseClose=''' + ('True' if refuse_close else 'False') + '''
On Error Resume Next
QPRecoverSavedJob Command,SnapshotPath
Failure=Err.Number
Err.Clear
On Error GoTo 0
Require Not QP_App.SaveCalled,"read-only disk recovery saved again"
''' + ('''
Require Failure<>0,"unconfirmed recovery close was accepted"
Require InStr(QPReadUtf8(QP_Fso.BuildPath(QP_Root,"status.tsv")),"recovery_saved_verified")=0,"failure published proof"
''' if refuse_close else '''
Require Failure=0,"valid disk recovery failed"
Require Not QP_BatchMode,"read-only recovery will trigger a duplicate finish/save"
Require QP_App.CloseCount=1 And Len(QP_App.ProjectPath)=0,"verified recovery did not close"
Require Not QP_OwnedProject,"closed recovery retains stale ownership"
Require InStr(QPReadUtf8(QP_Fso.BuildPath(QP_Root,"status.tsv")),"recovery_saved_verified")>0,"missing disk proof"
'''))
def test_command_snapshot_validation_reads_the_file_once(tmp_path):
def count_reads(routines):
return routines.replace('Function QPReadSnapshot(ByVal Path)\n',
'Function QPReadSnapshot(ByVal Path)\n SnapshotReads=SnapshotReads+1\n')
run_vbs(tmp_path, 'part_number', '''
Dim SnapshotReads
SnapshotReads=0
QPRequireCommandSnapshot Command,SnapshotPath
Require SnapshotReads=1,"same boundary parsed its snapshot twice"
''', routines_transform=count_reads)
@pytest.mark.parametrize('mode', ['preserve', 'part_label', 'part_number'])
def test_recovery_repairs_attributes_without_native_replace(tmp_path, mode):
run_vbs(tmp_path, mode, '''
+57 -2
View File
@@ -310,11 +310,66 @@ Const VDM_COMPPIN = 4096
Const VD_ALL = 0
'''
UNTOUCHED_ROUTINES = ['QPUntouchedComponent', '?QPUntouchedFullState', 'QPVerifyUntouched',
UNTOUCHED_ROUTINES = ['QPUntouchedComponent', '?QPUntouchedFullState', '?QPUntouchedIdentity', 'QPVerifyUntouched',
'QPReadSnapshot', 'QPWriteUtf8', 'QPReadUtf8', 'QPEncode', 'QPDecode',
'QPFindCurrentAttribute', 'QPEffectiveAttributeValue', 'QPEffectiveComponentAttribute',
'QPSnapshotAttribute', 'QPOptionalAttributeField', 'QPSortSnapshotLines',
'QPConnectionSignature', 'QPComponentPins', 'QPInvalidatePinIndex', 'QPAddSortedSignature']
'QPConnectionSignature', '?QPConnectionSignatureFromPins', 'QPComponentPins', 'QPInvalidatePinIndex', 'QPAddSortedSignature']
def test_snapshot_attribute_reads_its_location_once(tmp_path):
fake = FULL_FAKE.replace('Function GetLocation()\n Dim Point:',
'Function GetLocation()\n LocationReads=LocationReads+1\n Dim Point:', 1)
output = run_vbs(tmp_path, ['QPSnapshotAttribute', 'QPOptionalAttributeField', 'QPEncode'], '''
Dim Attr, Text, LocationReads
LocationReads=0
Set Attr=New FullAttribute
Text=QPSnapshotAttribute("u1",Attr)
WScript.Echo LocationReads
WScript.Echo Replace(Text,vbCrLf,"")
''', LOG_STUB + fake)
assert output.splitlines() == ['1', 'A\tu1\tValue\tSAME\tSAME\t0\t0\t1\t2\t3\t0\t1']
def test_unselected_identity_comes_from_the_same_complete_attribute_read(tmp_path):
output = run_vbs(tmp_path, UNTOUCHED_ROUTINES, '''
Dim Comp, Attr, Text, Lines, Names, Values, Item
Set QP_App=New FakeApp
Set Comp=New FullComponent
Comp.UID="u2": Comp.Refdes="R2": Comp.RaiseOnFind=True
Names=Array("Part Number","Value","Part Name","Part Label")
Values=Array("00123","BASE","NAME","LABEL")
For Item=0 To 3
Set Attr=New FullAttribute
Attr.Name=Names(Item): Attr.Value=Values(Item): Attr.InstanceValue=""
If Item=1 Then Attr.InstanceValue="OVERRIDE"
Comp.Attributes.Add Attr,True
Next
Text=QPUntouchedComponent(Comp)
Lines=Split(Text,vbCrLf)
WScript.Echo Lines(0)
''', LOG_STUB + FULL_FAKE)
assert output == 'U\tu2\tR2\t00123\tOVERRIDE\tNAME\tLABEL'
def test_unselected_pin_snapshot_reuses_the_queried_pin_collection(tmp_path):
fake = FULL_FAKE.replace('Function QPSheetIdentity()\n',
'Function QPSheetIdentity()\n ContextReads=ContextReads+1\n')
output = run_vbs(tmp_path, UNTOUCHED_ROUTINES, '''
Dim Comp, Pin, Net, Conn, Text, ContextReads
ContextReads=0
Set QP_App=New FakeApp
Set Comp=New FullComponent: Comp.UID="u2": Comp.Refdes="R2"
Set Net=New FakeNet: Net.UID="net1": Net.LogicalNetName="GND"
Set Conn=New FakeConnection: Set Conn.Net=Net
Set Pin=New FakePin: Set Pin.Component=Comp: Set Pin.Connection=Conn: Pin.Number="1"
Pin.X=0: Pin.Y=0
QP_App.ActiveView.Pins.Add Pin,True
Text=QPUntouchedFullState(Comp)
WScript.Echo ContextReads
WScript.Echo CStr(CBool(InStr(Text,"net1|GND")>0))
''', LOG_STUB + fake)
assert output.splitlines() == ['2', 'True']
@pytest.mark.parametrize(('mutation', 'expected'), [