Skip to content

Commit

Permalink
add Periodic test
Browse files Browse the repository at this point in the history
  • Loading branch information
wycccccc committed Apr 26, 2022
1 parent 825e443 commit e809356
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions app/src/test/java/org/astraea/cost/PeriodicTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.astraea.cost;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class PeriodicTest extends Periodic<Map<Integer, Double>> {
private double brokerValue = 0.0;

@Test
void testTryUpdate() {
var broker1 = tryUpdate(this::testMap);
Assertions.assertEquals(broker1.get(0), 0.0);
var broker2 = tryUpdate(this::testMap);
Assertions.assertEquals(broker2.get(0), 0.0);
sleep(1);
broker2 = tryUpdate(this::testMap);
Assertions.assertEquals(broker2.get(0), 1.0);
}

Map<Integer, Double> testMap() {
var broker = new HashMap<Integer, Double>();
broker.put(0, brokerValue);
brokerValue++;
return broker;
}

private static void sleep(int seconds) {
try {
TimeUnit.SECONDS.sleep(seconds);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}

0 comments on commit e809356

Please sign in to comment.