From 8fb0fac1531954b3ce81f95d42be944b4f9d1e3a Mon Sep 17 00:00:00 2001 From: Xing Wu Date: Thu, 31 May 2018 12:55:02 -0700 Subject: [PATCH] createTasks should throw all RuntimeException happen in thread. (#25) --- changelog.md | 4 +++ .../microsoft/azure/batch/TaskOperations.java | 16 ++++----- .../com/microsoft/azure/batch/TaskTests.java | 34 +++++++++++++++++++ 3 files changed, 45 insertions(+), 9 deletions(-) diff --git a/changelog.md b/changelog.md index 2276b569d28c0..e31d848361736 100644 --- a/changelog.md +++ b/changelog.md @@ -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. diff --git a/src/main/java/com/microsoft/azure/batch/TaskOperations.java b/src/main/java/com/microsoft/azure/batch/TaskOperations.java index 596f81786fa20..93aea9fd32b99 100644 --- a/src/main/java/com/microsoft/azure/batch/TaskOperations.java +++ b/src/main/java/com/microsoft/azure/batch/TaskOperations.java @@ -85,11 +85,10 @@ public void createTask(String jobId, TaskAddParameter taskToAdd, Iterable taskList) throws BatchErrorException, IOException, InterruptedException { + public void createTasks(String jobId, List taskList) throws RuntimeException, InterruptedException { createTasks(jobId, taskList, null); } @@ -163,7 +162,7 @@ public void run() { } } } - } catch (BatchErrorException e) { + } catch (RuntimeException e) { // Any exception will stop further call exception = e; pendingList.addAll(taskList); @@ -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 taskList, Iterable additionalBehaviors) throws BatchErrorException, IOException, InterruptedException { + public void createTasks(String jobId, List taskList, Iterable additionalBehaviors) throws RuntimeException, InterruptedException { BehaviorManager bhMgr = new BehaviorManager(this.customBehaviors(), additionalBehaviors); @@ -265,9 +263,9 @@ public void createTasks(String jobId, List 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; } } diff --git a/src/test/java/com/microsoft/azure/batch/TaskTests.java b/src/test/java/com/microsoft/azure/batch/TaskTests.java index a2a0682b67d85..ed78a06d8640d 100644 --- a/src/test/java/com/microsoft/azure/batch/TaskTests.java +++ b/src/test/java/com/microsoft/azure/batch/TaskTests.java @@ -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; @@ -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 tasksToAdd = new ArrayList<>(); + for (int i=0; i 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('.', '-'));