You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
2.0 KiB
Java
63 lines
2.0 KiB
Java
package org.gcube.application.geoportal.service.engine.caches;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.gcube.application.cms.tests.TokenSetter;
|
|
import org.gcube.application.geoportal.service.engine.providers.AbstractScopedMap;
|
|
import org.gcube.application.geoportal.service.engine.providers.TTLObject;
|
|
import org.gcube.application.geoportal.service.model.internal.faults.ConfigurationException;
|
|
import org.junit.Test;
|
|
|
|
import java.time.Duration;
|
|
import java.time.Instant;
|
|
import java.time.LocalDateTime;
|
|
import java.time.temporal.ChronoUnit;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
import static org.junit.Assert.assertTrue;
|
|
|
|
@Slf4j
|
|
public class Caches {
|
|
|
|
@Test
|
|
public void testCache() throws ConfigurationException {
|
|
|
|
TokenSetter.set("/gcube/devsec/devVRE");
|
|
|
|
Duration ttl=Duration.of(10, ChronoUnit.SECONDS);
|
|
|
|
Duration monitorWindow=Duration.of(100, ChronoUnit.SECONDS);
|
|
|
|
DummyCache cache= new DummyCache();
|
|
cache.setTTL(ttl);
|
|
|
|
|
|
Instant startMonitoring=Instant.now();
|
|
|
|
LocalDateTime previous=cache.getObject();
|
|
while(Duration.between(startMonitoring,Instant.now()).compareTo(monitorWindow)<0){
|
|
LocalDateTime obj= cache.getObject();
|
|
if(obj.equals(previous)){
|
|
//objects are equals, TTL should be valid
|
|
// Assert : now-creationTime < TTL
|
|
assertTrue(Duration.between(obj,LocalDateTime.now()).compareTo(ttl)<0);
|
|
}else {
|
|
// different object only after TTL
|
|
// Assert : now-creationTime < TTL
|
|
assertTrue(Duration.between(obj,LocalDateTime.now()).compareTo(ttl)<0);
|
|
// Assert : now-previous.creationTime > TTL
|
|
assertTrue(Duration.between(previous,LocalDateTime.now()).compareTo(ttl)>0);
|
|
}
|
|
previous=obj;
|
|
try {
|
|
Thread.sleep(ttl.abs().dividedBy(2).toMillis());
|
|
} catch (InterruptedException e) {
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|