/** * Starts asynchronous cancellation on a long-running operation. The server makes a best effort to * cancel the operation, but success is not guaranteed. If the server doesn't support this method, * it returns `google.rpc.Code.UNIMPLEMENTED`. Clients can use * [Operations.GetOperation][google.longrunning.Operations.GetOperation] or other methods to check * whether the cancellation succeeded or whether the operation completed despite cancellation. On * successful cancellation, the operation is not deleted; instead, it becomes an operation with an * [Operation.error][google.longrunning.Operation.error] value with a * [google.rpc.Status.code][google.rpc.Status.code] of 1, corresponding to `Code.CANCELLED`. * * <p>Sample code: * * <pre><code> * try (OperationsClient operationsClient = OperationsClient.create()) { * String name = ""; * operationsClient.cancelOperation(name); * } * </code></pre> * * @param name The name of the operation resource to be cancelled. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ public final void cancelOperation(String name) { CancelOperationRequest request = CancelOperationRequest.newBuilder().setName(name).build(); cancelOperation(request); }
/** * Starts asynchronous cancellation on a long-running operation. The server makes a best effort to * cancel the operation, but success is not guaranteed. If the server doesn't support this method, * it returns `google.rpc.Code.UNIMPLEMENTED`. Clients can use * [Operations.GetOperation][google.longrunning.Operations.GetOperation] or other methods to check * whether the cancellation succeeded or whether the operation completed despite cancellation. On * successful cancellation, the operation is not deleted; instead, it becomes an operation with an * [Operation.error][google.longrunning.Operation.error] value with a * [google.rpc.Status.code][google.rpc.Status.code] of 1, corresponding to `Code.CANCELLED`. * * <p>Sample code: * * <pre><code> * try (OperationsClient operationsClient = OperationsClient.create()) { * String name = ""; * operationsClient.cancelOperation(name); * } * </code></pre> * * @param name The name of the operation resource to be cancelled. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ public final void cancelOperation(String name) { CancelOperationRequest request = CancelOperationRequest.newBuilder().setName(name).build(); cancelOperation(request); }
@Test @SuppressWarnings("all") public void cancelOperationExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockOperations.addException(exception); try { String name = "name3373707"; client.cancelOperation(name); Assert.fail("No exception raised"); } catch (InvalidArgumentException e) { // Expected exception } }
@Test @SuppressWarnings("all") public void cancelOperationTest() { Empty expectedResponse = Empty.newBuilder().build(); mockOperations.addResponse(expectedResponse); String name = "name3373707"; client.cancelOperation(name); List<GeneratedMessageV3> actualRequests = mockOperations.getRequests(); Assert.assertEquals(1, actualRequests.size()); CancelOperationRequest actualRequest = (CancelOperationRequest) actualRequests.get(0); Assert.assertEquals(name, actualRequest.getName()); }