diff options
Diffstat (limited to 'sandbox/old/contrib/persistence/store.journal/src/test/java/org')
13 files changed, 1105 insertions, 0 deletions
diff --git a/sandbox/old/contrib/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/JournalStoreExpireTestCase.java b/sandbox/old/contrib/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/JournalStoreExpireTestCase.java new file mode 100644 index 0000000000..4e23b3ca72 --- /dev/null +++ b/sandbox/old/contrib/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/JournalStoreExpireTestCase.java @@ -0,0 +1,89 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.tuscany.persistence.store.journal; + +import java.util.UUID; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.net.URI; + +import org.apache.tuscany.spi.component.SCAObject; +import org.apache.tuscany.spi.event.RuntimeEventListener; +import org.apache.tuscany.spi.services.store.StoreExpirationEvent; +import org.apache.tuscany.spi.services.store.StoreMonitor; + +import junit.framework.TestCase; +import org.easymock.IAnswer; +import org.easymock.classextension.EasyMock; +import org.objectweb.howl.log.LogEventListener; + +/** + * @version $Rev$ $Date$ + */ +public class JournalStoreExpireTestCase extends TestCase { + @SuppressWarnings({"FieldCanBeLocal"}) + private JournalStore store; + private SCAObject owner; + + public void testNotifyOnExpire() throws Exception { + final CountDownLatch latch = new CountDownLatch(1); + StoreMonitor monitor = EasyMock.createMock(StoreMonitor.class); + Journal journal = EasyMock.createNiceMock(Journal.class); + journal.setLogEventListener(EasyMock.isA(LogEventListener.class)); + EasyMock.replay(journal); + + RuntimeEventListener listener = org.easymock.EasyMock.createMock(RuntimeEventListener.class); + listener.onEvent(org.easymock.EasyMock.isA(StoreExpirationEvent.class)); + EasyMock.expectLastCall().andStubAnswer(new IAnswer<Object>() { + public Object answer() throws Throwable { + latch.countDown(); + return null; + } + }); + org.easymock.EasyMock.replay(listener); + + store = new JournalStore(monitor, journal) { + }; + store.addListener(listener); + store.init(); + final String id = UUID.randomUUID().toString(); + store.insertRecord(owner, id, "test", 1); + if (!latch.await(10000, TimeUnit.MILLISECONDS)) { + // failed to notify listener + fail(); + } + store.destroy(); + EasyMock.verify(listener); + } + + + protected void setUp() throws Exception { + super.setUp(); + TestUtils.cleanupLog(); + owner = EasyMock.createMock(SCAObject.class); + URI uri = URI.create("foo"); + EasyMock.expect(owner.getUri()).andReturn(uri).atLeastOnce(); + EasyMock.replay(owner); + } + + protected void tearDown() throws Exception { + super.tearDown(); + TestUtils.cleanupLog(); + } +} diff --git a/sandbox/old/contrib/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/JournalStoreInsertTestCase.java b/sandbox/old/contrib/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/JournalStoreInsertTestCase.java new file mode 100644 index 0000000000..a2cab56134 --- /dev/null +++ b/sandbox/old/contrib/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/JournalStoreInsertTestCase.java @@ -0,0 +1,134 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.tuscany.persistence.store.journal; + +import java.util.UUID; +import java.net.URI; + +import org.apache.tuscany.spi.component.SCAObject; +import org.apache.tuscany.spi.services.store.Store; +import org.apache.tuscany.spi.services.store.StoreMonitor; + +import junit.framework.TestCase; +import org.easymock.IAnswer; +import org.easymock.classextension.EasyMock; +import org.objectweb.howl.log.LogEventListener; + +/** + * @version $Rev$ $Date$ + */ +public class JournalStoreInsertTestCase extends TestCase { + @SuppressWarnings({"FieldCanBeLocal"}) + private JournalStore store; + private SCAObject owner; + + public void testOrderedShutdown() throws Exception { + StoreMonitor monitor = EasyMock.createMock(StoreMonitor.class); + Journal journal = EasyMock.createMock(Journal.class); + journal.setLogEventListener(EasyMock.isA(LogEventListener.class)); + journal.open(); + journal.close(); + EasyMock.replay(journal); + store = new JournalStore(monitor, journal) { + }; + store.init(); + store.destroy(); + EasyMock.verify(journal); + } + + public void testInsertRecord() throws Exception { + StoreMonitor monitor = EasyMock.createMock(StoreMonitor.class); + Journal journal = EasyMock.createMock(Journal.class); + journal.setLogEventListener(EasyMock.isA(LogEventListener.class)); + journal.open(); + final String id = UUID.randomUUID().toString(); + EasyMock.expect(journal.writeHeader(EasyMock.isA(byte[].class), EasyMock.eq(false))) + .andStubAnswer(new IAnswer<Long>() { + public Long answer() throws Throwable { + Header header = new Header(); + header.setFields(new byte[][]{(byte[]) EasyMock.getCurrentArguments()[0]}); + // deserialize the message to test the format is correct + SerializationHelper.deserializeHeader(header); + assertTrue("Operation not INSERT", Header.INSERT == header.getOperation()); + assertTrue("Expiration incorrect", Store.NEVER == header.getExpiration()); + assertTrue("Least significant id incorrect", + id.equals(header.getId())); + assertTrue("Most significant id incorrect", + id.equals(header.getId())); + assertTrue("Records incorrect", 1 == header.getNumBlocks()); + assertTrue("Owner id incorrect", "foo".equals(header.getOwnerId())); + return 1L; + } + }); + EasyMock.expect(journal.writeBlock(EasyMock.isA(byte[].class), EasyMock.isA(byte[].class), EasyMock.eq(true))) + .andStubAnswer(new IAnswer<Long>() { + public Long answer() throws Throwable { + byte[] payload = (byte[]) EasyMock.getCurrentArguments()[0]; + assertTrue("Block data incorrect", "test".equals(SerializationHelper.deserialize(payload, null))); + return 1L; + } + }); + journal.close(); + EasyMock.replay(journal); + store = new JournalStore(monitor, journal) { + }; + store.init(); + store.insertRecord(owner, id, "test", Store.NEVER); + store.destroy(); + EasyMock.verify(journal); + } + + /** + * Verifies that a written record will be cached. This is verified by the fact that long-term storage is never + * accessed. + */ + public void testInsertRecordCache() throws Exception { + StoreMonitor monitor = EasyMock.createMock(StoreMonitor.class); + Journal journal = EasyMock.createMock(Journal.class); + journal.setLogEventListener(EasyMock.isA(LogEventListener.class)); + journal.open(); + final String id = UUID.randomUUID().toString(); + EasyMock.expect(journal.writeHeader(EasyMock.isA(byte[].class), EasyMock.eq(false))).andReturn(1L); + EasyMock.expect(journal.writeBlock(EasyMock.isA(byte[].class), EasyMock.isA(byte[].class), EasyMock.eq(true))) + .andReturn(1L); + journal.close(); + EasyMock.replay(journal); + store = new JournalStore(monitor, journal) { + }; + store.init(); + store.insertRecord(owner, id, "test", Store.NEVER); + assertEquals("test", store.readRecord(owner, id)); + store.destroy(); + EasyMock.verify(journal); + } + + protected void setUp() throws Exception { + super.setUp(); + TestUtils.cleanupLog(); + owner = EasyMock.createMock(SCAObject.class); + URI uri = URI.create("foo"); + EasyMock.expect(owner.getUri()).andReturn(uri).atLeastOnce(); + EasyMock.replay(owner); + } + + protected void tearDown() throws Exception { + super.tearDown(); + TestUtils.cleanupLog(); + } +} diff --git a/sandbox/old/contrib/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/JournalStoreOverflowTestCase.java b/sandbox/old/contrib/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/JournalStoreOverflowTestCase.java new file mode 100644 index 0000000000..61a77869c1 --- /dev/null +++ b/sandbox/old/contrib/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/JournalStoreOverflowTestCase.java @@ -0,0 +1,91 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.tuscany.persistence.store.journal; + +import java.util.UUID; +import java.net.URI; + +import org.apache.tuscany.spi.component.SCAObject; +import org.apache.tuscany.spi.services.store.StoreMonitor; + +import junit.framework.TestCase; +import org.easymock.classextension.EasyMock; + +/** + * @version $Rev$ $Date$ + */ +public class JournalStoreOverflowTestCase extends TestCase { + @SuppressWarnings({"FieldCanBeLocal"}) + private JournalStore store; + private SCAObject owner; + + /** + * Validates records are moved forward during a log overflow + * + * @throws Exception + */ + public void testOverflow() throws Exception { + StoreMonitor monitor = EasyMock.createMock(StoreMonitor.class); + store = new JournalStore(monitor); + store.setMaxBlocksPerFile(3); + store.init(); + long expire = System.currentTimeMillis() + 200; + store.insertRecord(owner, UUID.randomUUID().toString(), "test", expire); + store.insertRecord(owner, UUID.randomUUID().toString(), "test", expire); + store.insertRecord(owner, UUID.randomUUID().toString(), "test", expire); + store.insertRecord(owner, UUID.randomUUID().toString(), "test", expire); + store.insertRecord(owner, UUID.randomUUID().toString(), "test", expire); // + store.insertRecord(owner, UUID.randomUUID().toString(), "test", expire); + Thread.sleep(250); + store.insertRecord(owner, UUID.randomUUID().toString(), "test", expire + 20000); + store.insertRecord(owner, UUID.randomUUID().toString(), "test", expire + 20000); + store.destroy(); + } + + + public void testOverflowAtInsertHeader() throws Exception { + + } + + public void testOverflowAtUpdateHeader() throws Exception { + + } + + public void testOverflowAtDeleteHeader() throws Exception { + + } + + public void testOverflowAtBlock() throws Exception { + + } + + protected void setUp() throws Exception { + super.setUp(); + TestUtils.cleanupLog(); + owner = EasyMock.createMock(SCAObject.class); + EasyMock.expect(owner.getUri()).andReturn(URI.create("foo")).atLeastOnce(); + EasyMock.replay(owner); + } + + protected void tearDown() throws Exception { + super.tearDown(); + TestUtils.cleanupLog(); + } + +} diff --git a/sandbox/old/contrib/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/JournalTestCase.java b/sandbox/old/contrib/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/JournalTestCase.java new file mode 100644 index 0000000000..2df83a0e74 --- /dev/null +++ b/sandbox/old/contrib/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/JournalTestCase.java @@ -0,0 +1,78 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.tuscany.persistence.store.journal; + +import java.io.ByteArrayOutputStream; +import java.util.UUID; + +import static org.apache.tuscany.spi.services.store.Store.NEVER; + +import junit.framework.TestCase; +import static org.apache.tuscany.persistence.store.journal.SerializationHelper.serializeHeader; +import static org.apache.tuscany.persistence.store.journal.SerializationHelper.serializeRecordId; +import org.objectweb.howl.log.Configuration; +import org.objectweb.howl.log.LogRecord; + +/** + * @version $Rev$ $Date$ + */ +public class JournalTestCase extends TestCase { + private Journal journal; + + public void testWriteHeader() throws Exception { + String id = UUID.randomUUID().toString(); + long key = journal.writeHeader(serializeHeader(Header.INSERT, 10, "foo/bar", id, NEVER), false); + LogRecord record = journal.get(null, key); + Header header = new Header(); + header.setFields(record.getFields()); + SerializationHelper.deserializeHeader(header); + assertTrue(record.type == Journal.HEADER); + assertEquals(Header.INSERT, header.getOperation()); + assertEquals(10, header.getNumBlocks()); + assertEquals("foo/bar", header.getOwnerId()); + assertEquals(id, header.getId()); + assertEquals(NEVER, header.getExpiration()); + } + + public void testWriteRecord() throws Exception { + byte[] recordId = serializeRecordId("foo", UUID.randomUUID().toString()); + long key = journal.writeBlock("this is a test".getBytes(), recordId, true); + LogRecord record = journal.get(null, key); + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + assertEquals(record.type, Journal.RECORD); + stream.write(record.getFields()[1]); + JournalRecord jrecord = new JournalRecord(stream.toByteArray()); + assertEquals("this is a test", new String(jrecord.getData())); + } + + protected void setUp() throws Exception { + super.setUp(); + TestUtils.cleanupLog(); + Configuration config = new Configuration(); + config.setLogFileDir("../stores"); + journal = new Journal(config); + journal.open(); + } + + protected void tearDown() throws Exception { + super.tearDown(); + journal.close(); + TestUtils.cleanupLog(); + } +} diff --git a/sandbox/old/contrib/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/MockSCAExternalizable.java b/sandbox/old/contrib/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/MockSCAExternalizable.java new file mode 100644 index 0000000000..62e8a50635 --- /dev/null +++ b/sandbox/old/contrib/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/MockSCAExternalizable.java @@ -0,0 +1,57 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.tuscany.persistence.store.journal; + +import java.io.Externalizable; +import java.io.IOException; +import java.io.ObjectInput; +import java.io.ObjectOutput; + +import org.apache.tuscany.spi.component.ReactivationException; +import org.apache.tuscany.spi.component.SCAExternalizable; +import org.apache.tuscany.spi.component.WorkContext; + +/** + * @version $Rev$ $Date$ + */ +public class MockSCAExternalizable implements Externalizable, SCAExternalizable { + private WorkContext context; + private boolean reactivated; + + public void writeExternal(ObjectOutput out) throws IOException { + + } + + public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { + + } + + public boolean isReactivated() { + return reactivated; + } + + public void setWorkContext(WorkContext context) { + this.context = context; + } + + public void reactivate() throws ReactivationException { + assert context != null : "WorkContext not properly set"; + reactivated = true; + } +} diff --git a/sandbox/old/contrib/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/RecordKeyTestCase.java b/sandbox/old/contrib/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/RecordKeyTestCase.java new file mode 100644 index 0000000000..ddd69a1f54 --- /dev/null +++ b/sandbox/old/contrib/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/RecordKeyTestCase.java @@ -0,0 +1,77 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.tuscany.persistence.store.journal; + +import java.net.URI; + +import org.apache.tuscany.spi.component.SCAObject; + +import junit.framework.TestCase; +import org.easymock.EasyMock; + +/** + * @version $Rev$ $Date$ + */ +public class RecordKeyTestCase extends TestCase { + + public void testEquals() throws Exception { + String id = "bar"; + URI uri = URI.create("foo"); + SCAObject owner1 = EasyMock.createMock(SCAObject.class); + EasyMock.expect(owner1.getUri()).andReturn(uri); + EasyMock.replay(owner1); + SCAObject owner2 = EasyMock.createMock(SCAObject.class); + EasyMock.expect(owner2.getUri()).andReturn(uri); + EasyMock.replay(owner2); + + RecordKey key1 = new RecordKey(id, owner1); + RecordKey key2 = new RecordKey(id, owner2); + assertEquals(key1, key2); + } + + public void testNotEqualsId() throws Exception { + String id = "bar"; + SCAObject owner1 = EasyMock.createMock(SCAObject.class); + URI uri = URI.create("foo"); + EasyMock.expect(owner1.getUri()).andReturn(uri); + EasyMock.replay(owner1); + SCAObject owner2 = EasyMock.createMock(SCAObject.class); + EasyMock.expect(owner2.getUri()).andReturn(uri); + EasyMock.replay(owner2); + RecordKey key1 = new RecordKey(id, owner1); + RecordKey key2 = new RecordKey("baz", owner2); + assertFalse(key1.equals(key2)); + } + + public void testNotEqualsOwner() throws Exception { + String id = "bar"; + URI fooUri = URI.create("foo"); + SCAObject owner1 = EasyMock.createMock(SCAObject.class); + EasyMock.expect(owner1.getUri()).andReturn(fooUri); + EasyMock.replay(owner1); + SCAObject owner2 = EasyMock.createMock(SCAObject.class); + URI barUri = URI.create("bar"); + EasyMock.expect(owner2.getUri()).andReturn(barUri); + EasyMock.replay(owner2); + RecordKey key1 = new RecordKey(id, owner1); + RecordKey key2 = new RecordKey(id, owner2); + assertFalse(key1.equals(key2)); + } + +} diff --git a/sandbox/old/contrib/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/SerializationHelperTestCase.java b/sandbox/old/contrib/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/SerializationHelperTestCase.java new file mode 100644 index 0000000000..160e4c7a23 --- /dev/null +++ b/sandbox/old/contrib/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/SerializationHelperTestCase.java @@ -0,0 +1,93 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.tuscany.persistence.store.journal; + +import java.util.List; +import java.util.UUID; + +import org.apache.tuscany.spi.component.WorkContext; +import static org.apache.tuscany.spi.services.store.Store.NEVER; + +import junit.framework.TestCase; +import static org.apache.tuscany.persistence.store.journal.SerializationHelper.deserialize; +import org.easymock.EasyMock; + +/** + * @version $Rev$ $Date$ + */ +public class SerializationHelperTestCase extends TestCase { + + public void testTwoEvenChunks() throws Exception { + byte[] bytes = "this is a test".getBytes(); + List<byte[]> chunks = SerializationHelper.partition(bytes, 7); + assertEquals(2, chunks.size()); + assertEquals("this is", new String(chunks.get(0))); + assertEquals(" a test", new String(chunks.get(1))); + } + + public void testUnevenChunks() throws Exception { + byte[] bytes = "this is a test123".getBytes(); + List<byte[]> chunks = SerializationHelper.partition(bytes, 7); + assertEquals(3, chunks.size()); + assertEquals("this is", new String(chunks.get(0))); + assertEquals(" a test", new String(chunks.get(1))); + assertEquals("123", new String(chunks.get(2))); + } + + public void testChunkSizeGreater() throws Exception { + byte[] bytes = "this is a test".getBytes(); + List<byte[]> chunks = SerializationHelper.partition(bytes, 512); + assertEquals(1, chunks.size()); + byte[] chunk = chunks.get(0); + assertEquals(14, chunk.length); + assertEquals("this is a test", new String(chunk)); + } + + public void testSerializeDeserializeNonSCAExternalizable() throws Exception { + byte[] bytes = SerializationHelper.serialize("foo"); + assertEquals("foo", deserialize(bytes, null)); + } + + public void testSerializeDeserializeSCAExternalizable() throws Exception { + byte[] bytes = SerializationHelper.serialize(new MockSCAExternalizable()); + WorkContext context = EasyMock.createNiceMock(WorkContext.class); + MockSCAExternalizable externalized = (MockSCAExternalizable) deserialize(bytes, context); + assertTrue(externalized.isReactivated()); + } + + public void testDeserializeHeader() throws Exception { + String id = UUID.randomUUID().toString(); + byte[] bytes = SerializationHelper.serializeHeader(Header.INSERT, 2, "foo", id, NEVER); + Header header = SerializationHelper.deserializeHeader(new MockHeader(bytes)); + assertEquals(Header.INSERT, header.getOperation()); + assertEquals(2, header.getNumBlocks()); + assertEquals("foo", header.getOwnerId()); + assertEquals(id, header.getId()); + assertEquals(NEVER, header.getExpiration()); + } + + private class MockHeader extends Header { + public MockHeader(byte[] bytes) { + super(); + fields = new byte[][]{bytes}; + } + } + + +} diff --git a/sandbox/old/contrib/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/TestUtils.java b/sandbox/old/contrib/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/TestUtils.java new file mode 100644 index 0000000000..5fb2ebeb82 --- /dev/null +++ b/sandbox/old/contrib/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/TestUtils.java @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.tuscany.persistence.store.journal; + +import java.io.File; + +/** + * JournalThroughputTest case utilities + * + * @version $Rev$ $Date$ + */ +public final class TestUtils { + + private TestUtils() { + + } + + /** + * Removes log files from disk + */ + public static void cleanupLog() { + File dir = new File("../stores"); + if (!dir.exists()) { + return; + } + for (File file : dir.listFiles()) { + file.delete(); + } + dir.delete(); + } +} diff --git a/sandbox/old/contrib/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/performance/Foo.java b/sandbox/old/contrib/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/performance/Foo.java new file mode 100644 index 0000000000..d9c7aee660 --- /dev/null +++ b/sandbox/old/contrib/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/performance/Foo.java @@ -0,0 +1,52 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.tuscany.persistence.store.journal.performance; + +import java.io.Serializable; + +/** + * @version $Rev$ $Date$ + */ +@SuppressWarnings({"SerializableHasSerializationMethods"}) +public class Foo implements Serializable { + private static final long serialVersionUID = -1003664515513709814L; + protected String baz; + protected int bar; + + public Foo(String baz, int bar) { + this.baz = baz; + this.bar = bar; + } + + public String getBaz() { + return baz; + } + + public void setBaz(String baz) { + this.baz = baz; + } + + public int getBar() { + return bar; + } + + public void setBar(int bar) { + this.bar = bar; + } +} diff --git a/sandbox/old/contrib/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/performance/JournalStoreThroughputTest.java b/sandbox/old/contrib/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/performance/JournalStoreThroughputTest.java new file mode 100644 index 0000000000..eeb566f182 --- /dev/null +++ b/sandbox/old/contrib/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/performance/JournalStoreThroughputTest.java @@ -0,0 +1,117 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.tuscany.persistence.store.journal.performance; + +import java.io.IOException; +import java.util.UUID; +import java.util.concurrent.BrokenBarrierException; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.CyclicBarrier; +import java.util.concurrent.TimeUnit; + +import org.apache.tuscany.spi.component.SCAObject; +import org.apache.tuscany.spi.services.store.StoreWriteException; + +import org.apache.tuscany.persistence.store.journal.JournalShutdownException; +import org.apache.tuscany.persistence.store.journal.JournalStore; +import static org.apache.tuscany.persistence.store.journal.SerializationHelper.serialize; +import static org.apache.tuscany.persistence.store.journal.SerializationHelper.serializeRecordId; +import org.apache.tuscany.persistence.store.journal.TestUtils; + +/** + * Runs a basic throughput tests on JournalStore operations + * <p/> + * TODO this should be integrated with a Maven itest-based performance harness + * + * @version $Rev$ $Date$ + */ +public class JournalStoreThroughputTest { + private static final int SIZE = 1000; + private CyclicBarrier barrier; + private JournalStore store; + private long now; + private SCAObject owner = new MockSCAObject(); + private String id = UUID.randomUUID().toString(); + private CountDownLatch latch = new CountDownLatch(1); + private long expire = System.currentTimeMillis() + 10000; + private Foo object = new Foo("this is a test", 1); + + public static void main(String[] args) throws Exception { + JournalStoreThroughputTest test = new JournalStoreThroughputTest(); + test.testAppend(); + test.latch.await(5000, TimeUnit.MILLISECONDS); + } + + public void testAppend() throws Exception { + TestUtils.cleanupLog(); + store = new JournalStore(new MockMonitor()); + store.init(); + final Thread[] threads = new Thread[SIZE]; + barrier = new CyclicBarrier(SIZE, new Runnable() { + public void run() { + try { + System.out.println("-----------------------------------------------------"); + System.out.println("JournalStore.append()"); + byte[] idBytes = serializeRecordId(owner.getUri().toString(), id); + byte[] bytes = serialize(object); + System.out.println("Approx record size :" + (bytes.length + idBytes.length)); + System.out.println("Total threads :" + barrier.getNumberWaiting()); + System.out.println("Forced writes :" + barrier.getNumberWaiting()); + System.out.println("Time:" + (System.currentTimeMillis() - now)); + store.destroy(); + latch.countDown(); + } catch (JournalShutdownException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + }); + for (int i = 0; i < SIZE; i++) { + threads[i] = new Thread(new InsertWorker(true)); + } + now = System.currentTimeMillis(); + for (int i = 0; i < SIZE; i++) { + threads[i].start(); + } + } + + private class InsertWorker implements Runnable { + boolean forced; + + public InsertWorker(boolean forced) { + this.forced = forced; + } + + public void run() { + try { + store.insertRecord(owner, id, object, expire); + barrier.await(); + } catch (StoreWriteException e) { + e.printStackTrace(); + } catch (BrokenBarrierException e) { + e.printStackTrace(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + + +} diff --git a/sandbox/old/contrib/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/performance/JournalThroughputTest.java b/sandbox/old/contrib/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/performance/JournalThroughputTest.java new file mode 100644 index 0000000000..7a3e8fa308 --- /dev/null +++ b/sandbox/old/contrib/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/performance/JournalThroughputTest.java @@ -0,0 +1,146 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.tuscany.persistence.store.journal.performance; + +import java.io.IOException; +import java.util.UUID; +import java.util.concurrent.BrokenBarrierException; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.CyclicBarrier; +import java.util.concurrent.TimeUnit; + +import org.apache.tuscany.spi.services.store.StoreWriteException; + +import org.apache.tuscany.persistence.store.journal.Journal; +import static org.apache.tuscany.persistence.store.journal.SerializationHelper.serializeRecordId; +import org.apache.tuscany.persistence.store.journal.TestUtils; + +/** + * Runs a basic throughput tests on Journal operations + * <p/> + * TODO this should be integrated with a Maven itest-based performance harness + * + * @version $Rev$ $Date$ + */ +public class JournalThroughputTest { + private static final int SIZE = 1000; + private CyclicBarrier barrier; + private Journal journal; + private byte[] bytes; + private byte[] recordId; + private long now; + private CountDownLatch latch = new CountDownLatch(1); + + public static void main(String[] args) throws Exception { + JournalThroughputTest test = new JournalThroughputTest(); + test.testForcedWrites(); + test.latch.await(5000, TimeUnit.MILLISECONDS); + test.testNonForcedWrites(); + } + + public void testForcedWrites() throws Exception { + TestUtils.cleanupLog(); + journal = new Journal(); + journal.open(); + recordId = serializeRecordId("foo", UUID.randomUUID().toString()); + bytes = "this is a test".getBytes(); + final Thread[] threads = new Thread[SIZE]; + barrier = new CyclicBarrier(SIZE, new Runnable() { + public void run() { + System.out.println("-----------------------------------------------------"); + System.out.println("Journal.writeBlock() using forced writes"); + System.out.println("Approx record size :" + (recordId.length + bytes.length)); + System.out.println("Total threads :" + barrier.getNumberWaiting()); + System.out.println("Forced writes :" + barrier.getNumberWaiting()); + System.out.println("Time:" + (System.currentTimeMillis() - now)); + try { + journal.close(); + latch.countDown(); + } catch (IOException e) { + e.printStackTrace(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + }); + for (int i = 0; i < SIZE; i++) { + threads[i] = new Thread(new Worker(true)); + } + now = System.currentTimeMillis(); + for (int i = 0; i < SIZE; i++) { + threads[i].start(); + } + } + + public void testNonForcedWrites() throws Exception { + TestUtils.cleanupLog(); + journal = new Journal(); + journal.open(); + recordId = serializeRecordId("foo", UUID.randomUUID().toString()); + bytes = "this is a test".getBytes(); + final Thread[] threads = new Thread[SIZE]; + barrier = new CyclicBarrier(SIZE, new Runnable() { + public void run() { + System.out.println("-----------------------------------------------------"); + System.out.println("Journal.writeBlock() using non-forced writes"); + System.out.println("Approx record size :" + (recordId.length + bytes.length)); + System.out.println("Total threads :" + barrier.getNumberWaiting()); + System.out.println("Forced writes :" + barrier.getNumberWaiting()); + System.out.println("Time:" + (System.currentTimeMillis() - now)); + System.out.println("-----------------------------------------------------"); + try { + journal.close(); + } catch (IOException e) { + e.printStackTrace(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + }); + for (int i = 0; i < SIZE; i++) { + threads[i] = new Thread(new Worker(false)); + } + now = System.currentTimeMillis(); + for (int i = 0; i < SIZE; i++) { + threads[i].start(); + } + } + + private class Worker implements Runnable { + boolean forced; + + public Worker(boolean forced) { + this.forced = forced; + } + + public void run() { + try { + journal.writeBlock(bytes, recordId, forced); + barrier.await(); + } catch (StoreWriteException e) { + e.printStackTrace(); + } catch (BrokenBarrierException e) { + e.printStackTrace(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + +} diff --git a/sandbox/old/contrib/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/performance/MockMonitor.java b/sandbox/old/contrib/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/performance/MockMonitor.java new file mode 100644 index 0000000000..a62630ebe0 --- /dev/null +++ b/sandbox/old/contrib/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/performance/MockMonitor.java @@ -0,0 +1,58 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.tuscany.persistence.store.journal.performance; + +import org.apache.tuscany.spi.services.store.StoreMonitor; + +import org.apache.tuscany.api.annotation.LogLevel; + +/** + * @version $Rev$ $Date$ + */ +public class MockMonitor implements StoreMonitor { + @LogLevel("DEBUG") + public void start(String msg) { + + } + + @LogLevel("DEBUG") + public void stop(String msg) { + + } + + @LogLevel("DEBUG") + public void beginRecover() { + + } + + @LogLevel("DEBUG") + public void endRecover() { + + } + + @LogLevel("DEBUG") + public void recover(Object recordId) { + + } + + @LogLevel("ERROR") + public void error(Throwable e) { + + } +} diff --git a/sandbox/old/contrib/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/performance/MockSCAObject.java b/sandbox/old/contrib/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/performance/MockSCAObject.java new file mode 100644 index 0000000000..e9ecdbab28 --- /dev/null +++ b/sandbox/old/contrib/persistence/store.journal/src/test/java/org/apache/tuscany/persistence/store/journal/performance/MockSCAObject.java @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.tuscany.persistence.store.journal.performance; + +import java.net.URI; + +import org.apache.tuscany.spi.CoreRuntimeException; +import org.apache.tuscany.spi.component.SCAObject; +import org.apache.tuscany.spi.event.Event; +import org.apache.tuscany.spi.event.EventFilter; +import org.apache.tuscany.spi.event.RuntimeEventListener; + +/** + * @version $Rev$ $Date$ + */ +public class MockSCAObject implements SCAObject { + + public URI getUri() { + return null; + } + + public void publish(Event object) { + + } + + public void addListener(RuntimeEventListener listener) { + + } + + public void addListener(EventFilter filter, RuntimeEventListener listener) { + + } + + public void removeListener(RuntimeEventListener listener) { + + } + + public int getLifecycleState() { + return 0; + } + + public void start() throws CoreRuntimeException { + + } + + public void stop() throws CoreRuntimeException { + + } + +} |