gcube-cms-suite/geoportal-service/src/test/java/org/gcube/application/geoportal/service/engine/caches/Caches.java

146 lines
5.8 KiB
Java
Raw Normal View History

2021-10-05 16:27:19 +02:00
package org.gcube.application.geoportal.service.engine.caches;
2021-10-06 14:34:07 +02:00
import com.mongodb.MongoWaitQueueFullException;
2021-10-05 16:27:19 +02:00
import lombok.extern.slf4j.Slf4j;
2022-09-20 14:46:22 +02:00
import org.gcube.application.cms.implementations.ImplementationProvider;
2021-10-05 16:27:19 +02:00
import org.gcube.application.cms.tests.TokenSetter;
2021-12-01 12:02:48 +01:00
import org.gcube.application.cms.tests.model.concessioni.TestConcessioniModel;
2022-09-20 14:46:22 +02:00
import org.gcube.application.geoportal.common.model.rest.ConfigurationException;
import org.gcube.application.geoportal.common.utils.StorageUtils;
2022-02-24 18:09:30 +01:00
import org.gcube.application.geoportal.common.utils.tests.GCubeTest;
2021-10-06 11:26:35 +02:00
import org.gcube.application.geoportal.service.BasicServiceTestUnit;
2021-10-05 16:27:19 +02:00
import org.junit.Test;
import java.io.File;
import java.io.FileNotFoundException;
2021-10-05 16:27:19 +02:00
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
2021-10-06 11:26:35 +02:00
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
2021-10-05 16:27:19 +02:00
import java.util.concurrent.TimeUnit;
2021-10-06 11:26:35 +02:00
import java.util.concurrent.atomic.AtomicLong;
2021-10-05 16:27:19 +02:00
import static org.junit.Assert.assertTrue;
2022-02-24 18:09:30 +01:00
import static org.junit.Assume.assumeTrue;
2021-10-05 16:27:19 +02:00
@Slf4j
2021-10-06 11:26:35 +02:00
public class Caches extends BasicServiceTestUnit {
2021-10-05 16:27:19 +02:00
@Test
public void testCache() throws ConfigurationException {
2022-02-24 18:09:30 +01:00
assumeTrue(GCubeTest.isTestInfrastructureEnabled());
TokenSetter.set(GCubeTest.getContext());
2021-10-05 16:27:19 +02:00
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) {
}
}
2021-10-06 11:26:35 +02:00
}
2021-10-05 16:27:19 +02:00
2022-02-24 18:09:30 +01:00
// @Test
2022-09-20 14:46:22 +02:00
// public void mongoconnections() throws ConfigurationException, InterruptedException {
// assumeTrue(GCubeTest.isTestInfrastructureEnabled());
// TokenSetter.set(GCubeTest.getContext());
// ExecutorService service = Executors.newFixedThreadPool(1000);
// LocalDateTime start=LocalDateTime.now();
// AtomicLong executed = new AtomicLong(0);
// AtomicLong launched = new AtomicLong(0);
// //for 100 secs
// while(Duration.between(start,LocalDateTime.now()).
// compareTo(Duration.of(100, ChronoUnit.SECONDS))<0){
// service.execute(new Runnable() {
// @Override
// public void run() {
// try {
// new ConcessioniMongoManager().list();
// } catch (ConfigurationException e) {
// e.printStackTrace();
// } catch (MongoWaitQueueFullException e) {
// log.info("Too many connections... ");
// }finally{
// executed.incrementAndGet();
// try {Thread.sleep(500);} catch (InterruptedException i) {}
// }
// }
// });
// launched.incrementAndGet();
// }
//
// while (!service.awaitTermination(2, TimeUnit.MINUTES)) {
// log.info("Waiting .. completed {}, out of {} ",executed.get(),launched.get());
// if(executed.get()==launched.get()) service.shutdown();
// }
// }
2021-10-06 16:12:16 +02:00
2022-02-24 18:09:30 +01:00
//@Test
public void testStorageCache() throws ConfigurationException, FileNotFoundException, InterruptedException {
2022-02-24 18:09:30 +01:00
assumeTrue(GCubeTest.isTestInfrastructureEnabled());
TokenSetter.set(GCubeTest.getContext());
ExecutorService service = Executors.newFixedThreadPool(10);
LocalDateTime start=LocalDateTime.now();
AtomicLong executed = new AtomicLong(0);
AtomicLong launched = new AtomicLong(0);
2022-04-27 19:36:10 +02:00
final StorageUtils storage=ImplementationProvider.get().getProvidedObjectByClass(StorageUtils.class);
2021-12-01 12:02:48 +01:00
String id =storage.putOntoStorage(new File(TestConcessioniModel.getBaseFolder(),"relazione.pdf"))[0].getId();
//for 100 secs
while(Duration.between(start,LocalDateTime.now()).
compareTo(Duration.of(100, ChronoUnit.SECONDS))<0){
service.execute(new Runnable() {
@Override
public void run() {
try {
2021-10-12 15:43:35 +02:00
try {Thread.sleep(1000);} catch (InterruptedException i) {}
2022-02-18 18:11:34 +01:00
System.out.println(storage.getURL(id));
2021-10-12 15:43:35 +02:00
// storage.getURL(id);
} catch (MongoWaitQueueFullException e) {
log.info("Too many connections... ");
}finally{
executed.incrementAndGet();
try {Thread.sleep(500);} catch (InterruptedException i) {}
}
}
});
launched.incrementAndGet();
}
while (!service.awaitTermination(2, TimeUnit.MINUTES)) {
log.info("Waiting .. completed {}, out of {} ",executed.get(),launched.get());
if(executed.get()==launched.get()) service.shutdown();
}
}
2021-10-06 16:12:16 +02:00
2021-10-05 16:27:19 +02:00
}