2

I noticed a strange behaviour when deploying PlatformCachePartition metadata which also includes test methods in the same package. Below is an example of the PlatformCachePartition metadata:

<?xml version="1.0" encoding="UTF-8"?>
<PlatformCachePartition xmlns="http://soap.sforce.com/2006/04/metadata">
    <isDefaultPartition>true</isDefaultPartition>
    <masterLabel>PlatformCache</masterLabel>
    <platformCachePartitionTypes>
        <allocatedCapacity>10</allocatedCapacity>
        <allocatedPurchasedCapacity>0</allocatedPurchasedCapacity>
        <allocatedTrialCapacity>0</allocatedTrialCapacity>
        <cacheType>Organization</cacheType>
    </platformCachePartitionTypes>
    <platformCachePartitionTypes>
        <allocatedCapacity>10</allocatedCapacity>
        <allocatedPurchasedCapacity>0</allocatedPurchasedCapacity>
        <allocatedTrialCapacity>0</allocatedTrialCapacity>
        <cacheType>Session</cacheType>
    </platformCachePartitionTypes>
</PlatformCachePartition>

Tests are pretty simple, we've wrapped all the cache business in another class, but in a nutshell the tests are storing and retrieving a value for a specific key, then asserting:

@isTest
public class CacheManager_Test
{
    public static testMethod void testSessionCache() 
    {
        String cacheKey1 = CacheManager.normaliseCacheKey(CacheManager.CacheKey.MY_ENUM);
        CacheManager.putSessionValue(cacheKey1, 'mysamplevalue', 300);
        System.assertEquals('mysamplevalue', CacheManager.getSessionValue(cacheKey1));
    }

    public static testMethod void testOrgCache() 
    {
        String cacheKey1 = CacheManager.normaliseCacheKey(CacheManager.CacheKey.MY_ENUM);
        CacheManager.putOrgValue(cacheKey1, '123', 300);
        System.assertEquals('123', CacheManager.getOrgValue(cacheKey1));
    }
}

What's interesting is that when I run these test methods in isolation, they both pass. Even if I deploy the test class into another org it runs, no issues. However, if I also add the PlatformCachePartition metadata in the same package and try to deploy (with running tests), both asserts fail. What's going on here?

2

After careful investigation, we discovered a couple of things:

  • First, this issue seems to be resolved in API v41.0 (we were using v40.0 for the deployment for some reason).
  • What happens in API's < v41.0 is that upon deploying PlatformCachePartition, the allocatedCapacity gets reset to 0, therefore you can't access the cache. If you must use an older version, you should first deploy and configure the cache allocations in the destination org prior deploying any tests related to the cache, and exclude the PlatformCachePartition from the second deployment.
| improve this answer | |

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.