fixed thread safety in test

This commit is contained in:
Adrian Cole 2012-01-11 20:12:44 -08:00
parent d6bf96d898
commit 2bff310db2
1 changed files with 34 additions and 26 deletions

View File

@ -18,14 +18,14 @@
*/ */
package org.jclouds.blobstore; package org.jclouds.blobstore;
import static org.easymock.classextension.EasyMock.createMock; import static org.easymock.EasyMock.createMock;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import org.easymock.classextension.EasyMock; import org.easymock.EasyMock;
import org.jclouds.blobstore.domain.PageSet; import org.jclouds.blobstore.domain.PageSet;
import org.jclouds.blobstore.domain.StorageMetadata; import org.jclouds.blobstore.domain.StorageMetadata;
import org.jclouds.blobstore.domain.internal.PageSetImpl; import org.jclouds.blobstore.domain.internal.PageSetImpl;
@ -38,11 +38,12 @@ import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
@Test(singleThreaded = true, testName = "BlobStoresTest")
public class BlobStoresTest { public class BlobStoresTest {
private final String containerName = "mycontainer"; private final String containerName = "mycontainer";
@Test(expectedExceptions={ContainerNotFoundException.class}) @Test(expectedExceptions = { ContainerNotFoundException.class })
public void testListAllForUnknownContainerFromTransientBlobStoreEagerly() throws Exception { public void testListAllForUnknownContainerFromTransientBlobStoreEagerly() throws Exception {
ListContainerOptions containerOptions = ListContainerOptions.NONE; ListContainerOptions containerOptions = ListContainerOptions.NONE;
ListAllOptions listAllOptions = ListAllOptions.Builder.eager(true); ListAllOptions listAllOptions = ListAllOptions.Builder.eager(true);
@ -58,7 +59,7 @@ public class BlobStoresTest {
/** /**
* Default listAll is not eager, so test that exception is thrown when first attempt to iterate. * Default listAll is not eager, so test that exception is thrown when first attempt to iterate.
*/ */
@Test(expectedExceptions={ContainerNotFoundException.class}) @Test(expectedExceptions = { ContainerNotFoundException.class })
public void testListAllForUnknownContainerFromTransientBlobStore() throws Exception { public void testListAllForUnknownContainerFromTransientBlobStore() throws Exception {
ListContainerOptions options = ListContainerOptions.NONE; ListContainerOptions options = ListContainerOptions.NONE;
BlobStoreContext context = new BlobStoreContextFactory().createContext("transient", "dummyid", "dummykey"); BlobStoreContext context = new BlobStoreContextFactory().createContext("transient", "dummyid", "dummykey");
@ -70,7 +71,7 @@ public class BlobStoresTest {
context.close(); context.close();
} }
} }
@Test @Test
public void testListAllFromTransientBlobStore() throws Exception { public void testListAllFromTransientBlobStore() throws Exception {
runListAllFromTransientBlobStore(false); runListAllFromTransientBlobStore(false);
@ -89,48 +90,53 @@ public class BlobStoresTest {
BlobStore blobStore = null; BlobStore blobStore = null;
try { try {
blobStore = context.getBlobStore(); blobStore = context.getBlobStore();
blobStore.createContainerInLocation(null, containerName); blobStore.createContainerInLocation(null, containerName);
Set<String> expectedNames = new HashSet<String>(); Set<String> expectedNames = new HashSet<String>();
for (int i = 0; i < NUM_BLOBS; i++) { for (int i = 0; i < NUM_BLOBS; i++) {
String blobName = "myname"+i; String blobName = "myname" + i;
blobStore.putBlob(containerName, blobStore.blobBuilder(blobName).payload("payload"+i).build()); blobStore.putBlob(containerName, blobStore.blobBuilder(blobName).payload("payload" + i).build());
expectedNames.add(blobName); expectedNames.add(blobName);
} }
ListAllOptions listAllOptions = ListAllOptions.Builder.eager(eager); ListAllOptions listAllOptions = ListAllOptions.Builder.eager(eager);
Iterable<StorageMetadata> iterable = BlobStores.listAll(blobStore, containerName, containerOptions, listAllOptions); Iterable<StorageMetadata> iterable = BlobStores.listAll(blobStore, containerName, containerOptions,
listAllOptions);
for (int i = 0; i < numTimesToIterate; i++) { for (int i = 0; i < numTimesToIterate; i++) {
Iterable<String> iterableNames = Iterables.transform(iterable, new Function<StorageMetadata,String>() { Iterable<String> iterableNames = Iterables.transform(iterable, new Function<StorageMetadata, String>() {
@Override public String apply(StorageMetadata input) { @Override
public String apply(StorageMetadata input) {
return input.getName(); return input.getName();
}}); }
});
// Note that blob.getMetadata being put does not equal blob metadata being retrieved
// Note that blob.getMetadata being put does not equal blob metadata being retrieved
// because uri is null in one and populated in the other. // because uri is null in one and populated in the other.
// Therefore we just compare names to ensure the iterator worked. // Therefore we just compare names to ensure the iterator worked.
assertEquals(ImmutableSet.copyOf(iterableNames), expectedNames); assertEquals(ImmutableSet.copyOf(iterableNames), expectedNames);
} }
} finally { } finally {
if (blobStore != null) blobStore.deleteContainer(containerName); if (blobStore != null)
blobStore.deleteContainer(containerName);
context.close(); context.close();
} }
} }
@Test @Test
public void testListAllWhenOnePage() throws Exception { public void testListAllWhenOnePage() throws Exception {
BlobStore blobStore = createMock(BlobStore.class); BlobStore blobStore = createMock(BlobStore.class);
ListContainerOptions options = ListContainerOptions.NONE; ListContainerOptions options = ListContainerOptions.NONE;
StorageMetadata v1 = createMock(StorageMetadata.class); StorageMetadata v1 = createMock(StorageMetadata.class);
PageSet<StorageMetadata> pageSet = new PageSetImpl<StorageMetadata>(Collections.singletonList(v1), null); PageSet<StorageMetadata> pageSet = new PageSetImpl<StorageMetadata>(Collections.singletonList(v1), null);
EasyMock.<PageSet<? extends StorageMetadata>>expect(blobStore.list(containerName, options)).andReturn(pageSet).once(); EasyMock.<PageSet<? extends StorageMetadata>> expect(blobStore.list(containerName, options)).andReturn(pageSet)
.once();
EasyMock.replay(blobStore); EasyMock.replay(blobStore);
Iterable<StorageMetadata> iterable = BlobStores.listAll(blobStore, containerName, options); Iterable<StorageMetadata> iterable = BlobStores.listAll(blobStore, containerName, options);
assertEquals(ImmutableList.copyOf(iterable), ImmutableList.of(v1)); assertEquals(ImmutableList.copyOf(iterable), ImmutableList.of(v1));
} }
@Test @Test
public void testListAllWhenTwoPages() throws Exception { public void testListAllWhenTwoPages() throws Exception {
BlobStore blobStore = createMock(BlobStore.class); BlobStore blobStore = createMock(BlobStore.class);
@ -140,11 +146,13 @@ public class BlobStoresTest {
StorageMetadata v2 = createMock(StorageMetadata.class); StorageMetadata v2 = createMock(StorageMetadata.class);
PageSet<StorageMetadata> pageSet = new PageSetImpl<StorageMetadata>(Collections.singletonList(v1), "marker1"); PageSet<StorageMetadata> pageSet = new PageSetImpl<StorageMetadata>(Collections.singletonList(v1), "marker1");
PageSet<StorageMetadata> pageSet2 = new PageSetImpl<StorageMetadata>(Collections.singletonList(v2), null); PageSet<StorageMetadata> pageSet2 = new PageSetImpl<StorageMetadata>(Collections.singletonList(v2), null);
EasyMock.<PageSet<? extends StorageMetadata>>expect(blobStore.list(containerName, options)).andReturn(pageSet).once(); EasyMock.<PageSet<? extends StorageMetadata>> expect(blobStore.list(containerName, options)).andReturn(pageSet)
EasyMock.<PageSet<? extends StorageMetadata>>expect(blobStore.list(containerName, options2)).andReturn(pageSet2).once(); .once();
EasyMock.<PageSet<? extends StorageMetadata>> expect(blobStore.list(containerName, options2)).andReturn(pageSet2)
.once();
EasyMock.replay(blobStore); EasyMock.replay(blobStore);
Iterable<StorageMetadata> iterable = BlobStores.listAll(blobStore, containerName, options); Iterable<StorageMetadata> iterable = BlobStores.listAll(blobStore, containerName, options);
assertEquals(ImmutableList.copyOf(iterable), ImmutableList.of(v1, v2)); assertEquals(ImmutableList.copyOf(iterable), ImmutableList.of(v1, v2));
} }