diff --git a/src/main/java/org/elasticsearch/common/recycler/QueueRecycler.java b/src/main/java/org/elasticsearch/common/recycler/QueueRecycler.java index 57d0edcb782..a9a05f1d3a6 100644 --- a/src/main/java/org/elasticsearch/common/recycler/QueueRecycler.java +++ b/src/main/java/org/elasticsearch/common/recycler/QueueRecycler.java @@ -28,13 +28,13 @@ import java.util.Queue; */ public class QueueRecycler extends Recycler { - final Queue> queue; + final Queue queue; public QueueRecycler(C c) { - this(c, ConcurrentCollections.>newQueue()); + this(c, ConcurrentCollections.newQueue()); } - public QueueRecycler(C c, Queue> queue) { + public QueueRecycler(C c, Queue queue) { super(c); this.queue = queue; } @@ -46,11 +46,11 @@ public class QueueRecycler extends Recycler { @Override public V obtain(int sizing) { - V v = queue.poll(); + T v = queue.poll(); if (v == null) { - v = new QV(c.newInstance(sizing)); + v = c.newInstance(sizing); } - return v; + return new QV(v); } class QV implements Recycler.V { @@ -77,7 +77,7 @@ public class QueueRecycler extends Recycler { throw new ElasticSearchIllegalStateException("recycler entry already released..."); } c.clear(value); - queue.offer(this); + queue.offer(value); value = null; } } diff --git a/src/test/java/org/elasticsearch/common/recycler/AbstractRecyclerTests.java b/src/test/java/org/elasticsearch/common/recycler/AbstractRecyclerTests.java new file mode 100644 index 00000000000..3c841d881ee --- /dev/null +++ b/src/test/java/org/elasticsearch/common/recycler/AbstractRecyclerTests.java @@ -0,0 +1,86 @@ +/* + * Licensed to ElasticSearch and Shay Banon under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. ElasticSearch 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.elasticsearch.common.recycler; + +import org.elasticsearch.ElasticSearchIllegalStateException; +import org.elasticsearch.test.ElasticsearchTestCase; + +import java.util.Arrays; + +public abstract class AbstractRecyclerTests extends ElasticsearchTestCase { + + protected static final Recycler.C RECYCLER_C = new Recycler.C() { + + @Override + public byte[] newInstance(int sizing) { + return new byte[10]; + } + + @Override + public void clear(byte[] value) { + Arrays.fill(value, (byte) 0); + } + + }; + + protected abstract Recycler newRecycler(); + + public void testReuse() { + Recycler r = newRecycler(); + Recycler.V o = r.obtain(); + final byte[] b1 = o.v(); + o.release(); + o = r.obtain(); + final byte[] b2 = o.v(); + if (o.isRecycled()) { + assertSame(b1, b2); + } + o.release(); + } + + public void testClear() { + Recycler r = newRecycler(); + Recycler.V o = r.obtain(); + getRandom().nextBytes(o.v()); + o.release(); + o = r.obtain(); + for (int i = 0; i < o.v().length; ++i) { + assertEquals(0, o.v()[i]); + } + o.release(); + } + + public void testDoubleRelease() { + final Recycler r = newRecycler(); + final Recycler.V v1 = r.obtain(); + v1.release(); + try { + v1.release(); + } catch (ElasticSearchIllegalStateException e) { + // impl has protection against double release: ok + return; + } + // otherwise ensure that the impl may not be returned twice + final Recycler.V v2 = r.obtain(); + final Recycler.V v3 = r.obtain(); + assertNotSame(v2.v(), v3.v()); + } + +} diff --git a/src/test/java/org/elasticsearch/common/recycler/NoneRecyclerTests.java b/src/test/java/org/elasticsearch/common/recycler/NoneRecyclerTests.java new file mode 100644 index 00000000000..141c59cf971 --- /dev/null +++ b/src/test/java/org/elasticsearch/common/recycler/NoneRecyclerTests.java @@ -0,0 +1,29 @@ +/* + * Licensed to ElasticSearch and Shay Banon under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. ElasticSearch 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.elasticsearch.common.recycler; + +public class NoneRecyclerTests extends AbstractRecyclerTests { + + @Override + protected Recycler newRecycler() { + return new NoneRecycler(RECYCLER_C); + } + +} diff --git a/src/test/java/org/elasticsearch/common/recycler/QueueRecyclerTests.java b/src/test/java/org/elasticsearch/common/recycler/QueueRecyclerTests.java new file mode 100644 index 00000000000..ea1960051f0 --- /dev/null +++ b/src/test/java/org/elasticsearch/common/recycler/QueueRecyclerTests.java @@ -0,0 +1,29 @@ +/* + * Licensed to ElasticSearch and Shay Banon under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. ElasticSearch 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.elasticsearch.common.recycler; + +public class QueueRecyclerTests extends AbstractRecyclerTests { + + @Override + protected Recycler newRecycler() { + return new QueueRecycler(RECYCLER_C); + } + +} diff --git a/src/test/java/org/elasticsearch/common/recycler/SoftThreadLocalRecyclerTests.java b/src/test/java/org/elasticsearch/common/recycler/SoftThreadLocalRecyclerTests.java new file mode 100644 index 00000000000..c99e790b635 --- /dev/null +++ b/src/test/java/org/elasticsearch/common/recycler/SoftThreadLocalRecyclerTests.java @@ -0,0 +1,29 @@ +/* + * Licensed to ElasticSearch and Shay Banon under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. ElasticSearch 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.elasticsearch.common.recycler; + +public class SoftThreadLocalRecyclerTests extends AbstractRecyclerTests { + + @Override + protected Recycler newRecycler() { + return new SoftThreadLocalRecycler(RECYCLER_C, randomIntBetween(5, 10)); + } + +} diff --git a/src/test/java/org/elasticsearch/common/recycler/ThreadLocalRecyclerTests.java b/src/test/java/org/elasticsearch/common/recycler/ThreadLocalRecyclerTests.java new file mode 100644 index 00000000000..6a88320c97e --- /dev/null +++ b/src/test/java/org/elasticsearch/common/recycler/ThreadLocalRecyclerTests.java @@ -0,0 +1,29 @@ +/* + * Licensed to ElasticSearch and Shay Banon under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. ElasticSearch 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.elasticsearch.common.recycler; + +public class ThreadLocalRecyclerTests extends AbstractRecyclerTests { + + @Override + protected Recycler newRecycler() { + return new ThreadLocalRecycler(RECYCLER_C, randomIntBetween(5, 10)); + } + +}