Skip to content

Commit beea588

Browse files
mderkaajkannan
authored andcommitted
Third concept of DNS batch. (#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 10afe86 commit beea588

20 files changed

Lines changed: 1778 additions & 90 deletions

File tree

Lines changed: 107 additions & 0 deletions
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 com.google.common.base.Preconditions.checkNotNull;
20+
import static com.google.common.base.Preconditions.checkState;
21+
22+
import java.util.LinkedList;
23+
import java.util.List;
24+
25+
/**
26+
* This class holds a single result of a batch call. {@code T} is the type of the result and {@code
27+
* E} is the type of the service-dependent exception thrown when a processing error occurs.
28+
*/
29+
public abstract class BatchResult<T, E extends BaseServiceException> {
30+
31+
private T result;
32+
private boolean completed = false;
33+
private E error;
34+
private List<Callback<T, E>> toBeNotified = new LinkedList<>();
35+
36+
/**
37+
* Returns {@code true} if the batch has been completed and the result is available; {@code false}
38+
* otherwise.
39+
*/
40+
public boolean completed() {
41+
return completed;
42+
}
43+
44+
/**
45+
* Returns the result of this call.
46+
*
47+
* @throws IllegalStateException if the batch has not been completed yet
48+
* @throws E if an error occurred when processing this request
49+
*/
50+
public T get() throws E {
51+
checkState(completed(), "Batch has not been completed yet");
52+
if (error != null) {
53+
throw error;
54+
}
55+
return result;
56+
}
57+
58+
/**
59+
* Adds a callback for the batch operation.
60+
*
61+
* @throws IllegalStateException if the batch has been completed already
62+
*/
63+
public void notify(Callback<T, E> callback) {
64+
if (completed) {
65+
throw new IllegalStateException("The batch has been completed. All the calls to the notify()"
66+
+ " method should be done prior to submitting the batch.");
67+
}
68+
toBeNotified.add(callback);
69+
}
70+
71+
/**
72+
* Sets an error and status as completed. Notifies all callbacks.
73+
*/
74+
protected void error(E error) {
75+
this.error = error;
76+
this.completed = true;
77+
for (Callback<T, E> callback : toBeNotified) {
78+
callback.error(error);
79+
}
80+
}
81+
82+
/**
83+
* Sets a result and status as completed. Notifies all callbacks.
84+
*/
85+
protected void success(T result) {
86+
this.result = result;
87+
this.completed = true;
88+
for (Callback<T, E> callback : toBeNotified) {
89+
callback.success(result);
90+
}
91+
}
92+
93+
/**
94+
* An interface for the batch callbacks.
95+
*/
96+
public interface Callback<T, E> {
97+
/**
98+
* The method to be called when the batched operation succeeds.
99+
*/
100+
void success(T result);
101+
102+
/**
103+
* The method to be called when the batched operation fails.
104+
*/
105+
void error(E exception);
106+
}
107+
}
Lines changed: 107 additions & 0 deletions
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/Dns.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616

1717
package com.google.cloud.dns;
1818

19-
import com.google.common.collect.ImmutableList;
2019
import com.google.cloud.FieldSelector;
2120
import com.google.cloud.FieldSelector.Helper;
2221
import com.google.cloud.Page;
2322
import com.google.cloud.Service;
2423
import com.google.cloud.dns.spi.DnsRpc;
24+
import com.google.common.collect.ImmutableList;
2525

2626
import java.util.List;
2727

@@ -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)