From 177735d070ba4b1e34e8f0e6fabfff0ab910995a Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Sun, 15 Oct 2006 17:50:08 +0000 Subject: Created StorageCollection. More tests added. --- .../carbonado/repo/replicated/TestCursors.java | 204 ++++++++++++++ .../repo/replicated/TestProxiedStorable.java | 219 +++++++++++++++ .../carbonado/repo/replicated/TestRepair.java | 294 +++++++++++++++++++++ 3 files changed, 717 insertions(+) create mode 100644 src/test/java/com/amazon/carbonado/repo/replicated/TestCursors.java create mode 100644 src/test/java/com/amazon/carbonado/repo/replicated/TestProxiedStorable.java create mode 100644 src/test/java/com/amazon/carbonado/repo/replicated/TestRepair.java (limited to 'src/test/java/com/amazon/carbonado/repo') diff --git a/src/test/java/com/amazon/carbonado/repo/replicated/TestCursors.java b/src/test/java/com/amazon/carbonado/repo/replicated/TestCursors.java new file mode 100644 index 0000000..d9a6b38 --- /dev/null +++ b/src/test/java/com/amazon/carbonado/repo/replicated/TestCursors.java @@ -0,0 +1,204 @@ +/* + * Copyright 2006 Amazon Technologies, Inc. or its affiliates. + * Amazon, Amazon.com and Carbonado are trademarks or registered trademarks + * of Amazon Technologies, Inc. or its affiliates. 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.amazon.carbonado.repo.replicated; + +import java.util.Collection; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.Iterator; +import java.util.Map; + +import junit.framework.TestCase; +import junit.framework.TestSuite; + +import com.amazon.carbonado.Repository; +import com.amazon.carbonado.RepositoryBuilder; +import com.amazon.carbonado.Storage; +import com.amazon.carbonado.Cursor; +import com.amazon.carbonado.Query; +import com.amazon.carbonado.FetchException; +import com.amazon.carbonado.RepositoryException; +import com.amazon.carbonado.ConfigurationException; + +import com.amazon.carbonado.TestUtilities; +import com.amazon.carbonado.stored.StorableTestBasic; +import com.amazon.carbonado.stored.STBContainer; + +/** + * + * @author Don Schneider + */ +public class TestCursors extends TestCase { + + public static void main(String[] args) { + junit.textui.TestRunner.run(suite()); + } + + public static TestSuite suite() { + return new TestSuite(TestCursors.class); + } + + public TestCursors(String s) { + super(s); + } + + public TestCursors() { + this("default"); + } + + /** + * Test joins + */ + public void test_joins() throws Exception { + Repository repository = TestUtilities.buildTempRepository("test_joins"); + setup(repository); + do_test_joins(repository); + repository.close(); + repository = null; + } + + public void do_test_joins(Repository repository) throws Exception { + Storage storage = repository.storageFor(STBContainer.class); + STBContainer s = storage.prepare(); + s.setName("first"); + s.setCategory("imaString_1"); + s.setCount(1000001); + + Collection ids = checkContents(s, 1); + + assertEquals(10, ids.size()); + + LinkedHashMap> allIdCollections = + new LinkedHashMap>(); + Cursor cSTBC = storage.query().fetch(); + while (cSTBC.hasNext()) { + STBContainer stbc = cSTBC.next(); + ids = checkContents(stbc, stbc.getCount()); + for (Map.Entry entry : allIdCollections.entrySet()) { + if (entry.getKey().equals(stbc.getCategory())) { + assertTrue(ids.equals(entry.getValue())); + } else { + assertFalse(ids.equals(entry.getValue())); + } + } + allIdCollections.put(stbc.getCategory(), ids); + } + } + + /** + * Test queries + */ + public void test_queries() throws Exception { + Repository repository = TestUtilities.buildTempRepository("test_queries"); + setup(repository); + do_test_queries(repository); + repository.close(); + } + + public void do_test_queries(Repository repository) throws Exception { + Storage storage = + repository.storageFor(StorableTestBasic.class); + filterTest(storage, "id > ? & id < ?", 10, 20, 11, 20); + filterTest(storage, "id >= ? & id < ?", 10, 20, 10, 20); + filterTest(storage, "id > ? & id <= ?", 10, 20, 11, 21); + filterTest(storage, "id >= ? & id <= ?", 10, 20, 10, 21); + } + + /** + * Test RRQueries + * Includes an implicit test of the repository configuration + */ + public void test_RRQueries() throws Exception { + RepositoryBuilder replicaRepository = TestUtilities.newTempRepositoryBuilder(); + RepositoryBuilder masterRepository = TestUtilities.newTempRepositoryBuilder("alt"); + + ReplicatedRepositoryBuilder builder = new ReplicatedRepositoryBuilder(); + try { + builder.assertReady(); + fail("unready builder passed assertReady()"); + } + catch (ConfigurationException e) { + // expected + } + builder.setName("test"); + try { + builder.assertReady(); + fail("unready builder passed assertReady()"); + } + catch (ConfigurationException e) { + // expected + } + builder.setReplicaRepositoryBuilder(replicaRepository); + try { + builder.assertReady(); + fail("builder config passed assertReady()"); + } + catch (ConfigurationException e) { + // expected + } + builder.setMasterRepositoryBuilder(masterRepository); + builder.assertReady(); + + Repository RR = builder.build(); + + setup(RR); + + do_test_joins(RR); + do_test_queries(RR); + + RR.close(); + } + + private Collection checkContents(STBContainer s, int count) throws FetchException { + Query q = s.getContained(); + Cursor cSTB = q.fetch(); + Collection ids = new HashSet(15); + while (cSTB.hasNext()) { + StorableTestBasic stb = cSTB.next(); + assertEquals("imaString_" + count % 10, stb.getStringProp()); + Integer id = new Integer(stb.getId()); + assertFalse(ids.contains(id)); + ids.add(id); + } + return ids; + } + + private void filterTest(Storage storage, + String queryString, + int min, + int max, + int first, + int last) + throws Exception + { + Query q = storage.query(queryString).with(min).with(max); + Cursor c = q.fetch(); + int i = first; + while (c.hasNext()) { + StorableTestBasic item = c.next(); + assertEquals(queryString, i, item.getId()); + i++; + } + assertEquals(queryString, last, i); + } + + private void setup(Repository repository) throws RepositoryException { + StorableTestBasic.insertBunches(repository, 100); + STBContainer.insertBunches(repository, 30); + } +} diff --git a/src/test/java/com/amazon/carbonado/repo/replicated/TestProxiedStorable.java b/src/test/java/com/amazon/carbonado/repo/replicated/TestProxiedStorable.java new file mode 100644 index 0000000..6de2faf --- /dev/null +++ b/src/test/java/com/amazon/carbonado/repo/replicated/TestProxiedStorable.java @@ -0,0 +1,219 @@ +/* + * Copyright 2006 Amazon Technologies, Inc. or its affiliates. + * Amazon, Amazon.com and Carbonado are trademarks or registered trademarks + * of Amazon Technologies, Inc. or its affiliates. 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.amazon.carbonado.repo.replicated; + +import java.lang.reflect.Method; + +import junit.framework.TestSuite; +import com.amazon.carbonado.TestUtilities; +import com.amazon.carbonado.spi.StorableInterceptorFactory; +import com.amazon.carbonado.stored.StorableTestBasic; +import com.amazon.carbonado.stored.StorableTestBasicIdMunger; +import com.amazon.carbonado.TestStorables; + +import com.amazon.carbonado.Repository; +import com.amazon.carbonado.Storable; +import com.amazon.carbonado.Storage; +import com.amazon.carbonado.SupportException; +import com.amazon.carbonado.FetchException; +import com.amazon.carbonado.TestStorableBase; + +import com.amazon.carbonado.spi.WrappedStorage; + +/* + * TestProxiedStorable + * + * @author Don Schneider + */ +public class TestProxiedStorable extends TestStorableBase { + public static void main(String[] args) { + junit.textui.TestRunner.run(suite()); + } + + public static TestSuite suite() { + return new TestSuite(TestProxiedStorable.class); + } + + public TestProxiedStorable() { + super(); + } + + /** + * Test copy storable properties interface + */ + public void test_copyStorableProperties() throws Exception { + Storage storage = getRepository().storageFor(StorableTestBasic.class); + StorableTestBasic storable = storage.prepare(); + + TestStorables.InvocationTracker tracker = new TestStorables.InvocationTracker("tracker"); + storable.copyAllProperties(tracker); + // unloaded, not dirty; nothing happens + tracker.assertTrack(0); + + storable.setId(1); + storable.setIntProp(1); + storable.copyAllProperties(tracker); + // setId, setIntProp + tracker.assertTrack(0x1 + 0x10); + tracker.clearTracks(); + + setBasicProperties(storable); + storable.copyAllProperties(tracker); + // setXxx + tracker.assertTrack(TestStorables.ALL_SET_METHODS); + tracker.clearTracks(); + + storable = storage.prepare(); + storable.copyPrimaryKeyProperties(tracker); + tracker.assertTrack(0); + setPrimaryKeyProperties(storable); + storable.copyPrimaryKeyProperties(tracker); + // setXxx + tracker.assertTrack(TestStorables.ALL_PRIMARY_KEYS); + tracker.clearTracks(); + + storable = storage.prepare(); + storable.copyUnequalProperties(tracker); + // Better not do anything since unloaded + tracker.assertTrack(0); + storable.setIntProp(0); // this will now be dirty, and equal + storable.copyUnequalProperties(tracker); + // Should only examine one dirty property since still unloaded + tracker.assertTrack(0x8); + storable.setIntProp(1); // this will now be dirty and not equal + storable.copyUnequalProperties(tracker); + tracker.assertTrack(0x8 + 0x10); + + // get a fresh one + storable = storage.prepare(); + storable.setStringProp("hi"); + storable.setId(22); + storable.copyPrimaryKeyProperties(tracker); + storable.copyDirtyProperties(tracker); + // setId, setStringProp + tracker.assertTrack(0x1 + 0x4); + } + + /** + * Test Interceptor + */ + public void test_Interceptor() throws Exception { + Storage storage = getRepository().storageFor(StorableTestBasic.class); + StorableTestBasic storable = storage.prepare(); + + StorableInterceptorFactory proxyFactory + = StorableInterceptorFactory.getInstance(StorableTestBasicIdMunger.class, + StorableTestBasic.class, + false); + StorableTestBasic proxy = proxyFactory.create(storable); + + proxy.setId(1); + assertEquals(storable.getId(), 2<<8); + + storable.setId(5<<8); + assertEquals(proxy.getId(), 4); + + proxy.setStringProp("passthrough"); + assertEquals("passthrough", storable.getStringProp()); + } + + /** + * Test replicatingStorable + */ + public void test_replicatingStorable() throws Exception { + Repository altRepo = TestUtilities.buildTempRepository("alt"); + + final Storage readage = + getRepository().storageFor(StorableTestBasic.class); + final Storage writage = altRepo.storageFor(StorableTestBasic.class); + + Storage wrappage = + new ReplicatedStorage(getRepository(), readage, writage); + + StorableTestBasic replicator = wrappage.prepare(); + + replicator.setId(1); + setBasicProperties(replicator); + replicator.insert(); + + StorableTestBasic reader = load(readage, 1); + StorableTestBasic writer = load(writage, 1); + + assertTrue(reader.equalProperties(writer)); + + assertStorableEquivalenceById(1, readage, writage); + + replicator = wrappage.prepare(); + + replicator.setId(1); + replicator.setStringProp("updated"); + replicator.setLongProp(2342332); + replicator.update(); + writer = load(writage, 1); + reader = load(readage, 1); + assertTrue(reader.equalProperties(writer)); + + replicator.delete(); + + + try { + reader.load(); + fail("successfully loaded deleted 'read' storable"); + } + catch (FetchException e) { + // expected + } + try { + writer.load(); + fail("successfully loaded deleted 'write' storable"); + } + catch (FetchException e) { + // expected + } + + StorableTestBasic replicator2 = wrappage.prepare(); + + replicator2.setId(2); + setBasicProperties(replicator2); + replicator2.insert(); + + // Now use the old replicator (which should be in "unloaded" state, since we've + // just finished deleting something, to delete it. + replicator.setId(2); + replicator.delete(); + + try { + load(readage, 2); + fail("successfully loaded deleted 'read' storable 2"); + } + catch (FetchException e) { + // expected + } + try { + load(writage, 2); + fail("successfully loaded deleted 'write' storable 2"); + } + catch (FetchException e) { + // expected + } + + altRepo.close(); + altRepo = null; + } + +} diff --git a/src/test/java/com/amazon/carbonado/repo/replicated/TestRepair.java b/src/test/java/com/amazon/carbonado/repo/replicated/TestRepair.java new file mode 100644 index 0000000..d21fceb --- /dev/null +++ b/src/test/java/com/amazon/carbonado/repo/replicated/TestRepair.java @@ -0,0 +1,294 @@ +/* + * Copyright 2006 Amazon Technologies, Inc. or its affiliates. + * Amazon, Amazon.com and Carbonado are trademarks or registered trademarks + * of Amazon Technologies, Inc. or its affiliates. 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.amazon.carbonado.repo.replicated; + +import junit.framework.TestCase; +import junit.framework.TestSuite; + +import com.amazon.carbonado.OptimisticLockException; +import com.amazon.carbonado.Repository; +import com.amazon.carbonado.RepositoryBuilder; +import com.amazon.carbonado.Storage; +import com.amazon.carbonado.UniqueConstraintException; + +import com.amazon.carbonado.repo.replicated.ReplicatedRepository; +import com.amazon.carbonado.repo.replicated.ReplicatedRepositoryBuilder; + +import com.amazon.carbonado.TestUtilities; +import com.amazon.carbonado.stored.StorableTestBasic; +import com.amazon.carbonado.stored.StorableVersioned; + +/** + * + * + * @author Brian S O'Neill + */ +public class TestRepair extends TestCase { + public static void main(String[] args) { + junit.textui.TestRunner.run(suite()); + } + + public static TestSuite suite() { + return new TestSuite(TestRepair.class); + } + + private Repository mReplica; + private Repository mMaster; + private Repository mReplicated; + + public TestRepair(String name) { + super(name); + } + + protected void setUp() throws Exception { + RepositoryBuilder replica = TestUtilities.newTempRepositoryBuilder("rr-replica"); + RepositoryBuilder master = TestUtilities.newTempRepositoryBuilder("rr-master"); + + ReplicatedRepositoryBuilder builder = new ReplicatedRepositoryBuilder(); + builder.setName("rr"); + builder.setReplicaRepositoryBuilder(replica); + builder.setMasterRepositoryBuilder(master); + + ReplicatedRepository rr = (ReplicatedRepository) builder.build(); + + mReplica = rr.getReplicaRepository(); + mMaster = rr.getMasterRepository(); + mReplicated = rr; + } + + protected void tearDown() throws Exception { + mReplicated.close(); + mReplica = null; + mMaster = null; + mReplicated = null; + } + + public void testMissingEntry() throws Exception { + // Insert an entry into master. + { + Storage storage = mMaster.storageFor(StorableTestBasic.class); + StorableTestBasic stb = storage.prepare(); + stb.setId(5); + stb.setStringProp("hello"); + stb.setIntProp(1); + stb.setLongProp(1L); + stb.setDoubleProp(1.0); + stb.insert(); + } + + // Verify not available from rr. + { + Storage storage = mReplicated.storageFor(StorableTestBasic.class); + StorableTestBasic stb = storage.prepare(); + stb.setId(5); + assertFalse(stb.tryLoad()); + } + + // Insert into rr. + { + Storage storage = mReplicated.storageFor(StorableTestBasic.class); + StorableTestBasic stb = storage.prepare(); + stb.setId(5); + stb.setStringProp("world"); + stb.setIntProp(1); + stb.setLongProp(1L); + stb.setDoubleProp(1.0); + try { + stb.insert(); + fail(); + } catch (UniqueConstraintException e) { + } + } + + // Wait a moment for repair thread to finish. + Thread.sleep(1000); + + // Verify available from rr. + { + Storage storage = mReplicated.storageFor(StorableTestBasic.class); + StorableTestBasic stb = storage.prepare(); + stb.setId(5); + assertTrue(stb.tryLoad()); + assertEquals("hello", stb.getStringProp()); + } + } + + public void testMissingVersionedEntry() throws Exception { + // Insert an entry into master. + { + Storage storage = mMaster.storageFor(StorableVersioned.class); + StorableVersioned sv = storage.prepare(); + sv.setID(5); + sv.setValue("hello"); + sv.insert(); + } + + // Verify not available from rr. + { + Storage storage = mReplicated.storageFor(StorableVersioned.class); + StorableVersioned sv = storage.prepare(); + sv.setID(5); + assertFalse(sv.tryLoad()); + } + + // Insert into rr. + { + Storage storage = mReplicated.storageFor(StorableVersioned.class); + StorableVersioned sv = storage.prepare(); + sv.setID(5); + sv.setValue("world"); + try { + sv.insert(); + fail(); + } catch (UniqueConstraintException e) { + } + } + + // Wait a moment for repair thread to finish. + Thread.sleep(1000); + + // Verify available from rr. + { + Storage storage = mReplicated.storageFor(StorableVersioned.class); + StorableVersioned sv = storage.prepare(); + sv.setID(5); + assertTrue(sv.tryLoad()); + assertEquals("hello", sv.getValue()); + assertEquals(1, sv.getVersion()); + } + } + + public void testStaleEntry() throws Exception { + // Insert an entry into rr. + { + Storage storage = mReplicated.storageFor(StorableVersioned.class); + StorableVersioned sv = storage.prepare(); + sv.setID(5); + sv.setValue("hello"); + sv.insert(); + } + + // Update master entry. + { + Storage storage = mMaster.storageFor(StorableVersioned.class); + StorableVersioned sv = storage.prepare(); + sv.setID(5); + sv.load(); + sv.setValue("world"); + sv.update(); + } + + // Verify old version in replica. + { + Storage storage = mReplica.storageFor(StorableVersioned.class); + StorableVersioned sv = storage.prepare(); + sv.setID(5); + sv.load(); + assertEquals(1, sv.getVersion()); + assertEquals("hello", sv.getValue()); + } + + // Attempt to update rr entry. + { + Storage storage = mReplicated.storageFor(StorableVersioned.class); + StorableVersioned sv = storage.prepare(); + sv.setID(5); + sv.load(); + assertEquals(1, sv.getVersion()); + assertEquals("hello", sv.getValue()); + + sv.setValue("ciao"); + try { + sv.update(); + fail(); + } catch (OptimisticLockException e) { + } + } + + // Wait a moment for repair thread to finish. + Thread.sleep(1000); + + // Verify new version in rr and update it. + { + Storage storage = mReplicated.storageFor(StorableVersioned.class); + StorableVersioned sv = storage.prepare(); + sv.setID(5); + sv.load(); + assertEquals(2, sv.getVersion()); + assertEquals("world", sv.getValue()); + + sv.setValue("ciao"); + sv.update(); + } + } + + public void testStaleEntryAndBackoff() throws Exception { + // Insert an entry into rr. + { + Storage storage = mReplicated.storageFor(StorableVersioned.class); + StorableVersioned sv = storage.prepare(); + sv.setID(5); + sv.setValue("hello"); + sv.insert(); + } + + // Update master entry. + { + Storage storage = mMaster.storageFor(StorableVersioned.class); + StorableVersioned sv = storage.prepare(); + sv.setID(5); + sv.load(); + sv.setValue("world"); + sv.update(); + } + + // Attempt to update rr entry. + { + Storage storage = mReplicated.storageFor(StorableVersioned.class); + StorableVersioned sv = storage.prepare(); + sv.setID(5); + + int failCount = 0; + + for (int retryCount = 3;;) { + try { + sv.load(); + sv.setValue("ciao"); + sv.update(); + break; + } catch (OptimisticLockException e) { + failCount++; + retryCount = e.backoff(e, retryCount, 1000); + } + } + + assertTrue(failCount > 0); + } + + // Verify new version in rr. + { + Storage storage = mReplicated.storageFor(StorableVersioned.class); + StorableVersioned sv = storage.prepare(); + sv.setID(5); + sv.load(); + assertEquals(3, sv.getVersion()); + assertEquals("ciao", sv.getValue()); + } + } +} -- cgit v1.2.3