Skip to content

Commit

Permalink
fix reactor#1326 Fix ClassCastException in OneQueue/ZeroQueue + Java9…
Browse files Browse the repository at this point in the history
… build

This commit fixes a ClassCastException in the `toArray(T[])` methods of
`OneQueue` and `ZeroQueue`. Under Java 9, the compiler would make the
error in `OneQueue` explicit.

It also reactivate mandatory builds of the project under JDK9 in Travis
and amends a test that fails in Java 9 due to additional stacktrace
lines.

Reviewed in reactor#1327
  • Loading branch information
szpak authored and simonbasle committed Aug 23, 2018
1 parent 36b8ccf commit f7c7e11
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 20 deletions.
5 changes: 0 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ jdk:
- oraclejdk8
- oraclejdk9

matrix:
fast_finish: true
allow_failures:
- jdk: oraclejdk9

sudo: required

script: ./travis-build.sh
Expand Down
25 changes: 14 additions & 11 deletions reactor-core/src/main/java/reactor/util/concurrent/Queues.java
Original file line number Diff line number Diff line change
Expand Up @@ -334,17 +334,21 @@ public Object[] toArray() {
@Override
@SuppressWarnings("unchecked")
public <T1> T1[] toArray(T1[] a) {
if (a.length > 0) {
a[0] = (T1)get();
if (a.length > 1) {
a[1] = null;
}
return a;
int size = size();
if (a.length < size) {
a = (T1[]) java.lang.reflect.Array.newInstance(
a.getClass().getComponentType(), size);
}
if (size == 1) {
a[0] = (T1) get();
}
return (T1[])toArray();
if (a.length > size) {
a[size] = null;
}
return a;
}
/** */
private static final long serialVersionUID = -6079491923525372331L;

private static final long serialVersionUID = -6079491923525372331L;
}

static final class ZeroQueue<T> implements Queue<T>, Serializable {
Expand Down Expand Up @@ -441,9 +445,8 @@ public Object[] toArray() {
public <T1> T1[] toArray(T1[] a) {
if (a.length > 0) {
a[0] = null;
return a;
}
return (T1[])toArray();
return a;
}

private static final long serialVersionUID = -8876883675795156827L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,10 @@ public void parallelFluxCheckpointDescriptionAndForceStack() {

String debugStack = sw.toString();

assertThat(debugStack).endsWith("Assembly trace from producer [reactor.core.publisher.ParallelSource], described as [descriptionCorrelation1234] :\n"
assertThat(debugStack).contains("Assembly trace from producer [reactor.core.publisher.ParallelSource], described as [descriptionCorrelation1234] :\n"
+ "\treactor.core.publisher.ParallelFlux.checkpoint(ParallelFlux.java:215)\n"
+ "\treactor.core.publisher.FluxOnAssemblyTest.parallelFluxCheckpointDescriptionAndForceStack(FluxOnAssemblyTest.java:225)\n"
+ "Error has been observed by the following operator(s):\n"
+ "\treactor.core.publisher.FluxOnAssemblyTest.parallelFluxCheckpointDescriptionAndForceStack(FluxOnAssemblyTest.java:225)\n");
assertThat(debugStack).endsWith("Error has been observed by the following operator(s):\n"
+ "\t|_\tParallelFlux.checkpoint ⇢ reactor.core.publisher.FluxOnAssemblyTest.parallelFluxCheckpointDescriptionAndForceStack(FluxOnAssemblyTest.java:225)\n\n");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright (c) 2011-2018 Pivotal Software Inc, All Rights Reserved.
*
* Licensed 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 reactor.util.concurrent;

import org.junit.Test;

import java.util.Queue;

import static org.assertj.core.api.Assertions.assertThat;

//https://github.com/reactor/reactor-core/pull/1326
public class QueuesOneQueueTest {

private static final int TEST_ELEMENT = 2;

@Test
public void emptyOneQueueShouldConvertToArrayWhenPassedZeroLengthArray() {
assertThat(emptyOneQueue().toArray(new Integer[0])).isEmpty();
}

@Test
public void oneQueueWithOneElementShouldConvertToArrayWhenPassedZeroLengthArray() {
assertThat(oneQueueWithTestElement(TEST_ELEMENT).toArray(new Integer[0]))
.containsExactly(TEST_ELEMENT);
}

@Test
public void emptyOneQueueShouldConvertToArrayAndPutNullMarkerAndReuseInputArrayOnWhenPassedOneLengthArray() {
Queue<Integer> q = emptyOneQueue();
//and
Integer[] passedArray = new Integer[1];
//when
Integer[] convertedArray = q.toArray(passedArray);
//then
assertThat(convertedArray)
.containsExactly((Integer)null)
.isSameAs(passedArray);
}

@Test
public void oneQueueWithOneElementShouldConvertToArrayAndReuseInputArrayWhenPassedOneLengthArray() {
Queue<Integer> q = oneQueueWithTestElement(TEST_ELEMENT);
//and
Integer[] passedArray = new Integer[1];
//when
Integer[] convertedArray = q.toArray(passedArray);
//then
assertThat(convertedArray)
.containsExactly(TEST_ELEMENT)
.isSameAs(passedArray);
}

@Test
public void emptyOneQueueShouldConvertToArrayAndPutNullMarkerAndReuseInputArrayWhenPassedLargerArray() {
//given
Queue<Integer> q = emptyOneQueue();
//and
Integer[] passedArray = {1, 2, 3};
//when
Integer[] convertedArray = q.toArray(passedArray);
//then
assertThat(convertedArray)
.hasSize(3)
.startsWith(null, 2, 3)
.isSameAs(passedArray);
}

@Test
public void oneQueueWithOneElementShouldConvertToArrayAndPutNullMarkerAndReuseInputArrayWhenPassedLargerArray() {
Queue<Integer> q = oneQueueWithTestElement(TEST_ELEMENT);
//and
Integer[] passedArray = {1, 2, 3};
//given
Integer[] convertedArray = q.toArray(passedArray);
//then
assertThat(convertedArray)
.hasSize(3)
.startsWith(TEST_ELEMENT, null, 3)
.isSameAs(passedArray);
}

private Queue<Integer> emptyOneQueue() {
return Queues.<Integer>one().get();
}

private Queue<Integer> oneQueueWithTestElement(int element) {
Queue<Integer> q = Queues.<Integer>one().get();
q.add(element);
return q;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,15 @@ public void zeroQueueOperations() {
assertThat(q.toArray()).as("toArray").isEmpty();
assertThat(q.toArray(new Integer[0])).as("toArray(empty)").isEmpty();

Integer[] array = new Integer[] { -1, -2, -3 };
Integer[] array = new Integer[]{-1, -2, -3};
assertThat(q.toArray(array)).as("toArray(pre-filled)").containsExactly(null, -2, -3);
}

@Test //https://github.com/reactor/reactor-core/issues/1326
public void toArrayOnZeroQueueShouldNotFailAlsoOnJava9() {
Queue<Integer> emptyQueue = Queues.<Integer>empty().get();

assertThat(emptyQueue.toArray(new Integer[0])).as("toArray(empty)").isEmpty();
}

}

0 comments on commit f7c7e11

Please sign in to comment.