Skip to content

Commit

Permalink
[Unit Tests] Basic tests 1 (#326)
Browse files Browse the repository at this point in the history
* Extract test deps

* Rate limiter test

* Timer test

* Decimal format test

* List partition test

* Licenses
  • Loading branch information
teabot authored Sep 28, 2022
1 parent a1b5bc3 commit 929c4c3
Show file tree
Hide file tree
Showing 9 changed files with 212 additions and 8 deletions.
12 changes: 12 additions & 0 deletions benchmark-framework/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,18 @@
<artifactId>jetty-util</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,19 @@
package io.openmessaging.benchmark.utils;

import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

public class Timer {
private final long startTime;
private final Supplier<Long> nanoClock;

Timer(Supplier<Long> nanoClock) {
this.nanoClock = nanoClock;
startTime = this.nanoClock.get();
}

public Timer() {
startTime = System.nanoTime();
this(() -> System.nanoTime());
}

public double elapsedMillis() {
Expand All @@ -31,7 +38,7 @@ public double elapsedSeconds() {
}

private double elapsed(TimeUnit unit) {
long now = System.nanoTime();
long now = nanoClock.get();
return (now - startTime) / (double) unit.toNanos(1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
*/
package io.openmessaging.benchmark.utils;

import java.time.Clock;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLongFieldUpdater;
import java.util.concurrent.locks.LockSupport;
import java.util.function.Supplier;

/**
* Provides a next operation time for rate limited operation streams.<br>
Expand All @@ -32,8 +34,9 @@ public final class UniformRateLimiter {
private volatile long virtualTime;
private final double opsPerSec;
private final long intervalNs;
private final Supplier<Long> nanoClock;

public UniformRateLimiter(final double opsPerSec) {
UniformRateLimiter(final double opsPerSec, Supplier<Long> nanoClock) {
if (Double.isNaN(opsPerSec) || Double.isInfinite(opsPerSec)) {
throw new IllegalArgumentException("opsPerSec cannot be Nan or Infinite");
}
Expand All @@ -42,6 +45,11 @@ public UniformRateLimiter(final double opsPerSec) {
}
this.opsPerSec = opsPerSec;
intervalNs = Math.round(ONE_SEC_IN_NS / opsPerSec);
this.nanoClock = nanoClock;

}
public UniformRateLimiter(final double opsPerSec) {
this(opsPerSec, () -> System.nanoTime());
}

public double getOpsPerSec() {
Expand All @@ -56,7 +64,7 @@ public long acquire() {
final long currOpIndex = V_TIME_UPDATER.getAndIncrement(this);
long start = this.start;
if (start == Long.MIN_VALUE) {
start = System.nanoTime();
start = nanoClock.get();
if (!START_UPDATER.compareAndSet(this, Long.MIN_VALUE, start)) {
start = this.start;
assert start != Long.MIN_VALUE;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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 io.openmessaging.benchmark.utils;

import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.List;
import org.junit.jupiter.api.Test;

class ListPartitionTest {

@Test
void partitionList() {
List<Integer> list = asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
List<List<Integer>> lists = ListPartition.partitionList(list, 3);
assertThat(lists).satisfies(s -> {
assertThat(s).hasSize(3);
assertThat(s.get(0)).isEqualTo(asList(1, 4, 7, 10));
assertThat(s.get(1)).isEqualTo(asList(2, 5, 8));
assertThat(s.get(2)).isEqualTo(asList(3, 6, 9));
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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 io.openmessaging.benchmark.utils;

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

class PaddingDecimalFormatTest {

@Test
void format() {
PaddingDecimalFormat format = new PaddingDecimalFormat("0.0", 7);
assertThat(format.format(1L)).isEqualTo(" 1.0");
assertThat(format.format(1000L)).isEqualTo(" 1000.0");
assertThat(format.format(10000000L)).isEqualTo("10000000.0");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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 io.openmessaging.benchmark.utils;

import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.function.Supplier;
import org.junit.jupiter.api.Test;

class TimerTest {

@Test
void elapsedMillis() {
Supplier<Long> mockClock = mock(Supplier.class);
when(mockClock.get()).thenReturn(MILLISECONDS.toNanos(1), MILLISECONDS.toNanos(3));
Timer timer = new Timer(mockClock);
assertThat(timer.elapsedMillis()).isEqualTo(2.0d);
}

@Test
void elapsedSeconds() {
Supplier<Long> mockClock = mock(Supplier.class);
when(mockClock.get()).thenReturn(SECONDS.toNanos(1), SECONDS.toNanos(3));
Timer timer = new Timer(mockClock);
assertThat(timer.elapsedSeconds()).isEqualTo(2.0d);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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 io.openmessaging.benchmark.utils;

import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.function.Supplier;
import org.junit.jupiter.api.Test;

class UniformRateLimiterTest {

@Test
void getOpsPerSec() {
assertThat(new UniformRateLimiter(1000).getOpsPerSec()).isEqualTo(1000.0d);
}

@Test
void getIntervalNs() {
assertThat(new UniformRateLimiter(1000).getIntervalNs()).isEqualTo(SECONDS.toNanos(1) / 1000);
}

@Test
void acquireSlowSingleThread() {
Supplier<Long> mockClock = mock(Supplier.class);
when(mockClock.get()).thenReturn(SECONDS.toNanos(2));
UniformRateLimiter rateLimiter = new UniformRateLimiter(1000, mockClock);
assertThat(rateLimiter.acquire()).isEqualTo(2000000000L);
assertThat(rateLimiter.acquire()).isEqualTo(2001000000L);
assertThat(rateLimiter.acquire()).isEqualTo(2002000000L);
}

@Test
void uninterruptibleSleepNs() {
long start = System.nanoTime();
long expectedEnd = start + MILLISECONDS.toNanos(100);
UniformRateLimiter.uninterruptibleSleepNs(expectedEnd);
long end = System.nanoTime();
assertThat(end).isGreaterThan(expectedEnd);
}

@Test
void cinitExceptions() {
assertThatCode(() -> new UniformRateLimiter(Double.NaN)).isInstanceOf(IllegalArgumentException.class);
assertThatCode(() -> new UniformRateLimiter(1.0d / 0.0d)).isInstanceOf(IllegalArgumentException.class);
assertThatCode(() -> new UniformRateLimiter(-0.1)).isInstanceOf(IllegalArgumentException.class);
assertThatCode(() -> new UniformRateLimiter(0.0)).isInstanceOf(IllegalArgumentException.class);
}
}
4 changes: 0 additions & 4 deletions driver-kafka/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,10 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>4.7.0</version>
<scope>test</scope>
</dependency>
</dependencies>

Expand Down
14 changes: 14 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@
<lombok.version>1.18.24</lombok.version>
<jackson.version>2.13.2</jackson.version>
<jcommander.version>1.48</jcommander.version>
<junit.jupiter.version>5.9.0</junit.jupiter.version>
<mockito.junit.jupiter.version>4.8.0</mockito.junit.jupiter.version>
<netty.version>4.1.65.Final</netty.version>
<slf4j.version>1.7.36</slf4j.version>

Expand Down Expand Up @@ -383,6 +385,18 @@
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>${mockito.junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down

0 comments on commit 929c4c3

Please sign in to comment.