Skip to content

Commit c95e1ec

Browse files
mderkamziccard
authored andcommitted
Third concept of DNS batch. (googleapis#787)
* Added third concept of DNS batch. * Simplified the internals. Removed DnsBatch.Request. * Work in progress. Implemented more calls and added tests. * Turned BatchResult into an abstract class. * Created RpcBatch interface for opacity of batch. Added core unit test for BatchResult. Added javadoc for BatchResult generics. Changed prefix of newCallback methods. Fixed method javadoc. * Fixed documentation. * Removed addToBatch methods from RPC. * Removed conflicts with master branch. * Implemented batch processing for change requests and record sets. * Implemented the rest of the batch functions and tests. - Renames several forgotten DnsRecord variables and functions in tests - Moved the Callback interface from DnsRpc to RpcBatch * Added tests for callbacks. Fixed documentation. * Added onFailure callback tests and fixed one doc string. * Extracted GoogleJsonError to a final attribute. * Fixed imports and implemented notify. * Fixed import orders * Annotated getters as @VisibleForTesting. * Fixed docs and renamed a few methods. Also consolidated mocks batch in tests. * Added notify test * Renamed submitted() to completed(). * Consolidated more tests and added exception to notify. * Added test for null result in BatchResult.
1 parent 5e0de55 commit c95e1ec

File tree

17 files changed

+1772
-72
lines changed

17 files changed

+1772
-72
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright 2016 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud;
18+
19+
import static com.google.common.base.Preconditions.checkState;
20+
21+
import java.util.LinkedList;
22+
import java.util.List;
23+
24+
/**
25+
* This class holds a single result of a batch call. {@code T} is the type of the result and {@code
26+
* E} is the type of the service-dependent exception thrown when a processing error occurs.
27+
*/
28+
public abstract class BatchResult<T, E extends BaseServiceException> {
29+
30+
private T result;
31+
private boolean completed = false;
32+
private E error;
33+
private List<Callback<T, E>> toBeNotified = new LinkedList<>();
34+
35+
/**
36+
* Returns {@code true} if the batch has been completed and the result is available; {@code false}
37+
* otherwise.
38+
*/
39+
public boolean completed() {
40+
return completed;
41+
}
42+
43+
/**
44+
* Returns the result of this call.
45+
*
46+
* @throws IllegalStateException if the batch has not been completed yet
47+
* @throws E if an error occurred when processing this request
48+
*/
49+
public T get() throws E {
50+
checkState(completed(), "Batch has not been completed yet");
51+
if (error != null) {
52+
throw error;
53+
}
54+
return result;
55+
}
56+
57+
/**
58+
* Adds a callback for the batch operation.
59+
*
60+
* @throws IllegalStateException if the batch has been completed already
61+
*/
62+
public void notify(Callback<T, E> callback) {
63+
if (completed) {
64+
throw new IllegalStateException("The batch has been completed. All the calls to the notify()"
65+
+ " method should be done prior to submitting the batch.");
66+
}
67+
toBeNotified.add(callback);
68+
}
69+
70+
/**
71+
* Sets an error and status as completed. Notifies all callbacks.
72+
*/
73+
protected void error(E error) {
74+
this.error = error;
75+
this.completed = true;
76+
for (Callback<T, E> callback : toBeNotified) {
77+
callback.error(error);
78+
}
79+
}
80+
81+
/**
82+
* Sets a result and status as completed. Notifies all callbacks.
83+
*/
84+
protected void success(T result) {
85+
this.result = result;
86+
this.completed = true;
87+
for (Callback<T, E> callback : toBeNotified) {
88+
callback.success(result);
89+
}
90+
}
91+
92+
/**
93+
* An interface for the batch callbacks.
94+
*/
95+
public interface Callback<T, E> {
96+
/**
97+
* The method to be called when the batched operation succeeds.
98+
*/
99+
void success(T result);
100+
101+
/**
102+
* The method to be called when the batched operation fails.
103+
*/
104+
void error(E exception);
105+
}
106+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* Copyright 2016 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud;
18+
19+
import static org.junit.Assert.assertFalse;
20+
import static org.junit.Assert.assertSame;
21+
import static org.junit.Assert.assertTrue;
22+
import static org.junit.Assert.fail;
23+
24+
import org.easymock.EasyMock;
25+
import org.junit.Before;
26+
import org.junit.Test;
27+
28+
public class BatchResultTest {
29+
30+
private BatchResult<Boolean, BaseServiceException> result;
31+
32+
@Before
33+
public void setUp() {
34+
result = new BatchResult<Boolean, BaseServiceException>() {};
35+
}
36+
37+
@Test
38+
public void testSuccess() {
39+
assertFalse(result.completed());
40+
try {
41+
result.get();
42+
fail("This was not completed yet.");
43+
} catch (IllegalStateException ex) {
44+
// expected
45+
}
46+
result.success(true);
47+
assertTrue(result.get());
48+
// test that null is allowed
49+
result.success(null);
50+
}
51+
52+
@Test
53+
public void testError() {
54+
assertFalse(result.completed());
55+
try {
56+
result.get();
57+
fail("This was not completed yet.");
58+
} catch (IllegalStateException ex) {
59+
// expected
60+
}
61+
BaseServiceException ex = new BaseServiceException(0, "message", "reason", false);
62+
result.error(ex);
63+
try {
64+
result.get();
65+
fail("This is a failed operation and should have thrown a DnsException.");
66+
} catch (BaseServiceException real) {
67+
assertSame(ex, real);
68+
}
69+
}
70+
71+
@Test
72+
public void testNotifyError() {
73+
final BaseServiceException ex = new BaseServiceException(0, "message", "reason", false);
74+
assertFalse(result.completed());
75+
BatchResult.Callback<Boolean, BaseServiceException> callback =
76+
EasyMock.createStrictMock(BatchResult.Callback.class);
77+
callback.error(ex);
78+
EasyMock.replay(callback);
79+
result.notify(callback);
80+
result.error(ex);
81+
try {
82+
result.notify(callback);
83+
fail("The batch has been completed.");
84+
} catch (IllegalStateException exception) {
85+
// expected
86+
}
87+
EasyMock.verify(callback);
88+
}
89+
90+
@Test
91+
public void testNotifySuccess() {
92+
assertFalse(result.completed());
93+
BatchResult.Callback<Boolean, BaseServiceException> callback =
94+
EasyMock.createStrictMock(BatchResult.Callback.class);
95+
callback.success(true);
96+
EasyMock.replay(callback);
97+
result.notify(callback);
98+
result.success(true);
99+
try {
100+
result.notify(callback);
101+
fail("The batch has been completed.");
102+
} catch (IllegalStateException exception) {
103+
// expected
104+
}
105+
EasyMock.verify(callback);
106+
}
107+
}

gcloud-java-dns/src/main/java/com/google/cloud/dns/ChangeRequest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@ public ChangeRequest reload(Dns.ChangeRequestOption... options) {
173173

174174
/**
175175
* Returns {@code true} if the change request has been completed. If the status is not {@link
176-
* Status#DONE} already, the method makes an API call to Google Cloud DNS to update the change
177-
* request first.
176+
* ChangeRequestInfo.Status#DONE} already, the method makes an API call to Google Cloud DNS to
177+
* update the change request first.
178178
*
179179
* @throws DnsException upon failure of the API call or if the associated zone was not found
180180
*/

gcloud-java-dns/src/main/java/com/google/cloud/dns/Dns.java

+5
Original file line numberDiff line numberDiff line change
@@ -503,4 +503,9 @@ ChangeRequest getChangeRequest(String zoneName, String changeRequestId,
503503
* @see <a href="https://cloud.google.com/dns/api/v1/changes/list">Cloud DNS Chages: list</a>
504504
*/
505505
Page<ChangeRequest> listChangeRequests(String zoneName, ChangeRequestListOption... options);
506+
507+
/**
508+
* Creates a new empty batch for grouping multiple service calls in one underlying RPC call.
509+
*/
510+
DnsBatch batch();
506511
}

0 commit comments

Comments
 (0)