Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions force-app/main/default/classes/ChainableDataBatch.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
public abstract class ChainableDataBatch extends Chainable
implements Database.Batchable<SObject>, Database.Stateful, Database.AllowsCallouts {
// ABSTRACT

protected abstract Database.QueryLocator start(Context ctx);
protected abstract void execute(Context ctx, List<SObject> scope);
protected abstract void finish(Context ctx);


// PUBLIC

public Database.QueryLocator start(Database.BatchableContext ctx) {
return start( new Context(ctx) );
}


public void execute(Database.BatchableContext ctx, List<SObject> scope) {
execute( new Context(ctx), scope );
}


public void finish(Database.BatchableContext ctx) {
finish( new Context(ctx) );

executeNext();
}


protected virtual Integer batchSize() {
return 200;
}


public override void executeAsynchronously() {
Database.executeBatch(this, batchSize());
}


public override void executeSynchronously(Context ctx) {
Database.QueryLocator fullScope = start(ctx);
Database.QueryLocatorIterator iterator = fullScope.iterator();
List<SObject> batchedList = new List<SObject>();
Integer batchIndex = 0;

while(iterator.hasNext()) {
batchedList.add(iterator.next());
if(batchedList.size() == batchSize()) {
batchIndex++;
execute(ctx, batchedList);
batchedList.clear();
}
if (Test.isRunningTest() && batchIndex == 1) {
break;
}
}
if(!batchedList.isEmpty()) {
execute(ctx, batchedList);
}

finish(ctx);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>64.0</apiVersion>
<status>Active</status>
</ApexClass>
4 changes: 4 additions & 0 deletions force-app/main/test/classes/Chainable_Test.cls
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class Chainable_Test {

.then( new SampleBatch() )
.then( new SampleQueueable() )
.then( new SampleDataBatch() )

.execute();

Expand All @@ -28,6 +29,9 @@ public class Chainable_Test {
System.assertEquals('SampleBatch.execute', (String)calls.next());
System.assertEquals('SampleBatch.finish', (String)calls.next());
System.assertEquals('SampleQueueable.execute', (String)calls.next());
System.assertEquals('SampleDataBatch.start', (String)calls.next());
System.assertEquals('SampleDataBatch.execute', (String)calls.next());
System.assertEquals('SampleDataBatch.finish', (String)calls.next());
}


Expand Down
22 changes: 22 additions & 0 deletions force-app/main/test/classes/Error_Test.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
@IsTest
public with sharing class Error_Test {

private static final String CALL_LOG = 'calls';
private static List<Object> deferredCalls;

@TestSetup
private static void prepareData() {
insert new Account(Name = 'Acme'); // Note: SampleBatch iterates over Accounts
}

@IsTest
private static void showErrorDuringTest() {

// Execute
Chainable chain = new SampleInternalErrorDuringTest()
.setShared(CALL_LOG, new List<String>())
.execute();

}

}
5 changes: 5 additions & 0 deletions force-app/main/test/classes/Error_Test.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>64.0</apiVersion>
<status>Active</status>
</ApexClass>
18 changes: 18 additions & 0 deletions force-app/main/test/classes/SampleDataBatch.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
public class SampleDataBatch extends ChainableDataBatch {

protected override Database.QueryLocator start(Context ctx) {
Chainable_Test.log(this);

return Database.getQueryLocator([SELECT Phone FROM Account]);
}


protected override void execute(Context ctx, List<SObject> scope) {
Chainable_Test.log(this);
}


protected override void finish(Context ctx) {
Chainable_Test.log(this);
}
}
5 changes: 5 additions & 0 deletions force-app/main/test/classes/SampleDataBatch.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>64.0</apiVersion>
<status>Active</status>
</ApexClass>
18 changes: 18 additions & 0 deletions force-app/main/test/classes/SampleInternalErrorDuringTest.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
public class SampleInternalErrorDuringTest extends ChainableBatch {

protected override Iterable<Object> start(Context ctx) {
Chainable_Test.log(this);

return Database.getQueryLocator([SELECT Phone FROM Account]);
}


protected override void execute(Context ctx, Iterable<Object> scope) {
Chainable_Test.log(this);
}


protected override void finish(Context ctx) {
Chainable_Test.log(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>64.0</apiVersion>
<status>Active</status>
</ApexClass>