Search in sources :

Example 41 with Duration

use of com.google.protobuf.Duration in project core-java by SpineEventEngine.

the class OperatorEvaluatorShould method compare_timestamps_by_GT.

@Test
public void compare_timestamps_by_GT() {
    final Duration delta = seconds(5);
    final Timestamp small = getCurrentTime();
    final Timestamp medium = add(small, delta);
    final Timestamp big = add(medium, delta);
    assertTrue(eval(medium, GREATER_THAN, small));
    assertTrue(eval(big, GREATER_THAN, medium));
    assertTrue(eval(big, GREATER_THAN, small));
    assertFalse(eval(small, GREATER_THAN, small));
    assertFalse(eval(small, GREATER_THAN, big));
    assertFalse(eval(nullRef(), GREATER_THAN, small));
}
Also used : Duration(com.google.protobuf.Duration) Timestamp(com.google.protobuf.Timestamp) Test(org.junit.Test)

Example 42 with Duration

use of com.google.protobuf.Duration in project core-java by SpineEventEngine.

the class CommandScheduler method setSchedulingTime.

/**
 * Sets a new scheduling time in the {@linkplain CommandContext.Schedule context}
 * of the passed command.
 *
 * @param command        a command to update
 * @param schedulingTime the time when the command was scheduled by the {@code CommandScheduler}
 * @return an updated command
 */
static Command setSchedulingTime(Command command, Timestamp schedulingTime) {
    checkNotNull(command);
    checkNotNull(schedulingTime);
    final Duration delay = command.getContext().getSchedule().getDelay();
    final Command result = setSchedule(command, delay, schedulingTime);
    return result;
}
Also used : Command(io.spine.core.Command) Duration(com.google.protobuf.Duration)

Example 43 with Duration

use of com.google.protobuf.Duration in project core-java by SpineEventEngine.

the class ExecutorCommandScheduler method getDelayMilliseconds.

private static long getDelayMilliseconds(Command command) {
    final Schedule schedule = command.getContext().getSchedule();
    final Duration delay = schedule.getDelay();
    final long delaySec = delay.getSeconds();
    final long delayMillisFraction = delay.getNanos() / NANOS_IN_MILLISECOND;
    /**
     * Maximum value of {@link Duration#getSeconds()} is
     * <a href="https://github.com/google/protobuf/blob/master/src/google/protobuf/duration.proto"+315,576,000,000.</a>.
     *
     * {@link Long.MAX_VALUE} is +9,223,372,036,854,775,807. That's why it is safe to multiply
     * {@code delaySec * MILLIS_IN_SECOND}.
     */
    final long absoluteMillis = delaySec * MILLIS_IN_SECOND + delayMillisFraction;
    return absoluteMillis;
}
Also used : Schedule(io.spine.core.CommandContext.Schedule) Duration(com.google.protobuf.Duration)

Example 44 with Duration

use of com.google.protobuf.Duration in project core-java by SpineEventEngine.

the class Rescheduler method reschedule.

private void reschedule(Command command) {
    final Timestamp now = getCurrentTime();
    final Timestamp timeToPost = getTimeToPost(command);
    if (isLaterThan(now, /*than*/
    timeToPost)) {
        onScheduledCommandExpired(command);
    } else {
        final Interval interval = Intervals.between(now, timeToPost);
        final Duration newDelay = Intervals.toDuration(interval);
        final Command updatedCommand = CommandScheduler.setSchedule(command, newDelay, now);
        scheduler().schedule(updatedCommand);
    }
}
Also used : Command(io.spine.core.Command) Duration(com.google.protobuf.Duration) Timestamp(com.google.protobuf.Timestamp) Interval(io.spine.time.Interval)

Example 45 with Duration

use of com.google.protobuf.Duration in project core-java by SpineEventEngine.

the class Commands method isScheduled.

/**
 * Checks if the command is scheduled to be delivered later.
 *
 * @param command a command to check
 * @return {@code true} if the command context has a scheduling option set,
 * {@code false} otherwise
 */
public static boolean isScheduled(Command command) {
    checkNotNull(command);
    final Schedule schedule = command.getContext().getSchedule();
    final Duration delay = schedule.getDelay();
    if (isNotDefault(delay)) {
        checkArgument(delay.getSeconds() > 0, "Command delay seconds must be a positive value.");
        return true;
    }
    return false;
}
Also used : Schedule(io.spine.core.CommandContext.Schedule) Duration(com.google.protobuf.Duration)

Aggregations

Duration (com.google.protobuf.Duration)45 Test (org.junit.Test)31 Timestamp (com.google.protobuf.Timestamp)25 AbstractZonedTimeTest (io.spine.time.AbstractZonedTimeTest)8 LocalTime (io.spine.time.LocalTime)8 Command (io.spine.core.Command)5 BulkWriteOperation (io.spine.server.projection.BulkWriteOperation)4 Message (com.google.protobuf.Message)3 Schedule (io.spine.core.CommandContext.Schedule)3 Event (io.spine.core.Event)3 Interval (io.spine.time.Interval)3 Event (io.spine.base.Event)2 CommandContext (io.spine.core.CommandContext)2 ProjectId (io.spine.test.projection.ProjectId)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 ImmutableCollection (com.google.common.collect.ImmutableCollection)1 Schedule (io.spine.base.CommandContext.Schedule)1 Error (io.spine.base.Error)1 ThrowableMessage (io.spine.base.ThrowableMessage)1 CommandEnvelope (io.spine.core.CommandEnvelope)1