Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* ****************************************************************************
* Copyright 2014-2016 Spectra Logic Corporation. 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. A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file.
* This file 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.spectralogic.ds3client;

public final class IntValue {
private int intValue = 0;

public int increment() {
return ++intValue;
}

public int getValue() {
return intValue;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* ****************************************************************************
* Copyright 2014-2016 Spectra Logic Corporation. 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. A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file.
* This file 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.spectralogic.ds3client;

import com.google.common.collect.Lists;
import com.spectralogic.ds3client.networking.Headers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class MockedHeaders implements Headers {
private final Map<String, String> headerValues;

public MockedHeaders(final Map<String, String> headerValues) {
this.headerValues = normalizeHeaderValues(headerValues);
}

private static Map<String, String> normalizeHeaderValues(final Map<String, String> headerValues) {
final Map<String, String> headers = new HashMap<>();
for (final Map.Entry<String, String> entry : headerValues.entrySet()) {
headers.put(entry.getKey().toLowerCase(), entry.getValue());
}
return headers;
}

@Override
public List<String> get(final String key) {
return Lists.newArrayList(this.headerValues.get(key.toLowerCase()));
}

@Override
public Set<String> keys() {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* ****************************************************************************
* Copyright 2014-2016 Spectra Logic Corporation. 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. A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file.
* This file 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.spectralogic.ds3client;

import com.spectralogic.ds3client.networking.Headers;
import com.spectralogic.ds3client.networking.WebResponse;

import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

public class MockedWebResponse implements WebResponse {
private final InputStream responseStream;
private final int statusCode;
private final Headers headers;

public MockedWebResponse(final String responseString, final int statusCode, final Map<String, String> headers) {
this.responseStream = IOUtils.toInputStream(responseString);
this.statusCode = statusCode;
this.headers = new MockedHeaders(headers);
}

@Override
public InputStream getResponseStream() throws IOException {
return this.responseStream;
}

@Override
public int getStatusCode() {
return this.statusCode;
}

@Override
public Headers getHeaders() {
return headers;
}

@Override
public void close() throws IOException {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,23 @@
import com.google.common.collect.Lists;
import com.spectralogic.ds3client.Ds3Client;
import com.spectralogic.ds3client.Ds3ClientImpl;
import com.spectralogic.ds3client.IntValue;
import com.spectralogic.ds3client.commands.DeleteObjectRequest;
import com.spectralogic.ds3client.commands.GetObjectRequest;
import com.spectralogic.ds3client.commands.GetObjectResponse;
import com.spectralogic.ds3client.commands.PutObjectRequest;
import com.spectralogic.ds3client.commands.spectrads3.GetJobSpectraS3Request;
import com.spectralogic.ds3client.commands.spectrads3.GetJobSpectraS3Response;
import com.spectralogic.ds3client.helpers.Ds3ClientHelpers;
import com.spectralogic.ds3client.helpers.FailureEventListener;
import com.spectralogic.ds3client.helpers.FileObjectGetter;
import com.spectralogic.ds3client.helpers.FileObjectPutter;
import com.spectralogic.ds3client.helpers.events.FailureEvent;
import com.spectralogic.ds3client.helpers.options.ReadJobOptions;
import com.spectralogic.ds3client.integration.test.helpers.ABMTestHelper;
import com.spectralogic.ds3client.integration.test.helpers.Ds3ClientShim;
import com.spectralogic.ds3client.integration.test.helpers.Ds3ClientShimFactory;
import com.spectralogic.ds3client.integration.test.helpers.Ds3ClientShimWithFailedChunkAllocation;
import com.spectralogic.ds3client.integration.test.helpers.TempStorageIds;
import com.spectralogic.ds3client.integration.test.helpers.TempStorageUtil;
import com.spectralogic.ds3client.models.ChecksumType;
Expand Down Expand Up @@ -61,6 +66,7 @@
import static com.spectralogic.ds3client.integration.Util.RESOURCE_BASE_NAME;
import static com.spectralogic.ds3client.integration.Util.deleteAllContents;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

Expand Down Expand Up @@ -309,4 +315,97 @@ public void testPartialRetriesWithInjectedFailures() throws NoSuchMethodExceptio
deleteBigFileFromBlackPearlBucket();
}
}

@Test
public void testFiringFailureHandlerWhenGettingChunks()
throws URISyntaxException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, IOException
{
putBigFile();

final String tempPathPrefix = null;
final Path tempDirectory = Files.createTempDirectory(Paths.get("."), tempPathPrefix);

try {
final IntValue numFailuresRecorded = new IntValue();

final FailureEventListener failureEventListener = new FailureEventListener() {
@Override
public void onFailure(final FailureEvent failureEvent) {
numFailuresRecorded.increment();
assertEquals(FailureEvent.FailureActivity.GettingObject, failureEvent.doingWhat());
}
};

final Ds3ClientHelpers.Job readJob = createReadJobWithObjectsReadyToTransfer(Ds3ClientShimFactory.ClientFailureType.ChunkAllocation);

readJob.attachFailureEventListener(failureEventListener);

try {
readJob.transfer(new FileObjectGetter(tempDirectory));
} catch (final IOException e) {
assertEquals(1, numFailuresRecorded.getValue());
}
} finally {
FileUtils.deleteDirectory(tempDirectory.toFile());
deleteBigFileFromBlackPearlBucket();
}
}

private Ds3ClientHelpers.Job createReadJobWithObjectsReadyToTransfer(final Ds3ClientShimFactory.ClientFailureType clientFailureType)
throws IOException, URISyntaxException, NoSuchMethodException, IllegalAccessException, InvocationTargetException
{
final String DIR_NAME = "largeFiles/";
final String FILE_NAME = "lesmis-copies.txt";

final Path objPath = ResourceUtils.loadFileResource(DIR_NAME + FILE_NAME);
final long bookSize = Files.size(objPath);
final Ds3Object obj = new Ds3Object(FILE_NAME, bookSize);

final Ds3Client ds3Client = Ds3ClientShimFactory.makeWrappedDs3Client(clientFailureType, client);

final int maxNumBlockAllocationRetries = 3;
final int maxNumObjectTransferAttempts = 3;
final Ds3ClientHelpers ds3ClientHelpers = Ds3ClientHelpers.wrap(ds3Client,
maxNumBlockAllocationRetries,
maxNumObjectTransferAttempts);

final Ds3ClientHelpers.Job readJob = ds3ClientHelpers.startReadJob(BUCKET_NAME, Arrays.asList(obj));

return readJob;
}

@Test
public void testFiringFailureHandlerWhenGettingObject()
throws URISyntaxException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, IOException
{
putBigFile();

final String tempPathPrefix = null;
final Path tempDirectory = Files.createTempDirectory(Paths.get("."), tempPathPrefix);

try {
final IntValue numFailuresRecorded = new IntValue();

final FailureEventListener failureEventListener = new FailureEventListener() {
@Override
public void onFailure(final FailureEvent failureEvent) {
numFailuresRecorded.increment();
assertEquals(FailureEvent.FailureActivity.GettingObject, failureEvent.doingWhat());
}
};

final Ds3ClientHelpers.Job readJob = createReadJobWithObjectsReadyToTransfer(Ds3ClientShimFactory.ClientFailureType.GetObject);

readJob.attachFailureEventListener(failureEventListener);

try {
readJob.transfer(new FileObjectGetter(tempDirectory));
} catch (final IOException e) {
assertEquals(1, numFailuresRecorded.getValue());
}
} finally {
FileUtils.deleteDirectory(tempDirectory.toFile());
deleteBigFileFromBlackPearlBucket();
}
}
}
Loading