Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/java-siri-client' into siri-retr…
Browse files Browse the repository at this point in the history
…iever-dev

# Conflicts:
#	siri/siri_retriever/siri-0.1/src/main/java/org/hasadna/bus/service/ScheduleRetrieval.java
#	siri/siri_retriever/siri-0.1/src/test/resources/application-test.properties
  • Loading branch information
evyatark committed Nov 17, 2018
2 parents 1964931 + d868f23 commit 81e4c2a
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 3 deletions.
@@ -0,0 +1,144 @@
package org.hasadna.bus;

import org.assertj.core.api.Assertions;
import org.hasadna.bus.config.SchedulerConfig;
import org.hasadna.bus.service.Command;
import org.hasadna.bus.service.ScheduleRetrieval;
import org.hasadna.bus.service.SortedQueue;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.test.context.junit4.SpringRunner;

import java.time.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import static org.hasadna.bus.util.DateTimeUtils.DEFAULT_CLOCK;
import static org.hasadna.bus.util.DateTimeUtils.timeAsStr;
import static org.hasadna.bus.util.DateTimeUtils.toDateTime;

@RunWith(SpringRunner.class)
@SpringBootTest //(classes = {SchedulerConfig.class, BusApplication.class})
public class ValidateSchedulingTest {

@Autowired
ScheduleRetrieval scheduleRetrieval;

@Autowired
SortedQueue sq;

List<Command> data = new ArrayList<>();
LocalDateTime currentTime;

@Before
public void setup() {

currentTime = LocalDateTime.of(2018, 8, 7, 10, 0);
DayOfWeek dayOfWeek = currentTime.getDayOfWeek();

Command c = new Command("stopCode1");
c.lineRef = "lineRef1";
c.isActive = true;
c.weeklyDepartureTimes = new HashMap<>();
c.weeklyDepartureTimes.put(dayOfWeek, Arrays.asList("06:00", "07:00"));
c.lastArrivalTimes = new HashMap<>();
c.lastArrivalTimes.put(dayOfWeek, "09:00");
data.add(c);

}

@Test
public void test1() {

ScheduleRetrieval.ValidationResults validationResults = scheduleRetrieval.validateScheduling(
data,
currentTime);

Assertions.assertThat( validationResults.delayTillFirstDeparture ).isNotNull().isEmpty();
Assertions.assertThat( validationResults.notNeeded).isNotNull().isNotEmpty();
Assertions.assertThat( validationResults.notNeeded.get(0).stopCode).isEqualTo("stopCode1");
}

@Test
public void test2() {

currentTime = LocalDateTime.of(2018, 8, 7, 2, 0);
ScheduleRetrieval.ValidationResults validationResults = scheduleRetrieval.validateScheduling(
data,
currentTime);

Assertions.assertThat( validationResults.notNeeded ).isNotNull().isEmpty();
Assertions.assertThat( validationResults.delayTillFirstDeparture).isNotNull().isNotEmpty();
Assertions.assertThat( validationResults.delayTillFirstDeparture.get(0).stopCode).isEqualTo("stopCode1");
//// Assertions.assertThat( validationResults.delayTillFirstDeparture.get(0).nextExecution).isNotNull();
// Assertions.assertThat( currentTime).isNotNull();
// Assertions.assertThat( timeAsStr( currentTime.withHour(5).toLocalTime())).isEqualTo("05:00");
// Assertions.assertThat( validationResults.delayTillFirstDeparture.get(0).nextExecution)
// .isAfter(currentTime.withHour(5))
// .isBefore(currentTime.withHour(6));
}

@Test
public void test3() throws InterruptedException {
currentTime = LocalDateTime.of(2018, 8, 7, 2, 0);
DEFAULT_CLOCK = Clock.fixed(currentTime.toInstant(ZoneOffset.UTC), ZoneOffset.UTC);
sq.removeAll();
//System.out.println("a "+sq.getAllSchedules());
data.forEach(c -> sq.put(c));

//System.out.println("b "+sq.getAllSchedules());
scheduleRetrieval.updateSchedulingDataPeriodically();
Thread.sleep(2000); // updateScheduling happens in a different thread, so give it some time to happen
//System.out.println("c "+sq.getAllSchedules());
Assertions.assertThat(sq.getAllSchedules()).isNotNull().isNotEmpty();
//System.out.println("1 "+sq.getAllSchedules());
//List<Command> list = sq.getAllSchedules();
Assertions.assertThat( currentTime).isNotNull();
Assertions.assertThat( timeAsStr( currentTime.withHour(5).toLocalTime())).isEqualTo("05:00");
//System.out.println("2 "+list);
Assertions.assertThat(sq.getAllSchedules().get(0).nextExecution).isNotNull();
//System.out.println("3 "+sq.getAllSchedules());
Assertions.assertThat(sq.getAllSchedules().get(0).nextExecution).isNotNull();
LocalDateTime ne = sq.getAllSchedules().get(0).nextExecution;
LocalDateTime be = toDateTime("05:31", currentTime.toLocalDate());
LocalDateTime af = toDateTime("05:29", currentTime.toLocalDate());
Assertions.assertThat(ne)
.isBefore(be)
.isAfter(af);
// Assertions.assertThat(sq.getAllSchedules().get(0).nextExecution)
// .isBefore(toDateTime("05:31", currentTime.toLocalDate()))
// .isAfter(toDateTime("05:29", currentTime.toLocalDate()));

}

@Test
public void test4() throws InterruptedException {
//currentTime = LocalDateTime.of(2018, 8, 7, 2, 0);
DEFAULT_CLOCK =
Clock.offset(Clock.systemDefaultZone(), Duration.ofHours(-55) );
//Clock.fixed(Instant.parse("2018-04-29T10:15:30.00Z"),
// ZoneId.of("Asia/Calcutta"));
//Clock myClock = DEFAULT_CLOCK;
System.out.println(DEFAULT_CLOCK);
currentTime = LocalDateTime.now(DEFAULT_CLOCK);
System.out.println("now is " + currentTime.toString());

while (currentTime.toString().equals("1")) {
try {
if (sq.getAll() == null) System.out.println("sq.getAll is null");
System.out.println("" + LocalDateTime.now() + " queue:" + sq.getAll().length + ", active:" + sq.showActive().size());
System.out.println(" next at:" + sq.peek().nextExecution.toLocalTime().toString());
}
catch ( Exception ex) {
System.out.println("absorbing exception " + ex.getMessage());
}
Thread.sleep(10000);
}
}
}
Expand Up @@ -100,7 +100,6 @@ private boolean checkNecessityOfThisSchedulingUntilFirstDeparture(Command c, Loc
if (timeOfFirstDeparture.isAfter(currentTime.plusMinutes(30))) { // timeOfFirstDeparture is more than 30 minutes from now
// set nextExecution to 30 minutes before firstDeparture
LocalDateTime nextExecution = timeOfFirstDeparture.minusMinutes(30);
//c.nextExecution = nextExecution;
logger.info("route {} - postpone next execution to {}, firstDeparture only at {}", c.lineRef, nextExecution, timeOfFirstDeparture);
return disabled; //notNeeded
}
Expand Down Expand Up @@ -188,13 +187,13 @@ public ValidationResults validateScheduling(List<Command> data, LocalDateTime cu
List<Command> notNeeded = new ArrayList<>();
List<Command> delayTillFirstDeparture = new ArrayList<>();
for (Command c : data) {
// schedulerInactiveMechanismEnabled=false means that this method will only log its result
if (false == checkNecessityOfThisSchedulingUntilFirstDeparture(c, currentTime, !schedulerInactiveMechanismEnabled)) {
delayTillFirstDeparture.add(c);
//c.isActive = false;
c.isActive = false;
}
else if (false == checkNecessityOfThisSchedulingForRestOfToday(c, currentTime, !schedulerInactiveMechanismEnabled)) {
notNeeded.add(c);
// disabled = true means that this method will only log its result
}
}
if (!delayTillFirstDeparture.isEmpty()) {
Expand Down

0 comments on commit 81e4c2a

Please sign in to comment.