1

I have a weird thing happening with a test class. I can successfully run either of 2 test methods, but if I run them both, the first test method fails giving me an error:

Internal Salesforce Error: 209140802-20783 (1250402052) (1250402052)

The logs say it's failing at the upsert permsets line, which is also in the other test method that succeeds.

Here's the class I'm testing, which assigns/deletes a permission set assignment when a new user is added to the app.

public with sharing class PermSetAssignment {
    @future
    public static void setPsa(List<Id> userIdList) 
    {
        List<PermissionSetAssignment> psaList = new List<PermissionSetAssignment>();
        String ptuId = [SELECT id from PermissionSet WHERE Name = 'Proton_Text_User'].id;
        String smsptId = [SELECT id from PermissionSet WHERE Name = 'SMS_Push_Topic'].id;
        try
        {
            for(Id thisUserId : userIdList)
            {
                PermissionSetAssignment ptu = new PermissionSetAssignment();
                ptu.PermissionSetId = ptuId;
                ptu.AssigneeId = thisUserId;
                psaList.add(ptu);
                PermissionSetAssignment smspt = new PermissionSetAssignment();
                smspt.PermissionSetId = smsptId;
                smspt.AssigneeId = thisUserId;
                psaList.add(smspt);
            }
            try
            {
                insert psaList;
            } catch(DmlException e)
            {
                system.debug('Error: ' + e);
            }
        }catch(Exception e)
        {
            system.debug('Error: ' + e);
        }
    }

    @future
    public static void deletePsa(List<Id> userIdList)
    {  
        List<Id> ptaIdList = new List<Id>();
        ptaIdList.add([SELECT id from PermissionSet WHERE Name = 'Proton_Text_User'].Id);
        ptaIdList.add([SELECT id from PermissionSet WHERE Name = 'SMS_Push_Topic'].Id);
        List<PermissionSetAssignment> psaList= [SELECT id 
                                                FROM PermissionSetAssignment 
                                                WHERE PermissionSetId in :ptaIdList 
                                                AND AssigneeId in :userIdList];
        try 
        {
            delete psaList;
        } catch(DmlException e)
        {
            system.debug('Error: ' + e);
        }
    }

}

And here is the test class:

@isTest

private class PermSetAssignmentTest {
    @isTest 
    private static void test_setPsa() 
    {
        List<PermissionSet> permSets = new List<PermissionSet>();
        PermissionSet ptuPs;
        PermissionSet smsptPs;
        try
        {
             ptuPs = [SELECT id from PermissionSet WHERE Name = 'Proton_Text_User' LIMIT 1];
        }catch(QueryException qe)
        {
             ptuPs = new PermissionSet(Name = 'Proton_Text_User', Label = 'PtuPermSet');
        }

        try{ 
             smsptPs = [SELECT id from PermissionSet WHERE Name = 'SMS_Push_Topic' LIMIT 1];
        }catch(QueryException qe)
        {
             smsptPs = new PermissionSet(Name = 'SMS_Push_Topic', Label = 'SmsPtPermSet');
        }
        permSets.add(ptuPs);
        permSets.add(smsptPs);
        upsert permSets;

        List<User> Users = new List<User>();
        Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
        for (Integer i=0; i<5; i++) 
        {
                User u = new User(Alias = 'standt' + i, Email='teststandarduser' + i + '@testorg.com',
                EmailEncodingKey='UTF-8', LastName='Testing' + i, LanguageLocaleKey='en_US',
                LocaleSidKey='en_US', MobilePhone='+1234567800' + i, ProfileId = p.Id, 
                TimeZoneSidKey='America/Los_Angeles',
                UserName= DateTime.now().getTime() + '@testorg' + i +'.com'); 
                Users.add(u);
        }
        insert Users;
        List<Id> userIdList = new List<Id>();
        for (User u : Users)
        {
            userIdList.add(u.Id);
        }
        test.startTest();
        PermSetAssignment.setPsa(userIdList);
        test.stopTest();
        List<PermissionSetAssignment> ptuPsas= [SELECT Id 
                                                FROM PermissionSetAssignment
                                                WHERE AssigneeId in :userIdList
                                                AND PermissionSetId = :ptuPs.Id];
        System.assertEquals(5, ptuPsas.size());
        List<PermissionSetAssignment> smsptPsas=[SELECT Id 
                                                FROM PermissionSetAssignment
                                                WHERE AssigneeId in :userIdList
                                                AND PermissionSetId = :smsptPs.Id];
        System.assertEquals(5, smsptPsas.size());
    }

    @isTest
    private static void test_deletePsa()
    {
        List<PermissionSet> permSets = new List<PermissionSet>();
        PermissionSet ptuPs;
        PermissionSet smsptPs;
        try
        {
             ptuPs = [SELECT id from PermissionSet WHERE Name = 'Proton_Text_User' LIMIT 1];
        }catch(QueryException qe)
        {
             ptuPs = new PermissionSet(Name = 'Proton_Text_User', Label = 'PtuPermSet');
        }

        try{ 
             smsptPs = [SELECT id from PermissionSet WHERE Name = 'SMS_Push_Topic' LIMIT 1];
        }catch(QueryException qe)
        {
             smsptPs = new PermissionSet(Name = 'SMS_Push_Topic', Label = 'SmsPtPermSet');
        }
        permSets.add(ptuPs);
        permSets.add(smsptPs);
        upsert permSets;

        List<User> Users = new List<User>();
        Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
        for (Integer i=0; i<5; i++) 
        {
                User u = new User(Alias = 'standt' + i, Email='teststandarduser' + i + '@testorg.com',
                EmailEncodingKey='UTF-8', LastName='Testing' + i, LanguageLocaleKey='en_US',
                LocaleSidKey='en_US', MobilePhone='+1234567800' + i, ProfileId = p.Id, 
                TimeZoneSidKey='America/Los_Angeles',
                UserName= DateTime.now().getTime() + '@testorg' + i +'.com'); 
                Users.add(u);
        }
        insert Users;
        List<Id> userIdList = new List<Id>();
        for (User u : Users)
        {
            userIdList.add(u.Id);
        }
        List<PermissionSetAssignment> psaList = new List<PermissionSetAssignment>();
        for(Id thisUserId : userIdList)
        {
            PermissionSetAssignment ptu = new PermissionSetAssignment();
            ptu.PermissionSetId = ptuPs.Id;
            ptu.AssigneeId = thisUserId;
            psaList.add(ptu);
            PermissionSetAssignment smspt = new PermissionSetAssignment();
            smspt.PermissionSetId = smsptPs.Id;
            smspt.AssigneeId = thisUserId;
            psaList.add(smspt);
        }
        insert psaList;
        List<PermissionSetAssignment> ptuPsas= [SELECT Id 
                                                FROM PermissionSetAssignment
                                                WHERE AssigneeId in :userIdList
                                                AND PermissionSetId = :ptuPs.Id];
        System.assertEquals(5, ptuPsas.size());
        List<PermissionSetAssignment> smsptPsas=[SELECT Id 
                                                FROM PermissionSetAssignment
                                                WHERE AssigneeId in :userIdList
                                                AND PermissionSetId = :smsptPs.Id];
        System.assertEquals(5, smsptPsas.size());
        test.startTest();
        PermSetAssignment.deletePsa(userIdList);
        test.stopTest();
        ptuPsas= [  SELECT Id 
                    FROM PermissionSetAssignment
                    WHERE AssigneeId in :userIdList
                    AND PermissionSetId = :ptuPs.Id];
        System.assertEquals(0, ptuPsas.size());
        smsptPsas=[ SELECT Id 
                    FROM PermissionSetAssignment
                    WHERE AssigneeId in :userIdList
                    AND PermissionSetId = :smsptPs.Id];
        System.assertEquals(0, smsptPsas.size());
    }
}

Any idea what's going on?


Update: I separated the two test methods into separate test classes and they are both successful when I do a Run All. Odd workaround.


Update2: I tried @Adrian Larson's suggestion to use the @isTest(isParallel=false) annotation. I got the same error.

  • Try setting @IsParallel to false. – Adrian Larson Apr 28 '18 at 18:51
  • @AdrianLarson I'll try that and update this post. – PatMcClellan__c Apr 28 '18 at 18:58
1

Here's my guess: Each test is inserting the same permission set with the same unique name. There's a potential race condition when running both methods that they may go to insert the same permission set with the same name. I know you're not in SeeAllData=true, but some duplicate checks may still apply even though you can't query the row. And that may be why your getting an internal server error, rather than a DmlException about DUPLICATE_MASTER_LABEL. (If you want to know for sure, you can open a Salesforce case. Internal server errors, while I get them from time to time too, aren't really supposed to happen anyways.)

Another fix would be to use a testSetup method that did the insert of the permission sets. That way they'd only be happening once in the test class and there'd be no race condition between the two test methods. https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_testsetup_using.htm

Also, on a different note, you may want to consider if you really want to ignore the exceptions in your target code like you are.

  • Thanks for your input @William Young. I'm just going to keep them in separate classes and move on. Regarding your note on ignoring the exceptions... I'm actually using try/catch structure as shorthand for checking for returning 0 rows on the SOQL search -- replacement for more lines in an if/else statement. I saw it in someone else's code on here and thought it was nice. – PatMcClellan__c May 1 '18 at 3:16

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.