-
Notifications
You must be signed in to change notification settings - Fork 286
Fix missing return statements #3189
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
Merged
Merged
Changes from all commits
Commits
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 hidden or 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
158 changes: 158 additions & 0 deletions
158
ambry-commons/src/test/java/com/github/ambry/commons/InMemNamedBlobDbTest.java
This file contains hidden or 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,158 @@ | ||
| /** | ||
| * Copyright 2025 LinkedIn Corp. 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. | ||
| */ | ||
| package com.github.ambry.commons; | ||
|
|
||
| import com.github.ambry.named.DeleteResult; | ||
| import com.github.ambry.named.NamedBlobRecord; | ||
| import com.github.ambry.named.PutResult; | ||
| import com.github.ambry.protocol.GetOption; | ||
| import com.github.ambry.protocol.NamedBlobState; | ||
| import com.github.ambry.rest.RestServiceErrorCode; | ||
| import com.github.ambry.rest.RestServiceException; | ||
| import com.github.ambry.utils.MockTime; | ||
| import com.github.ambry.utils.Utils; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.concurrent.ExecutionException; | ||
| import org.junit.Test; | ||
|
|
||
| import static org.junit.Assert.*; | ||
|
|
||
|
|
||
| /** | ||
| * Test class for {@link InMemNamedBlobDb}. | ||
| */ | ||
| public class InMemNamedBlobDbTest { | ||
| private static final String ACCOUNT_NAME = "testAccount"; | ||
| private static final String CONTAINER_NAME = "testContainer"; | ||
| private static final String BLOB_NAME = "testBlob"; | ||
| private static final String BLOB_ID = "testBlobId"; | ||
| private static final long BLOB_SIZE = 1024L; | ||
|
|
||
| /** | ||
| * Test that getting an expired blob without Include_Expired_Blobs option returns Deleted error | ||
| * and does not complete the future successfully (tests the bug fix for missing return statement). | ||
| */ | ||
| @Test | ||
| public void testGetExpiredBlobReturnsDeletedError() throws Exception { | ||
| MockTime mockTime = new MockTime(1000L); | ||
| InMemNamedBlobDb db = new InMemNamedBlobDb(mockTime, 100, false); | ||
|
|
||
| // Create a blob with expiration time in the past | ||
| long expirationTime = 500L; // Expired (current time is 1000L) | ||
| NamedBlobRecord record = new NamedBlobRecord(ACCOUNT_NAME, CONTAINER_NAME, BLOB_NAME, BLOB_ID, expirationTime, | ||
| NamedBlobRecord.UNINITIALIZED_VERSION, BLOB_SIZE, 0, false); | ||
|
|
||
| // Put the blob | ||
| CompletableFuture<PutResult> putFuture = db.put(record, NamedBlobState.READY, false); | ||
| putFuture.get(); // Wait for put to complete | ||
|
|
||
| // Try to get the expired blob without Include_Expired_Blobs option | ||
| CompletableFuture<NamedBlobRecord> getFuture = db.get(ACCOUNT_NAME, CONTAINER_NAME, BLOB_NAME, GetOption.None, | ||
| false); | ||
|
|
||
| // Verify that the future completes exceptionally with Deleted error | ||
| try { | ||
| getFuture.get(); | ||
| fail("Expected RestServiceException with Deleted error code"); | ||
| } catch (ExecutionException e) { | ||
| assertTrue("Exception should be RestServiceException", e.getCause() instanceof RestServiceException); | ||
| RestServiceException restException = (RestServiceException) e.getCause(); | ||
| assertEquals("Error code should be Deleted", RestServiceErrorCode.Deleted, restException.getErrorCode()); | ||
| } | ||
|
|
||
| // Verify that the future is not completed successfully (this would happen if the return was missing) | ||
| assertTrue("Future should be completed exceptionally", getFuture.isCompletedExceptionally()); | ||
| assertFalse("Future should not be completed normally", getFuture.isDone() && !getFuture.isCompletedExceptionally()); | ||
| } | ||
|
|
||
| /** | ||
| * Test that getting an expired blob with Include_Expired_Blobs option returns the blob successfully. | ||
| */ | ||
| @Test | ||
| public void testGetExpiredBlobWithIncludeExpiredOption() throws Exception { | ||
| MockTime mockTime = new MockTime(1000L); | ||
| InMemNamedBlobDb db = new InMemNamedBlobDb(mockTime, 100, false); | ||
|
|
||
| // Create a blob with expiration time in the past | ||
| long expirationTime = 500L; // Expired (current time is 1000L) | ||
| NamedBlobRecord record = new NamedBlobRecord(ACCOUNT_NAME, CONTAINER_NAME, BLOB_NAME, BLOB_ID, expirationTime, | ||
| NamedBlobRecord.UNINITIALIZED_VERSION, BLOB_SIZE, 0, false); | ||
|
|
||
| // Put the blob | ||
| CompletableFuture<PutResult> putFuture = db.put(record, NamedBlobState.READY, false); | ||
| putFuture.get(); // Wait for put to complete | ||
|
|
||
| // Get the expired blob with Include_Expired_Blobs option | ||
| CompletableFuture<NamedBlobRecord> getFuture = db.get(ACCOUNT_NAME, CONTAINER_NAME, BLOB_NAME, | ||
| GetOption.Include_Expired_Blobs, false); | ||
|
|
||
| // Verify that the future completes successfully | ||
| NamedBlobRecord result = getFuture.get(); | ||
| assertNotNull("Result should not be null", result); | ||
| assertEquals("Blob name should match", BLOB_NAME, result.getBlobName()); | ||
| assertEquals("Expiration time should match", expirationTime, result.getExpirationTimeMs()); | ||
| } | ||
|
|
||
| /** | ||
| * Test that deleting a non-existent blob returns NotFound error and does not complete successfully | ||
| * (tests the bug fix for missing return statement in delete method). | ||
| */ | ||
| @Test | ||
| public void testDeleteNonExistentBlobReturnsNotFoundError() throws Exception { | ||
| MockTime mockTime = new MockTime(1000L); | ||
| InMemNamedBlobDb db = new InMemNamedBlobDb(mockTime, 100, false); | ||
|
|
||
| // Try to delete a blob that doesn't exist | ||
| CompletableFuture<DeleteResult> deleteFuture = db.delete(ACCOUNT_NAME, CONTAINER_NAME, BLOB_NAME); | ||
|
|
||
| // Verify that the future completes exceptionally with NotFound error | ||
| try { | ||
| deleteFuture.get(); | ||
| fail("Expected RestServiceException with NotFound error code"); | ||
| } catch (ExecutionException e) { | ||
| assertTrue("Exception should be RestServiceException", e.getCause() instanceof RestServiceException); | ||
| RestServiceException restException = (RestServiceException) e.getCause(); | ||
| assertEquals("Error code should be NotFound", RestServiceErrorCode.NotFound, restException.getErrorCode()); | ||
| } | ||
|
|
||
| // Verify that the future is not completed successfully (this would happen if the return was missing) | ||
| assertTrue("Future should be completed exceptionally", deleteFuture.isCompletedExceptionally()); | ||
| assertFalse("Future should not be completed normally", | ||
| deleteFuture.isDone() && !deleteFuture.isCompletedExceptionally()); | ||
| } | ||
|
|
||
| /** | ||
| * Test that deleting an existing blob completes successfully. | ||
| */ | ||
| @Test | ||
| public void testDeleteExistingBlob() throws Exception { | ||
| MockTime mockTime = new MockTime(1000L); | ||
| InMemNamedBlobDb db = new InMemNamedBlobDb(mockTime, 100, false); | ||
|
|
||
| // Create and put a blob | ||
| NamedBlobRecord record = new NamedBlobRecord(ACCOUNT_NAME, CONTAINER_NAME, BLOB_NAME, BLOB_ID, | ||
| Utils.Infinite_Time, NamedBlobRecord.UNINITIALIZED_VERSION, BLOB_SIZE, 0, false); | ||
| CompletableFuture<PutResult> putFuture = db.put(record, NamedBlobState.READY, false); | ||
| putFuture.get(); // Wait for put to complete | ||
|
|
||
| // Delete the blob | ||
| CompletableFuture<DeleteResult> deleteFuture = db.delete(ACCOUNT_NAME, CONTAINER_NAME, BLOB_NAME); | ||
|
|
||
| // Verify that the future completes successfully | ||
| DeleteResult result = deleteFuture.get(); | ||
| assertNotNull("Result should not be null", result); | ||
| assertFalse("Blob versions should not be empty", result.getBlobVersions().isEmpty()); | ||
| } | ||
| } | ||
|
|
||
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.
nit: 2nd assert is redundant. we could just keep one of these asserts.