Created
September 9, 2013 19:05
-
-
Save mickle00/6500029 to your computer and use it in GitHub Desktop.
Reproducible code that shows CPU Limits not getting reset as part of Test.startTest()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@isTest(seeAlLData=False) | |
private class CPU_Test{ | |
/** | |
There are two additional system static methods provided by Apex. | |
These methods, Test.startTest and Test.stopTest, are used when testing governor limits. | |
Or in other words, executing test scenarios with a larger data set. | |
The Test.startTest method marks the point in your test code when your test actually begins. | |
Each test method is allowed to call this method only once. | |
All of the code before this method should be used to initialize variables, populate data structures, | |
and so on, allowing you to set up everything you need in order to run your test. | |
After you call this method, you get a fresh set of governor limits for the | |
remainder of the test until you call Test.stopTest. | |
**/ | |
/** | |
* Should get new CPU Limits after setting up test data. This is currently failing. | |
*/ | |
static testMethod void shouldGetNewCPULimits () { | |
Integer nearLimitCpuTime = 0; | |
while (Limits.getCpuTime() < Limits.getLimitCpuTime() - 10){ | |
nearLimitCpuTime = Limits.getCpuTime(); | |
} | |
Test.startTest(); | |
system.assertNotEquals(nearLimitCpuTime, Limits.getCpuTime()); // Doesn't always fail. Give or take a coupple of milliseconds | |
system.assert(nearLimitCpuTime > Limits.getCpuTime(), 'Near CPU Limit should always be greater than fresh transaction with new limits'); // Near CPU Limit should always be greater than fresh transaction with new limits | |
system.assert(Limits.getCpuTime() < 1000, string.valueOf(Limits.getCpuTime()) + ' CPU Time Used'); //Should not use 1000ms of cpu time with new transaction. | |
Test.stopTest(); | |
} | |
/** | |
* SOQL Limit should Reset within Test Method. This is passing | |
*/ | |
static testMethod void shouldGetNewSOQLLimit () { | |
List<Account> accts = [SELECT Id FROM Account LIMIT 1]; | |
system.assertEquals(1, Limits.getQueries()); | |
Test.startTest(); | |
system.assertEquals(0, Limits.getQueries()); | |
Test.stopTest(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment