Skip to content

Commit

Permalink
createTasks should throw all RuntimeException happen in thread. (Azur…
Browse files Browse the repository at this point in the history
  • Loading branch information
xingwu1 authored May 31, 2018
1 parent b773f57 commit 8fb0fac
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Azure Batch SDK for Java release notes

## Changes in 3.3.0
### Features
- `createTasks` rethrow `RuntimeException` catched by internal threads.

## Changes in 3.2.0
### Features
- Update comments for some classes and properties.
Expand Down
16 changes: 7 additions & 9 deletions src/main/java/com/microsoft/azure/batch/TaskOperations.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,10 @@ public void createTask(String jobId, TaskAddParameter taskToAdd, Iterable<BatchC
*
* @param jobId The ID of the job to which to add the task.
* @param taskList A list of {@link TaskAddParameter tasks} to add.
* @throws BatchErrorException Exception thrown when an error response is received from the Batch service.
* @throws IOException Exception thrown when there is an error in serialization/deserialization of data sent to/received from the Batch service.
* @throws RuntimeException Exception thrown when an error response is received from the Batch service or any network exception.
* @throws InterruptedException Exception thrown if any thread has interrupted the current thread.
*/
public void createTasks(String jobId, List<TaskAddParameter> taskList) throws BatchErrorException, IOException, InterruptedException {
public void createTasks(String jobId, List<TaskAddParameter> taskList) throws RuntimeException, InterruptedException {
createTasks(jobId, taskList, null);
}

Expand Down Expand Up @@ -163,7 +162,7 @@ public void run() {
}
}
}
} catch (BatchErrorException e) {
} catch (RuntimeException e) {
// Any exception will stop further call
exception = e;
pendingList.addAll(taskList);
Expand All @@ -183,11 +182,10 @@ public void run() {
* @param jobId The ID of the job to which to add the task.
* @param taskList A list of {@link TaskAddParameter tasks} to add.
* @param additionalBehaviors A collection of {@link BatchClientBehavior} instances that are applied to the Batch service request.
* @throws BatchErrorException Exception thrown when an error response is received from the Batch service.
* @throws IOException Exception thrown when there is an error in serialization/deserialization of data sent to/received from the Batch service.
* @throws RuntimeException Exception thrown when an error response is received from the Batch service or any network exception.
* @throws InterruptedException Exception thrown if any thread has interrupted the current thread.
*/
public void createTasks(String jobId, List<TaskAddParameter> taskList, Iterable<BatchClientBehavior> additionalBehaviors) throws BatchErrorException, IOException, InterruptedException {
public void createTasks(String jobId, List<TaskAddParameter> taskList, Iterable<BatchClientBehavior> additionalBehaviors) throws RuntimeException, InterruptedException {

BehaviorManager bhMgr = new BehaviorManager(this.customBehaviors(), additionalBehaviors);

Expand Down Expand Up @@ -265,9 +263,9 @@ public void createTasks(String jobId, List<TaskAddParameter> taskList, Iterable<
if (innerException instanceof BatchErrorException) {
throw (BatchErrorException) innerException;
} else {
// WorkingThread will only catch and store a BatchErrorException or an IOException in its run() method.
// WorkingThread will only catch and store a BatchErrorException or a RuntimeException in its run() method.
// WorkingThread.getException() should therefore only return one of these two types, making the cast safe.
throw (IOException) innerException;
throw (RuntimeException) innerException;
}
}

Expand Down
34 changes: 34 additions & 0 deletions src/test/java/com/microsoft/azure/batch/TaskTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

package com.microsoft.azure.batch;

import com.microsoft.azure.batch.auth.BatchSharedKeyCredentials;
import com.microsoft.azure.batch.interceptor.BatchClientParallelOptions;
import com.microsoft.azure.batch.protocol.models.*;
import com.microsoft.azure.storage.blob.CloudBlobContainer;
Expand Down Expand Up @@ -301,6 +302,39 @@ public void testAddMultiTasks() throws Exception {
}
}


@Test
public void testAddMultiTasksWithError() throws Exception {

BatchSharedKeyCredentials noExistCredentials1 = new BatchSharedKeyCredentials(
"https://noexistaccount.westus.batch.azure.com",
"noexistaccount",
System.getenv("AZURE_BATCH_ACCESS_KEY"));
BatchClient testBatchClient = BatchClient.open(noExistCredentials1);

String jobId = getStringWithUserNamePrefix("-Job1-" + (new Date()).toString().replace(' ', '-').replace(':', '-').replace('.', '-'));

int TASK_COUNT=1000;

try {
// CREATE
List<TaskAddParameter> tasksToAdd = new ArrayList<>();
for (int i=0; i<TASK_COUNT; i++)
{
TaskAddParameter addParameter = new TaskAddParameter();
addParameter.withId(String.format("mytask%d", i)).withCommandLine(String.format("cmd /c echo hello %d",i));
tasksToAdd.add(addParameter);
}
BatchClientParallelOptions option = new BatchClientParallelOptions(10);
Collection<BatchClientBehavior> behaviors = new HashSet<>();
behaviors.add(option);
testBatchClient.taskOperations().createTasks(jobId, tasksToAdd, behaviors);
Assert.assertTrue("Should not here", true);
} catch (RuntimeException ex) {
System.out.printf("Expect exception %s", ex.toString());
}
}

@Test
public void testGetTaskCounts() throws Exception {
String jobId = getStringWithUserNamePrefix("-Job1-" + (new Date()).toString().replace(' ', '-').replace(':', '-').replace('.', '-'));
Expand Down

0 comments on commit 8fb0fac

Please sign in to comment.