jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michael Dürig <mdue...@apache.org>
Subject Re: svn commit: r1337248 - in /jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/concurrent: ./ Concurrent.java ConcurrentBlobTest.java ConcurrentCacheTest.java ConcurrentTest.java
Date Mon, 14 May 2012 12:59:56 GMT


On 11.5.12 17:13, stefan@apache.org wrote:
> Author: stefan
> Date: Fri May 11 16:13:31 2012
> New Revision: 1337248
>
> URL: http://svn.apache.org/viewvc?rev=1337248&view=rev
> Log:
> copied some mk-related tests in oak-core to oak-mk
>
> Added:
>      jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/concurrent/
>      jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/concurrent/Concurrent.java
>      jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/concurrent/ConcurrentBlobTest.java
>      jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/concurrent/ConcurrentCacheTest.java
>      jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/concurrent/ConcurrentTest.java

Is there a reason for keeping the duplicates in oak-core or can we 
remove them?

Michael

>
> Added: jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/concurrent/Concurrent.java
> URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/concurrent/Concurrent.java?rev=1337248&view=auto
> ==============================================================================
> --- jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/concurrent/Concurrent.java
(added)
> +++ jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/concurrent/Concurrent.java
Fri May 11 16:13:31 2012
> @@ -0,0 +1,113 @@
> +/*
> + * 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.jackrabbit.mk.concurrent;
> +
> +import org.h2.util.Profiler;
> +
> +import java.util.ArrayList;
> +import java.util.concurrent.atomic.AtomicBoolean;
> +import java.util.concurrent.atomic.AtomicInteger;
> +import java.util.concurrent.atomic.AtomicReference;
> +
> +/**
> + * A concurrency test tool.
> + */
> +public class Concurrent {
> +
> +    private static final boolean PROFILE = false;
> +
> +    private Concurrent() {
> +    }
> +
> +    /**
> +     * Run a task concurrently in 2 threads for 1 second.
> +     *
> +     * @param message the message
> +     * @param task the task
> +     * @throws Exception the first exception that is thrown (if any)
> +     */
> +    public static void run(String message, Task task) throws Exception {
> +        run(message, task, 2, 1000);
> +    }
> +
> +    public static void run(String message, final Task task, int threadCount, int millis)
throws Exception {
> +        final AtomicBoolean stopped = new AtomicBoolean();
> +        final AtomicReference<Throwable>  exception = new AtomicReference<Throwable>();
> +        ArrayList<Thread>  threads = new ArrayList<Thread>();
> +        final AtomicInteger counter = new AtomicInteger();
> +        Profiler p = new Profiler();
> +        if (PROFILE) {
> +            p.interval = 1;
> +            p.startCollecting();
> +        }
> +        for (int i = 0; i<  threadCount; i++) {
> +            Thread t = new Thread("Task " + i) {
> +                @Override
> +                public void run() {
> +                    while (!stopped.get()) {
> +                        try {
> +                            task.call();
> +                            counter.incrementAndGet();
> +                        } catch (Error e) {
> +                            exception.set(e);
> +                            stopped.set(true);
> +                        } catch (Exception e) {
> +                            exception.set(e);
> +                            stopped.set(true);
> +                        }
> +                    }
> +                }
> +            };
> +            if (threadCount == 1) {
> +                long stop = millis + System.currentTimeMillis();
> +                while (System.currentTimeMillis()<  stop) {
> +                    task.call();
> +                    counter.incrementAndGet();
> +                }
> +                millis = 0;
> +            } else {
> +                t.start();
> +                threads.add(t);
> +            }
> +        }
> +        Throwable e = null;
> +        while (e == null&&  millis>  0) {
> +            Thread.sleep(10);
> +            millis -= 10;
> +            e = exception.get();
> +        }
> +        stopped.set(true);
> +        for (Thread t : threads) {
> +            t.join();
> +        }
> +        if (e != null) {
> +            if (e instanceof Exception) {
> +                throw (Exception) e;
> +            }
> +            throw (Error) e;
> +        }
> +        if (PROFILE) {
> +            System.out.println(p.getTop(5));
> +        }
> +
> +    }
> +
> +    public interface Task {
> +        void call() throws Exception;
> +    }
> +
> +}
>
> Added: jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/concurrent/ConcurrentBlobTest.java
> URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/concurrent/ConcurrentBlobTest.java?rev=1337248&view=auto
> ==============================================================================
> --- jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/concurrent/ConcurrentBlobTest.java
(added)
> +++ jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/concurrent/ConcurrentBlobTest.java
Fri May 11 16:13:31 2012
> @@ -0,0 +1,64 @@
> +/*
> + * 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.jackrabbit.mk.concurrent;
> +
> +import junit.framework.Assert;
> +import org.apache.jackrabbit.mk.blobs.MemoryBlobStore;
> +import org.apache.jackrabbit.mk.util.IOUtils;
> +import org.junit.Before;
> +import org.junit.Test;
> +
> +import java.io.ByteArrayInputStream;
> +import java.io.ByteArrayOutputStream;
> +import java.util.Arrays;
> +import java.util.concurrent.atomic.AtomicInteger;
> +
> +/**
> + * Test concurrent access to the blob store.
> + */
> +public class ConcurrentBlobTest {
> +
> +    static final byte[] EMPTY = new byte[50];
> +
> +    MemoryBlobStore store = new MemoryBlobStore();
> +
> +    @Before
> +    public void setUp() throws Exception {
> +        store.setBlockSizeMin(50);
> +    }
> +
> +    @Test
> +    public void test() throws Exception {
> +        final AtomicInteger id = new AtomicInteger();
> +        Concurrent.run("blob", new Concurrent.Task() {
> +            @Override
> +            public void call() throws Exception {
> +                int i = id.getAndIncrement();
> +                ByteArrayOutputStream out = new ByteArrayOutputStream();
> +                IOUtils.writeLong(out, i);
> +                out.write(EMPTY);
> +                byte[] data = out.toByteArray();
> +                String id = store.writeBlob(new ByteArrayInputStream(data));
> +                Assert.assertEquals(58, store.getBlobLength(id));
> +                byte[] test = out.toByteArray();
> +                Assert.assertEquals(8, store.readBlob(id, 0, test, 0, 8));
> +                Assert.assertTrue(Arrays.equals(data, test));
> +            }
> +        });
> +    }
> +
> +}
>
> Added: jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/concurrent/ConcurrentCacheTest.java
> URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/concurrent/ConcurrentCacheTest.java?rev=1337248&view=auto
> ==============================================================================
> --- jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/concurrent/ConcurrentCacheTest.java
(added)
> +++ jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/concurrent/ConcurrentCacheTest.java
Fri May 11 16:13:31 2012
> @@ -0,0 +1,74 @@
> +/*
> + * 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.jackrabbit.mk.concurrent;
> +
> +import junit.framework.Assert;
> +import org.apache.jackrabbit.mk.util.Cache;
> +import org.junit.Test;
> +
> +import java.util.concurrent.atomic.AtomicInteger;
> +
> +/**
> + * Tests the cache implementation.
> + */
> +public class ConcurrentCacheTest implements Cache.Backend<Integer, ConcurrentCacheTest.Data>
 {
> +
> +    Cache<Integer, Data>  cache = Cache.newInstance(this, 5);
> +    AtomicInteger counter = new AtomicInteger();
> +    volatile int value;
> +
> +    @Test
> +    public void test() throws Exception {
> +        Concurrent.run("cache", new Concurrent.Task() {
> +            @Override
> +            public void call() throws Exception {
> +                int k = value++ % 10;
> +                Data v = cache.get(k);
> +                Assert.assertEquals(k, v.value);
> +            }
> +        });
> +    }
> +
> +    @Override
> +    public Data load(Integer key) {
> +        int start = counter.get();
> +        try {
> +            Thread.sleep(1);
> +        } catch (InterruptedException e) {
> +            // ignore
> +        }
> +        if (counter.getAndIncrement() != start) {
> +            throw new AssertionError("Concurrent load");
> +        }
> +        return new Data(key);
> +    }
> +
> +    static class Data implements Cache.Value {
> +
> +        int value;
> +
> +        Data(int value) {
> +            this.value = value;
> +        }
> +
> +        @Override
> +        public int getMemory() {
> +            return 1;
> +        }
> +
> +    }
> +}
>
> Added: jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/concurrent/ConcurrentTest.java
> URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/concurrent/ConcurrentTest.java?rev=1337248&view=auto
> ==============================================================================
> --- jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/concurrent/ConcurrentTest.java
(added)
> +++ jackrabbit/oak/trunk/oak-mk/src/test/java/org/apache/jackrabbit/mk/concurrent/ConcurrentTest.java
Fri May 11 16:13:31 2012
> @@ -0,0 +1,55 @@
> +/*
> + * 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.jackrabbit.mk.concurrent;
> +
> +import junit.framework.Assert;
> +import org.apache.jackrabbit.mk.api.MicroKernel;
> +import org.apache.jackrabbit.mk.core.MicroKernelImpl;
> +import org.junit.Test;
> +
> +import java.util.concurrent.atomic.AtomicInteger;
> +
> +/**
> + * Test concurrent access to nodes, the journal, and revision.
> + */
> +public class ConcurrentTest {
> +
> +    final MicroKernel mk = new MicroKernelImpl();
> +
> +    @Test
> +    public void test() throws Exception {
> +        final AtomicInteger id = new AtomicInteger();
> +        Concurrent.run("MicroKernel", new Concurrent.Task() {
> +            @Override
> +            public void call() throws Exception {
> +                long start = System.currentTimeMillis();
> +                String rev = mk.getHeadRevision();
> +                int i = id.getAndIncrement();
> +                String newRev = mk.commit("/", "+\"" + i + "\":{\"x\": " + i + "}",
rev, "");
> +                Assert.assertTrue(!newRev.equals(rev));
> +                mk.getJournal(rev, newRev, null);
> +                mk.getRevisionHistory(start, 100);
> +                mk.getNodes("/" + i, newRev);
> +                mk.getNodes("/" + i, newRev, 0, 0, 0, null);
> +                Assert.assertFalse(mk.nodeExists("/" + i, rev));
> +                Assert.assertTrue(mk.nodeExists("/" + i, newRev));
> +                rev = newRev;
> +                newRev = mk.commit("/", "-\"" + i + "\"", rev, "");
> +                Assert.assertTrue(mk.nodeExists("/" + i, rev));
> +                Assert.assertFalse(mk.nodeExists("/" + i, newRev));
> +            }
> +        });
> +    }
> +
> +}
>
>

Mime
View raw message