Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #216: Size of SpscGrowableArrayQueue can exceeds max capacity #218

Merged
merged 1 commit into from Sep 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -114,7 +114,7 @@ final boolean offerColdPath(
long currConsumerIndex = lvConsumerIndex();
// use lookAheadStep to store the consumer distance from final buffer
this.lookAheadStep = -(index - currConsumerIndex);
producerBufferLimit = currConsumerIndex + maxCapacity - 1;
producerBufferLimit = currConsumerIndex + maxCapacity;
}
else
{
Expand Down
Expand Up @@ -106,7 +106,7 @@ final boolean offerColdPath(final AtomicReferenceArray<E> buffer, final long mas
long currConsumerIndex = lvConsumerIndex();
// use lookAheadStep to store the consumer distance from final buffer
this.lookAheadStep = -(index - currConsumerIndex);
producerBufferLimit = currConsumerIndex + maxCapacity - 1;
producerBufferLimit = currConsumerIndex + maxCapacity;
} else {
producerBufferLimit = index + producerMask - 1;
adjustLookAheadStep(newCapacity);
Expand Down
Expand Up @@ -2,13 +2,17 @@

import org.jctools.queues.spec.ConcurrentQueueSpec;
import org.jctools.queues.spec.Ordering;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Queue;

import static org.hamcrest.Matchers.is;

@RunWith(Parameterized.class)
public class QueueSanityTestSpscGrowable extends QueueSanityTest
{
Expand All @@ -27,4 +31,30 @@ public static Collection<Object[]> parameters()
return list;
}

@Test
public void testSizeNeverExceedCapacity()
{
final SpscGrowableArrayQueue<Integer> q = new SpscGrowableArrayQueue<>(8, 16);
final Integer v = 0;
final int capacity = q.capacity();
for (int i = 0; i < capacity; i++)
{
Assert.assertTrue(q.offer(v));
}
Assert.assertFalse(q.offer(v));
Assert.assertThat(q.size(), is(capacity));
for (int i = 0; i < 6; i++)
{
Assert.assertEquals(v, q.poll());
}
//the consumer is left in the chunk previous the last and biggest one
Assert.assertThat(q.size(), is(capacity - 6));
for (int i = 0; i < 6; i++)
{
q.offer(v);
}
Assert.assertThat(q.size(), is(capacity));
Assert.assertFalse(q.offer(v));
}

}