-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for batches to DNS #940
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c95e1ec
Third concept of DNS batch. (#787)
mderka e86a7e8
Disallowed null error in BatchResult. Adjusted doc. (#921)
mderka 618b677
Integration test for batch. Fixes #874. (#919)
mderka b75652e
Added batch support to the local DNS helper. (#925)
mderka 357c779
Fix LocalDnsHelper Batch handling (#938)
aozarov 2995240
Fix minor style errros
mziccard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
108 changes: 108 additions & 0 deletions
108
gcloud-java-core/src/main/java/com/google/cloud/BatchResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* Copyright 2016 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.cloud; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
import static com.google.common.base.Preconditions.checkState; | ||
|
||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
/** | ||
* This class holds a single result of a batch call. The class is not thread-safe. | ||
* | ||
* @param <T> the type of the result | ||
* @param <E> the type of the service-dependent exception thrown when a processing error occurs | ||
* | ||
*/ | ||
public abstract class BatchResult<T, E extends BaseServiceException> { | ||
|
||
private T result; | ||
private boolean completed = false; | ||
private E error; | ||
private final List<Callback<T, E>> toBeNotified = new LinkedList<>(); | ||
|
||
/** | ||
* Returns {@code true} if the batch has been completed and the result is available; {@code false} | ||
* otherwise. | ||
*/ | ||
public boolean completed() { | ||
return completed; | ||
} | ||
|
||
/** | ||
* Returns the result of this call. | ||
* | ||
* @throws IllegalStateException if the batch has not been completed yet | ||
* @throws E if an error occurred when processing the batch request | ||
*/ | ||
public T get() throws E { | ||
checkState(completed(), "Batch has not been completed yet"); | ||
if (error != null) { | ||
throw error; | ||
} | ||
return result; | ||
} | ||
|
||
/** | ||
* Adds a callback for the batch operation. | ||
* | ||
* @throws IllegalStateException if the batch has been completed already | ||
*/ | ||
public void notify(Callback<T, E> callback) { | ||
checkState(!completed, "The batch has been completed. All the calls to the notify()" | ||
+ " method should be done prior to submitting the batch."); | ||
toBeNotified.add(callback); | ||
} | ||
|
||
/** | ||
* Sets an error and status as completed. Notifies all callbacks. | ||
*/ | ||
protected void error(E error) { | ||
this.error = checkNotNull(error); | ||
this.completed = true; | ||
for (Callback<T, E> callback : toBeNotified) { | ||
callback.error(error); | ||
} | ||
} | ||
|
||
/** | ||
* Sets a result and status as completed. Notifies all callbacks. | ||
*/ | ||
protected void success(T result) { | ||
this.result = result; | ||
this.completed = true; | ||
for (Callback<T, E> callback : toBeNotified) { | ||
callback.success(result); | ||
} | ||
} | ||
|
||
/** | ||
* An interface for the batch callbacks. | ||
*/ | ||
public interface Callback<T, E> { | ||
/** | ||
* The method to be called when the batched operation succeeds. | ||
*/ | ||
void success(T result); | ||
|
||
/** | ||
* The method to be called when the batched operation fails. | ||
*/ | ||
void error(E exception); | ||
} | ||
} |
113 changes: 113 additions & 0 deletions
113
gcloud-java-core/src/test/java/com/google/cloud/BatchResultTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* Copyright 2016 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.cloud; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertSame; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.junit.Assert.fail; | ||
|
||
import org.easymock.EasyMock; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
public class BatchResultTest { | ||
|
||
private BatchResult<Boolean, BaseServiceException> result; | ||
|
||
@Before | ||
public void setUp() { | ||
result = new BatchResult<Boolean, BaseServiceException>() {}; | ||
} | ||
|
||
@Test | ||
public void testSuccess() { | ||
assertFalse(result.completed()); | ||
try { | ||
result.get(); | ||
fail("This was not completed yet."); | ||
} catch (IllegalStateException ex) { | ||
// expected | ||
} | ||
result.success(true); | ||
assertTrue(result.get()); | ||
// test that null is allowed | ||
result.success(null); | ||
} | ||
|
||
@Test | ||
public void testError() { | ||
assertFalse(result.completed()); | ||
try { | ||
result.get(); | ||
fail("This was not completed yet."); | ||
} catch (IllegalStateException ex) { | ||
// expected | ||
} | ||
try { | ||
result.error(null); | ||
fail(); | ||
} catch (NullPointerException exc) { | ||
// expected | ||
} | ||
BaseServiceException ex = new BaseServiceException(0, "message", "reason", false); | ||
result.error(ex); | ||
try { | ||
result.get(); | ||
fail("This is a failed operation and should have thrown a DnsException."); | ||
} catch (BaseServiceException real) { | ||
assertSame(ex, real); | ||
} | ||
} | ||
|
||
@Test | ||
public void testNotifyError() { | ||
final BaseServiceException ex = new BaseServiceException(0, "message", "reason", false); | ||
assertFalse(result.completed()); | ||
BatchResult.Callback<Boolean, BaseServiceException> callback = | ||
EasyMock.createStrictMock(BatchResult.Callback.class); | ||
callback.error(ex); | ||
EasyMock.replay(callback); | ||
result.notify(callback); | ||
result.error(ex); | ||
try { | ||
result.notify(callback); | ||
fail("The batch has been completed."); | ||
} catch (IllegalStateException exception) { | ||
// expected | ||
} | ||
EasyMock.verify(callback); | ||
} | ||
|
||
@Test | ||
public void testNotifySuccess() { | ||
assertFalse(result.completed()); | ||
BatchResult.Callback<Boolean, BaseServiceException> callback = | ||
EasyMock.createStrictMock(BatchResult.Callback.class); | ||
callback.success(true); | ||
EasyMock.replay(callback); | ||
result.notify(callback); | ||
result.success(true); | ||
try { | ||
result.notify(callback); | ||
fail("The batch has been completed."); | ||
} catch (IllegalStateException exception) { | ||
// expected | ||
} | ||
EasyMock.verify(callback); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.