Skip to content

Instantly share code, notes, and snippets.

@pcon
Last active November 25, 2019 15:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save pcon/7452472 to your computer and use it in GitHub Desktop.
Save pcon/7452472 to your computer and use it in GitHub Desktop.
/**
* Sleep at least a second
*
* Found at: http://boards.developerforce.com/t5/Apex-Code-Development/Best-way-to-delay-until-time-changes-in-a-test/td-p/389619
*
*/
public static void waitAtLeastASecond() {
Integer msPerS = 1000;
Datetime start = Datetime.now();
Datetime current = Datetime.now();
// No sleep available so this ugliness
Integer counter = 0;
while ((current.getTime() / msPerS) == (start.getTime() / msPerS)) {
// This code takes about 250ms or more on na3
Long t1 = System.currentTimeMillis();
String bigRandomString = '';
for (Integer i = 0; i < 2000; i++) {
bigRandomString += Crypto.getRandomLong();
}
for (Integer i = 0; i < 50; i++) {
Blob cryptoKey = Crypto.generateAesKey(256);
Blob data = Blob.valueOf(bigRandomString);
Blob encryptedData = Crypto.encryptWithManagedIV('AES256', cryptoKey, data);
Blob decryptedData = Crypto.decryptWithManagedIV('AES256', cryptoKey, encryptedData);
}
Long t2 = System.currentTimeMillis();
System.debug('>>> delayUntilTimeChanged delayed for ' + (t2 - t1) + ' ms' +
', Count ' + counter +
', ScriptStatements ' + Limits.getScriptStatements() + ' of ' + Limits.getLimitScriptStatements() +
', CpuTime ' + Limits.getCpuTime() + ' of ' + Limits.getLimitCpuTime());
counter++;
current = Datetime.now();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment