ninja add unit test for 7882
Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/71ccc87a Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/71ccc87a Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/71ccc87a Branch: refs/heads/cassandra-2.1 Commit: 71ccc87a79d161cdf5cf219a276f5bd7313f9c80 Parents: 7cd1102 Author: Benedict Elliott Smith <bened...@apache.org> Authored: Wed Jan 28 22:50:13 2015 +0000 Committer: Benedict Elliott Smith <bened...@apache.org> Committed: Wed Jan 28 22:50:13 2015 +0000 ---------------------------------------------------------------------- .../utils/memory/NativeAllocatorTest.java | 111 +++++++++++++++++++ 1 file changed, 111 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cassandra/blob/71ccc87a/test/unit/org/apache/cassandra/utils/memory/NativeAllocatorTest.java ---------------------------------------------------------------------- diff --git a/test/unit/org/apache/cassandra/utils/memory/NativeAllocatorTest.java b/test/unit/org/apache/cassandra/utils/memory/NativeAllocatorTest.java new file mode 100644 index 0000000..83d6c0c --- /dev/null +++ b/test/unit/org/apache/cassandra/utils/memory/NativeAllocatorTest.java @@ -0,0 +1,111 @@ +/* +* 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.cassandra.utils.memory; + +import java.util.concurrent.*; +import java.util.concurrent.atomic.AtomicReference; + +import com.google.common.util.concurrent.Uninterruptibles; + +import org.junit.Test; + +import junit.framework.Assert; +import org.apache.cassandra.utils.concurrent.OpOrder; + +public class NativeAllocatorTest +{ + + @Test + public void testBookKeeping() throws ExecutionException, InterruptedException + { + { + final ScheduledExecutorService exec = Executors.newScheduledThreadPool(2); + final OpOrder order = new OpOrder(); + final OpOrder.Group group = order.start(); + final CountDownLatch canClean = new CountDownLatch(1); + final CountDownLatch isClean = new CountDownLatch(1); + final AtomicReference<NativeAllocator> allocatorRef = new AtomicReference<>(); + final AtomicReference<OpOrder.Barrier> barrier = new AtomicReference<>(); + final NativeAllocator allocator = new NativeAllocator(new NativePool(1, 100, 0.75f, new Runnable() + { + public void run() + { + try + { + canClean.await(); + } + catch (InterruptedException e) + { + throw new AssertionError(); + } + if (isClean.getCount() > 0) + { + allocatorRef.get().offHeap().release(80); + isClean.countDown(); + } + } + })); + allocatorRef.set(allocator); + final Runnable markBlocking = new Runnable() + { + + public void run() + { + barrier.set(order.newBarrier()); + barrier.get().issue(); + barrier.get().markBlocking(); + } + }; + final Runnable run = new Runnable() + { + public void run() + { + // allocate normal, check accounted and not cleaned + allocator.allocate(10, group); + Assert.assertEquals(10, allocator.offHeap().owns()); + Uninterruptibles.sleepUninterruptibly(10L, TimeUnit.MILLISECONDS); + Assert.assertEquals(1, isClean.getCount()); + + // allocate above watermark, check cleaned + allocator.allocate(70, group); + Assert.assertEquals(80, allocator.offHeap().owns()); + canClean.countDown(); + try + { + isClean.await(10L, TimeUnit.MILLISECONDS); + } + catch (InterruptedException e) + { + throw new AssertionError(); + } + Assert.assertEquals(0, isClean.getCount()); + Assert.assertEquals(0, allocator.offHeap().owns()); + + // allocate above limit, check we block until "marked blocking" + exec.schedule(markBlocking, 10L, TimeUnit.MILLISECONDS); + allocator.allocate(110, group); + Assert.assertNotNull(barrier.get()); + Assert.assertEquals(110, allocator.offHeap().owns()); + } + }; + exec.submit(run).get(); + } + } + +}