5

To work around another problem (EAI_AGAIN errors when pushing or running unit tests in a newly created scratch org from Jenkins CI using SFDX), I'd like to (gently) poll for the test results produced by sfdx force:apex:test:run to see if that helps. But the following command does not appear to be designed for that purpose.

The closest I can get is:

sfdx force:apex:test:report --testrunid 707K000001JUhgY --json --wait 2

but the result of that isn't JSON but rather the message:

Socket timeout occurred while listening for results.

and the exit status is 1, just as it is for other error conditions.

Is there an alternate, clean way, to poll say once every 30 seconds?

(Unanswered here Waiting for Test Report from Salesforce DX CLI.)

  • What about running an sfdx force:data:soql:query -t against ApexTestRunResult and doing a sleep between invocations? – David Reed Feb 8 at 16:32
  • @DavidReed Yes. Will give it a go (and expect it to work so if you post it as an answer +1 from me). – Keith C Feb 8 at 16:39
2

Here's my suggestion:

sfdx force:data:soql:query can run a Tooling API query with -t. Run something like

sfdx force:data:soql:query -t -q "SELECT Status FROM ApexTestRunResult WHERE Id = '$TEST_RUN_ID'" --json

Check the Status value, and sleep 30s if it's not 'Completed', iterating until that Status is reached.

  • Thanks David - works well! Note no rows returned until finished. – Keith C Feb 8 at 18:27
  • Interesting. Based on the available Status values and the use of an AsyncApexJob Id I was expecting it to return while processing. Glad the strategy worked nonetheless. – David Reed Feb 8 at 18:30
  • I didn't look this carefully enough; the query needs to be e.g. sfdx force:data:soql:query -t -q "SELECT Status FROM ApexTestRunResult where AsyncApexJobId = '707K000001JVY3v'" and that does return a row with a status of "Processing" then "Completed". That is TEST_RUN_ID is the AsyncApexJobId. – Keith C Feb 9 at 13:01
  • Thanks for running with it, @KeithC; apologies for not checking the exact semantics first! – David Reed Feb 11 at 2:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.