Skip to content

Commit

Permalink
[FLINK-26298][rpc][tests] Migrate tests to JUnit5
Browse files Browse the repository at this point in the history
  • Loading branch information
zentol authored Apr 28, 2022
1 parent daaf3be commit c9b5e2f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,93 +18,87 @@

package org.apache.flink.runtime.concurrent;

import org.apache.flink.util.TestLoggerExtension;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import javax.annotation.Nonnull;

import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.assertj.core.api.Assertions.assertThat;

/** Unit tests for {@link ScheduledFutureAdapter}. */
@ExtendWith(TestLoggerExtension.class)
public class ScheduledFutureAdapterTest {
class ScheduledFutureAdapterTest {

private ScheduledFutureAdapter<Integer> objectUnderTest;
private TestFuture innerDelegate;

@BeforeEach
public void before() {
void before() {
this.innerDelegate = new TestFuture();
this.objectUnderTest =
new ScheduledFutureAdapter<>(innerDelegate, 4200000321L, TimeUnit.NANOSECONDS);
}

@Test
public void testForwardedMethods() throws Exception {

assertEquals((Integer) 4711, objectUnderTest.get());
assertEquals(1, innerDelegate.getGetInvocationCount());
void testForwardedMethods() throws Exception {
assertThat(objectUnderTest.get()).isEqualTo(4711);
assertThat(innerDelegate.getGetInvocationCount()).isEqualTo(1);

assertEquals((Integer) 4711, objectUnderTest.get(42L, TimeUnit.SECONDS));
assertEquals(1, innerDelegate.getGetTimeoutInvocationCount());
assertThat(objectUnderTest.get(42L, TimeUnit.SECONDS)).isEqualTo(4711);
assertThat(innerDelegate.getGetTimeoutInvocationCount()).isEqualTo(1);

assertEquals(innerDelegate.isCancelExpected(), objectUnderTest.cancel(true));
assertEquals(1, innerDelegate.getCancelInvocationCount());
assertThat(objectUnderTest.cancel(true)).isEqualTo(innerDelegate.isCancelExpected());
assertThat(innerDelegate.getCancelInvocationCount()).isEqualTo(1);

innerDelegate.setCancelResult(!innerDelegate.isCancelExpected());
assertEquals(innerDelegate.isCancelExpected(), objectUnderTest.cancel(true));
assertEquals(2, innerDelegate.getCancelInvocationCount());
assertThat(objectUnderTest.cancel(true)).isEqualTo(innerDelegate.isCancelExpected());
assertThat(innerDelegate.getCancelInvocationCount()).isEqualTo(2);

assertEquals(innerDelegate.isCancelledExpected(), objectUnderTest.isCancelled());
assertEquals(1, innerDelegate.getIsCancelledInvocationCount());
assertThat(objectUnderTest.isCancelled()).isEqualTo(innerDelegate.isCancelledExpected());
assertThat(innerDelegate.getIsCancelledInvocationCount()).isEqualTo(1);

innerDelegate.setIsCancelledResult(!innerDelegate.isCancelledExpected());
assertEquals(innerDelegate.isCancelledExpected(), objectUnderTest.isCancelled());
assertEquals(2, innerDelegate.getIsCancelledInvocationCount());
assertThat(objectUnderTest.isCancelled()).isEqualTo(innerDelegate.isCancelledExpected());
assertThat(innerDelegate.getIsCancelledInvocationCount()).isEqualTo(2);

assertEquals(innerDelegate.isDoneExpected(), objectUnderTest.isDone());
assertEquals(1, innerDelegate.getIsDoneInvocationCount());
assertThat(objectUnderTest.isDone()).isEqualTo(innerDelegate.isDoneExpected());
assertThat(innerDelegate.getIsDoneInvocationCount()).isEqualTo(1);

innerDelegate.setIsDoneExpected(!innerDelegate.isDoneExpected());
assertEquals(innerDelegate.isDoneExpected(), objectUnderTest.isDone());
assertEquals(2, innerDelegate.getIsDoneInvocationCount());
assertThat(objectUnderTest.isDone()).isEqualTo(innerDelegate.isDoneExpected());
assertThat(innerDelegate.getIsDoneInvocationCount()).isEqualTo(2);
}

@Test
public void testCompareToEqualsHashCode() {
void testCompareToEqualsHashCode() {

assertEquals(0, objectUnderTest.compareTo(objectUnderTest));
assertEquals(objectUnderTest, objectUnderTest);
assertThat(objectUnderTest.compareTo(objectUnderTest)).isEqualTo(0);
assertThat((Object) objectUnderTest).isEqualTo(objectUnderTest);

ScheduledFutureAdapter<?> other =
getDeepCopyWithAdjustedTime(0L, objectUnderTest.getTieBreakerUid());

assertEquals(0, objectUnderTest.compareTo(other));
assertEquals(0, other.compareTo(objectUnderTest));
assertEquals(objectUnderTest, other);
assertEquals(objectUnderTest.hashCode(), other.hashCode());
assertThat(objectUnderTest.compareTo(other)).isEqualTo(0);
assertThat(other.compareTo(objectUnderTest)).isEqualTo(0);
assertThat((Object) other).isEqualTo(objectUnderTest);
assertThat(other.hashCode()).isEqualTo(objectUnderTest.hashCode());

other = getDeepCopyWithAdjustedTime(0L, objectUnderTest.getTieBreakerUid() + 1L);
assertEquals(-1, Integer.signum(objectUnderTest.compareTo(other)));
assertEquals(+1, Integer.signum(other.compareTo(objectUnderTest)));
assertNotEquals(objectUnderTest, other);
assertThat(Integer.signum(objectUnderTest.compareTo(other))).isEqualTo(-1);
assertThat(Integer.signum(other.compareTo(objectUnderTest))).isEqualTo(+1);
assertThat((Object) objectUnderTest).isNotEqualTo(other);

other = getDeepCopyWithAdjustedTime(+1L, objectUnderTest.getTieBreakerUid());
assertEquals(-1, Integer.signum(objectUnderTest.compareTo(other)));
assertEquals(+1, Integer.signum(other.compareTo(objectUnderTest)));
assertNotEquals(objectUnderTest, other);
assertThat(Integer.signum(objectUnderTest.compareTo(other))).isEqualTo(-1);
assertThat(Integer.signum(other.compareTo(objectUnderTest))).isEqualTo(+1);
assertThat((Object) objectUnderTest).isNotEqualTo(other);

other = getDeepCopyWithAdjustedTime(-1L, objectUnderTest.getTieBreakerUid());
assertEquals(+1, Integer.signum(objectUnderTest.compareTo(other)));
assertEquals(-1, Integer.signum(other.compareTo(objectUnderTest)));
assertNotEquals(objectUnderTest, other);
assertThat(Integer.signum(objectUnderTest.compareTo(other))).isEqualTo(+1);
assertThat(Integer.signum(other.compareTo(objectUnderTest))).isEqualTo(-1);
assertThat((Object) objectUnderTest).isNotEqualTo(other);
}

private ScheduledFutureAdapter<Integer> getDeepCopyWithAdjustedTime(long nanoAdjust, long uid) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 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.

org.apache.flink.util.TestLoggerExtension

0 comments on commit c9b5e2f

Please sign in to comment.