Repository navigation
Conversation
…st Run form Fixes jenkinsci#189. HtmlUnit runs timers and fetch/XHR callbacks on its own thread while the test reads the DOM. The Builds card on the job pages these fixtures open fetches the builds list on load and every 5 seconds and replaces its contents, so an XPath walk could meet a removed node (DOM2DTM NPE). waitForBackgroundJavaScript(5000) never saw an empty queue because that refresh re-arms itself, so it returned when the next refresh started. PageScriptFixtures.settled runs every job due now to the end, cancels the remaining timers and returns when no job is queued or running; from then on only the test thread can start a script. The Request Run form, rerun fallback form and dialog fixtures, parameterBlock, submit, and the tests that submitted the form and slept 5 seconds use it. src/test only. Signed-off-by: Shubham <[email protected]>
YongGoose
left a comment
There was a problem hiding this comment.
Thanks @shuseng8. The root-cause write-up is excellent: the Builds card's 5-second refresh explains both failures in #189, and it explains why waitForBackgroundJavaScript(5000) never came back empty. Waiting for a state rather than a time is the right idea, and ci.jenkins.io ran the whole suite green on e267b5d.
One thing to change, inline. The GitHub Actions workflows (Build, Windows tests, E2E) are waiting for approval on this PR. I'll get them running after your next push. The Windows run matters here, because #173's flake came from a slow Windows runner.
| jobs.waitForJobsStartingBefore(DUE_NOW_MS); | ||
| // Cancel the timers that are left (the Builds card's next refresh). A job that | ||
| // started in the meantime keeps the count above 0, so the loop checks again. | ||
| jobs.removeAllJobs(); |
There was a problem hiding this comment.
waitForJobsStartingBefore(DUE_NOW_MS) fixes its deadline when it is called: now + 1 s. A job that starts before the deadline but runs past it, such as a request on a slow runner (in #173 a Windows runner took about 16 s to load a page), can then schedule its continuation (setTimeout(…, 1), as you found for the Builds card) after the deadline. That continuation is not waited for, and removeAllJobs() on the next line drops it. So this can cancel more than the far-off refresh:
- whatever that continuation renders never appears. If it is the CRED list fill, T-05-105/106 fail after their 30 s wait;
- a check that something does not change later passes vacuously, because the later change was cancelled.
Could you cancel only what is not due soon? Keep calling waitForJobsStartingBefore(DUE_NOW_MS) until nothing is running and getEarliestJob() is either null or has a getTargetExecutionTime() more than DUE_NOW_MS ahead. Only then call removeAllJobs(). The state argument still holds when it returns (count 0), and a job that was due within the window can never be cancelled, however slow the runner is.
Signed-off-by: Shubham <[email protected]>
|
Thanks @YongGoose, good catch. |
YongGoose
left a comment
There was a problem hiding this comment.
Thanks @shuseng8, this is exactly the change I asked for. I read isDueSoon against HtmlUnit's job manager:
- A continuation that a slow job schedules after the deadline is now waited for, because it is queued and due within
DUE_NOW_MS. Only timers further out are cancelled. - If a job is still running while only a far-off timer is queued,
removeAllJobs()cancels just that timer. The running job and anything it schedules afterwards keep the count above 0, so the outer loop waits for them. - If the deadline passes while a job is still due soon, nothing is cancelled and the test fails with the job dump. That is the loud failure the class comment promises.
The RequestPrefillGapTest place from #189 is covered too: line 199 reads a page that TypedParameterFixtures.submit now settles.
Every check on 7c1cfdb is green, including the seven Windows groups and ci.jenkins.io's windows-21 build.
What this changes
Tests that read the Request Run form now wait for the page's background JavaScript to finish, and for nothing to be left that could run later, before they query the DOM. Only
src/testchanges.Cause. HtmlUnit runs
setTimeoutcallbacks andfetch/XMLHttpRequestcallbacks as jobs on its own thread, the client's JavaScript executor. The test reads the DOM on the test thread, and nothing locks the DOM between the two threads. The job pages these fixtures open include core's Builds card (jsbundles/pages/project/builds-card.js). It fetches the builds list when the page loads, then again every 5 seconds (updateBuildsRefreshInterval = 5000), and each time it replaces its contents withinnerHTML. If an XPath walk (DOM2DTM) is running when that subtree is replaced, it reaches a removed node and fails withnode is null. This is the error in the issue. I checked the queue right afterrequestRunFormloads the page: it holds asetTimeout(…, 1)(the first fetch's continuation) and thesetTimeout(() => load(), 5000)refresh. The refresh re-arms itself, sowc.waitForBackgroundJavaScript(5000)insubmitnever saw an empty queue. It always waited the full 5 seconds and returned just as the next refresh started. That explains the second failure in the issue (RequestPrefillGapTest:199,setFileon the page thatsubmitreturned).What the wait waits for. The new
PageScriptFixtures.settled(page)/settle(webClient)loops over the client's windows and, for each job manager that still has jobs:waitForJobsStartingBefore);removeAllJobs);getJobCount() == 0.Why that is always enough.
JavaScriptJobManagerImpltakes a job off its queue and marks it as the running job inside a singlesynchronizedblock, andgetJobCount()counts queued plus running jobs under the same lock. A count of 0 therefore means that nothing is queued and nothing is running. Jobs are created only by JavaScript, so after that point a job can start only if the test thread itself runs a script (a click, or a value change that firesonchange). No background script can change the DOM while the test reads it. The guarantee comes from that state, not from timing, so a slow machine does not break it. The bound (60 s) only matters for a page that never stops scheduling short timers, and then the test fails with the job dump instead of hanging or racing.Where it is used.
TypedParameterFixtures.requestRunForm: after loading the Request Run page.TypedParameterFixtures.parameterBlock, the XPath entry point ofsetFile,setValueandvalueOf: before every query. This covers forms that a test loaded by itself (for exampleRefusedBuildPrefillTestand the core build form inCredentialsParameterTest/RunParameterTest) and scripts started by the last change to a control. On a page that has already settled it returns at once.TypedParameterFixtures.submit: replaceswaitForBackgroundJavaScript(5000). This coversRequestPrefillGapTest:199, which reads the refused page thatsubmitreturns.RerunFallbackFixtures.openFormandnotices: the rerun fallback form, also used byRequestPrefillGapTest.DialogFixtures.openDialog: after the job page loads and after the fragment is inserted (it waited 5 s before, with the same race).RequestScreenGuardTest,RequestFormEncodingTest,RequestFormControlsTest,MultiApproverTest,ApproverIdStrategyTest.TypedParameterIntegrityTest.assertFieldRefusal,JobRunSidebarEntryTest(submitParametersand the landing page), andDialogTypedParameterTest.fileControls.I left the approval-page and dialog-confirmation waits alone (
DecisionAreaFixtures,RejectConfirmationTest,RevertPlacementTest, the naginator Retry click inRefusedRerunAuditTest, and the Cancel click inRequestFormControlsTest). They do not read a Request Run form, and some of them wait for a dialog script on purpose.Side effect: each form submission no longer sleeps 5 s, so these classes run faster.
Anchor
chore.Testing done
mvn -ntp test -DforkCount=6 -Dtest=TypedParameterValuesTest,RequestPrefillGapTest,DialogTypedParameterTest,CredentialsParameterTest,RunParameterTest,RequestScreenGuardTest,RequestFormEncodingTest,TypedParameterIntegrityTest,JobRunSidebarEntryTest,MultiApproverTest,ApproverIdStrategyTest,RequestFormControlsTest,RefusedBuildPrefillTest,IncidentRerunGapTest(JDK 21.0.13, Maven 3.9.9): 102 tests, 0 failures, 0 errors.mvn -ntp clean verify -DforkCount=6: stopped after 66 test classes / 355 tests had finished (0 failures, 0 errors) because the whole suite would take several hours on this machine. Thenmvn -ntp verify -DskipTests:BugInstance size is 0,spotless:checkpassed, BUILD SUCCESS.job/<name>/batch-control/with the fixtures'browser(...)client and dumped the job queue every 200 ms for 60 s. AsetTimeout(() => load(), 5000)re-armed every 5 s for the whole run, and each refresh came with asetTimeout(…, 1)continuation.Checks
mainbranch of your fork.buildworkflow passed on that branch in your fork before this pull request was opened. Not run: workflows are not enabled on my fork. Local results are above.mvn clean verifypassed locally: partial run, see above (0 failures in the 355 tests that ran; SpotBugsBugInstance size is 0).