Skip to content

Commit

Permalink
GEODE-6504: Instant class used. (#7779)
Browse files Browse the repository at this point in the history
* Using instant class instead of nanos.
* Nanos is not updated that often hence not proper resolution.
  • Loading branch information
nabarunnag authored Jun 10, 2022
1 parent b3716d9 commit 2e7a4fa
Showing 1 changed file with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
*/
package org.apache.geode.cache;

import static java.lang.System.nanoTime;
import static java.util.concurrent.TimeUnit.NANOSECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.apache.geode.cache.ExpirationAction.DESTROY;
import static org.apache.geode.cache.ExpirationAction.INVALIDATE;
Expand All @@ -31,6 +29,9 @@
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.verify;

import java.time.Duration;
import java.time.Instant;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -70,10 +71,10 @@ public void setUp() {
}

@Test
public void increaseRegionTtl() throws Exception {
public void increaseRegionTtl() {
int firstTtlSeconds = 3;
int secondTtlSeconds = 8;
long startNanos = nanoTime();
Instant startInstant = Instant.now();

RegionFactory<String, String> regionFactory = cache.createRegionFactory(LOCAL);
regionFactory.setRegionTimeToLive(new ExpirationAttributes(firstTtlSeconds, DESTROY));
Expand All @@ -84,15 +85,16 @@ public void increaseRegionTtl() throws Exception {
.setRegionTimeToLive(new ExpirationAttributes(secondTtlSeconds, DESTROY));

await().until(region::isDestroyed);
assertThat(NANOSECONDS.toSeconds(nanoTime() - startNanos))
.isGreaterThanOrEqualTo(secondTtlSeconds);
Instant endInstant = Instant.now();
assertThat(Duration.between(startInstant, endInstant))
.isGreaterThanOrEqualTo(Duration.ofSeconds(secondTtlSeconds));
}

@Test
public void decreaseRegionTtl() throws Exception {
public void decreaseRegionTtl() {
int firstTtlSeconds = 5;
int secondTtlSeconds = 1;
long startNanos = nanoTime();
Instant startInstant = Instant.now();

RegionFactory<String, String> regionFactory = cache.createRegionFactory(LOCAL);
regionFactory.setRegionTimeToLive(new ExpirationAttributes(firstTtlSeconds, DESTROY));
Expand All @@ -103,11 +105,13 @@ public void decreaseRegionTtl() throws Exception {
.setRegionTimeToLive(new ExpirationAttributes(secondTtlSeconds, DESTROY));

await().untilAsserted(() -> assertThat(region.isDestroyed()).isTrue());
assertThat(NANOSECONDS.toSeconds(nanoTime() - startNanos)).isLessThan(firstTtlSeconds);
Instant endInstant = Instant.now();
assertThat(Duration.between(startInstant, endInstant))
.isLessThan(Duration.ofSeconds(firstTtlSeconds));
}

@Test
public void regionTtlWithIdleMock() throws Exception {
public void regionTtlWithIdleMock() {
int ttlSeconds = 5;
int idleSeconds = 1;

Expand Down

0 comments on commit 2e7a4fa

Please sign in to comment.