-
Notifications
You must be signed in to change notification settings - Fork 453
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
Test case that fails due to NullPointerException #1346
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
201 changes: 201 additions & 0 deletions
201
google-http-client-jackson2/src/test/java/com/google/api/client/json/jackson2/BatchTest.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,201 @@ | ||
/* | ||
* | ||
* * Copyright 2021 Google LLC. | ||
* * | ||
* * 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.api.client.json.jackson2; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.fail; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.google.api.client.googleapis.batch.BatchRequest; | ||
import com.google.api.client.googleapis.batch.json.JsonBatchCallback; | ||
import com.google.api.client.googleapis.json.GoogleJsonError; | ||
import com.google.api.client.http.HttpHeaders; | ||
import com.google.api.client.http.HttpRequest; | ||
import com.google.api.client.http.HttpRequestInitializer; | ||
import com.google.api.client.http.HttpResponse; | ||
import com.google.api.client.http.HttpResponseException; | ||
import com.google.api.client.http.HttpUnsuccessfulResponseHandler; | ||
import com.google.api.client.http.LowLevelHttpResponse; | ||
import com.google.api.client.json.Json; | ||
import com.google.api.client.testing.http.MockHttpTransport; | ||
import com.google.api.client.testing.http.MockLowLevelHttpRequest; | ||
import com.google.api.services.storage.Storage; | ||
import com.google.api.services.storage.model.StorageObject; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import org.junit.Test; | ||
import org.mockito.Mockito; | ||
|
||
/** | ||
* Test to verify https://github.com/apache/beam/pull/14527#discussion_r613980011. | ||
* | ||
* <p>I wanted to put this in google-http-client module, but google-http-client-json dependency | ||
* would create a dependency cycle. Therefore I place this in this class. | ||
*/ | ||
public class BatchTest { | ||
|
||
private static InputStream toStream(String content) throws IOException { | ||
return new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)); | ||
} | ||
|
||
// This test case tries to simulate Beam's GcsUtilTest, where it reads the content of failed | ||
// response and throws corresponding IOException after a retry on status code 429. | ||
// https://github.com/apache/beam/pull/14527/files#diff-1b8fce5e4444d5c3e99bd0564a8848f9e6d232550efb67902bfeb5ac53819836R505 | ||
@Test | ||
public void testErrorContentReadRetry() throws IOException { | ||
String contentBoundary = "batch_foobarbaz"; | ||
String contentBoundaryLine = "--" + contentBoundary; | ||
String endOfContentBoundaryLine = "--" + contentBoundary + "--"; | ||
String content = | ||
contentBoundaryLine | ||
+ "\n" | ||
+ "Content-Type: application/http\n" | ||
+ "\n" | ||
+ "HTTP/1.1 404 Not Found\n" | ||
+ "Content-Length: -1\n" | ||
+ "\n" | ||
+ "{\"error\":{\"code\":404}}" | ||
+ "\n" | ||
+ "\n" | ||
+ endOfContentBoundaryLine | ||
+ "\n"; | ||
final LowLevelHttpResponse mockResponse = Mockito.mock(LowLevelHttpResponse.class); | ||
when(mockResponse.getContentType()) | ||
.thenReturn("text/plain; charset=UTF-8") | ||
.thenReturn("multipart/mixed; boundary=" + contentBoundary); | ||
|
||
// 429: Too many requests, then 200: OK. | ||
final int statusCode429_TooManyRequest = 429; | ||
when(mockResponse.getStatusCode()).thenReturn(statusCode429_TooManyRequest, 200); | ||
when(mockResponse.getContent()).thenReturn(toStream("rateLimitExceeded"), toStream(content)); | ||
|
||
MockHttpTransport mockTransport = | ||
new MockHttpTransport.Builder() | ||
.setLowLevelHttpRequest( | ||
new MockLowLevelHttpRequest() { | ||
@Override | ||
public LowLevelHttpResponse execute() throws IOException { | ||
return mockResponse; | ||
} | ||
}) | ||
.build(); | ||
|
||
HttpRequestInitializer httpRequestInitializer = | ||
new HttpRequestInitializer() { | ||
@Override | ||
public void initialize(HttpRequest request) throws IOException { | ||
request.setUnsuccessfulResponseHandler( | ||
new HttpUnsuccessfulResponseHandler() { | ||
@Override | ||
public boolean handleResponse( | ||
HttpRequest request, HttpResponse response, boolean supportsRetry) | ||
throws IOException { | ||
// true to retry | ||
boolean willRetry = response.getStatusCode() == statusCode429_TooManyRequest; | ||
return willRetry; | ||
} | ||
}); | ||
} | ||
}; | ||
Storage storageClient = | ||
new Storage(mockTransport, JacksonFactory.getDefaultInstance(), httpRequestInitializer); | ||
BatchRequest batch = storageClient.batch(httpRequestInitializer); | ||
|
||
Storage.Objects.Get getRequest = storageClient.objects().get("testbucket", "testobject"); | ||
|
||
final GoogleJsonError[] capturedGoogleJsonError = new GoogleJsonError[1]; | ||
getRequest.queue( | ||
batch, | ||
new JsonBatchCallback<StorageObject>() { | ||
@Override | ||
public void onSuccess(StorageObject response, HttpHeaders httpHeaders) | ||
throws IOException { | ||
System.out.println("Got response: " + response); | ||
} | ||
|
||
@Override | ||
public void onFailure(GoogleJsonError e, HttpHeaders httpHeaders) throws IOException { | ||
System.out.println("Got error: " + e); | ||
capturedGoogleJsonError[0] = e; | ||
} | ||
}); | ||
|
||
batch.execute(); | ||
assertNotNull(capturedGoogleJsonError[0]); | ||
|
||
// From {"error":{"code":404}} | ||
assertEquals(404, capturedGoogleJsonError[0].getCode()); | ||
} | ||
|
||
@Test | ||
public void testErrorContentRead_NoRetry() throws IOException { | ||
final LowLevelHttpResponse mockResponse = Mockito.mock(LowLevelHttpResponse.class); | ||
when(mockResponse.getContentType()).thenReturn(Json.MEDIA_TYPE); | ||
|
||
// 429: Too many requests | ||
when(mockResponse.getStatusCode()).thenReturn(429); | ||
|
||
// This value is dummy | ||
String contentInError = "{\"error\":{\"code\":429}}"; | ||
when(mockResponse.getContent()).thenReturn(toStream(contentInError)); | ||
|
||
MockHttpTransport mockTransport = | ||
new MockHttpTransport.Builder() | ||
.setLowLevelHttpRequest( | ||
new MockLowLevelHttpRequest() { | ||
@Override | ||
public LowLevelHttpResponse execute() throws IOException { | ||
return mockResponse; | ||
} | ||
}) | ||
.build(); | ||
|
||
// No retry | ||
Storage storageClient = new Storage(mockTransport, JacksonFactory.getDefaultInstance(), null); | ||
BatchRequest batch = storageClient.batch(); | ||
|
||
Storage.Objects.Get getRequest = storageClient.objects().get("testbucket", "testobject"); | ||
|
||
getRequest.queue( | ||
batch, | ||
new JsonBatchCallback<StorageObject>() { | ||
@Override | ||
public void onSuccess(StorageObject response, HttpHeaders httpHeaders) | ||
throws IOException { | ||
System.out.println("Got response: " + response); | ||
} | ||
|
||
@Override | ||
public void onFailure(GoogleJsonError e, HttpHeaders httpHeaders) throws IOException { | ||
System.out.println("Got error: " + e); | ||
} | ||
}); | ||
|
||
try { | ||
batch.execute(); | ||
fail("batch.execute should throw an exception"); | ||
} catch (HttpResponseException ex) { | ||
assertEquals(contentInError, ex.getContent()); | ||
} | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This succeeds. Maybe problem in retry?