function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Test classes: any way to "slow down" or impose delays in execution?

Hi there,


I have some classes that are testing some trigger code.  During the testing, a number of dependant records need to be created before the test can begin, at least 7 or so DML inserts, putting in anywhere from 1 to 20 records at a time.  All of these objects have their own triggers that also do some inserts and record creation. (my project involves keeping a pseudo-change log of records and specific fields)


What I'm finding is that the first layer of DML inserts (those inside the test class) seem to fire alright, but DMLs further down the chain (inside the triggers that belong to the objects I'm creating) are slow to execute, or perhaps more correctly, execute asynchronously from the perspective of the test class's flow.


My test class is dependent on the operations that happen in those triggers, and since the test class seems to "race" ahead, parts of the code I'm testing never get covered because those child triggers haven't finished doing what they need to do.


Is there any way to impose some kind of delay, some kind of "wait until this other execution stack is finished running" before moving forward with the test, or some kind of "commit all" that forces SF to execute all pending transactions before continuing?


This same problem has also caused some confusion for me it terms of governor limits.  Using things like Limit.getQueries() to check improvements to optimization has been confusing, as the test class races ahead to completion before counting up any SOQL queries executed in child triggers.


Watching debug statements in the system log while doing things manually through the browser and UI tells a much more accurate story about what's happening, something I may have missed if I only relied on test classes.


Any ideas?


Cheers, and Happy Holidays!

SScholtzSScholtz ideas from the community?