Skip to content

Commit

Permalink
Merge pull request #33 from alexist/feat/lasting-time-min-deadline
Browse files Browse the repository at this point in the history
feat(incr): decrease lasting time threshold deadline from 1000ms to 100ms
  • Loading branch information
grrolland authored Nov 24, 2023
2 parents 70fda5d + ba4d62a commit 28139ab
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 5 deletions.
7 changes: 3 additions & 4 deletions src/main/java/io/github/grrolland/hcshm/ShmValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,15 @@ public String getValue() {
/**
* Get le lasting time for the value
* <p>
* When the time to the expiration deadline is lower than 1000, return -1.
* <p>
* tha mean the value should expire immediately
* When the time to the expiration deadline is lower than 100 milliseconds, return -1.
* That mean the value should expire immediately
*
* @return the time to the expiration deadline
*/
public long getLastingTime() {
if (doExpire) {
final long lt = deadline - System.currentTimeMillis();
if (lt < 1000) {
if (lt < 100) {
return -1;
} else {
return lt;
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/io/github/grrolland/hcshm/HCSHMTestSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
* Test Suite initializing the distributed SHM
*/
@RunWith(Suite.class)
@Suite.SuiteClasses({DeleteTestCase.class,
@Suite.SuiteClasses({ShmValueTestCase.class,
DeleteTestCase.class,
GetTestCase.class,
IncrTestCase.class,
QuitTestCase.class,
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/io/github/grrolland/hcshm/IncrTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,15 @@ public void testIncrExpire() {
public void testMultiIncrExpireLoad() throws IOException {
Logger logger = (Logger) LoggerFactory.getLogger(IncrTestCase.class);
logger.setLevel(Level.INFO);

// Increment key
for (int i = 1; i <= 200; i++) {
getWriter().write("INCR key 1 0 60\r\n");
getWriter().flush();
logger.info("Expect value {}", i);
assertResponseGetValue(String.valueOf(i));
}
logger.info("DONE");
}

/**
Expand Down
46 changes: 46 additions & 0 deletions src/test/java/io/github/grrolland/hcshm/ShmValueTestCase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.github.grrolland.hcshm;

import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/***
* ShmValue test case
*/
public class ShmValueTestCase {

@Test
public void getValue() {
ShmValue shmValue = new ShmValue("X", 0);
assertEquals("X", shmValue.getValue());
}

@Test
public void getLastingTime() {
ShmValue shmValue = new ShmValue("X", 0);
assertEquals(0, shmValue.getLastingTime());
}

@Test
public void getLastingTimeExpired() {
ShmValue shmValue = new ShmValue("X", 99);
assertEquals(-1, shmValue.getLastingTime());
}

@Test
public void getLastingTimeNotExpired() {
ShmValue shmValue = new ShmValue("X", 150);
assertTrue(shmValue.getLastingTime() > 100);
}

@Test
public void expire() {
ShmValue shmValue = new ShmValue("X", 0);
assertEquals(0, shmValue.getLastingTime());
shmValue.expire(150);
assertTrue(shmValue.getLastingTime() > 100);
shmValue.expire(99);
assertEquals(-1, shmValue.getLastingTime());
}
}

0 comments on commit 28139ab

Please sign in to comment.