Search in sources :

Example 96 with Timestamp

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

the class AggregateStorageShould method sort_by_version_rather_then_by_timestamp.

@Test
public void sort_by_version_rather_then_by_timestamp() {
    final Project state = Project.getDefaultInstance();
    final Version minVersion = zero();
    final Version maxVersion = increment(minVersion);
    final Timestamp minTimestamp = Timestamps.MIN_VALUE;
    final Timestamp maxTimestamp = Timestamps.MAX_VALUE;
    // The first event is an event, which is the oldest, i.e. with the minimal version.
    final Event expectedFirst = eventFactory.createEvent(state, minVersion, maxTimestamp);
    final Event expectedSecond = eventFactory.createEvent(state, maxVersion, minTimestamp);
    storage.writeEvent(id, expectedSecond);
    storage.writeEvent(id, expectedFirst);
    final List<Event> events = storage.read(newReadRequest(id)).get().getEventList();
    assertTrue(events.indexOf(expectedFirst) < events.indexOf(expectedSecond));
}
Also used : Project(io.spine.test.aggregate.Project) Version(io.spine.core.Version) Event(io.spine.core.Event) Timestamp(com.google.protobuf.Timestamp) Test(org.junit.Test)

Example 97 with Timestamp

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

the class AggregateStorageShould method write_records_and_return_sorted_by_version_descending.

@Test
public void write_records_and_return_sorted_by_version_descending() {
    final int eventsNumber = 5;
    final List<AggregateEventRecord> records = newLinkedList();
    final Timestamp timestamp = getCurrentTime();
    Version currentVersion = zero();
    for (int i = 0; i < eventsNumber; i++) {
        final Project state = Project.getDefaultInstance();
        final Event event = eventFactory.createEvent(state, currentVersion, timestamp);
        final AggregateEventRecord record = StorageRecord.create(timestamp, event);
        records.add(record);
        currentVersion = increment(currentVersion);
    }
    writeAll(id, records);
    final Iterator<AggregateEventRecord> iterator = historyBackward();
    final List<AggregateEventRecord> actual = newArrayList(iterator);
    // expected records should be in a reverse order
    reverse(records);
    assertEquals(records, actual);
}
Also used : Project(io.spine.test.aggregate.Project) Version(io.spine.core.Version) Event(io.spine.core.Event) Timestamp(com.google.protobuf.Timestamp) Test(org.junit.Test)

Example 98 with Timestamp

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

the class EventStoreShould method read_events_by_type.

@Test
public void read_events_by_type() {
    final Timestamp now = getCurrentTime();
    final Event taskAdded1 = taskAdded(now);
    final Event projectCreated = projectCreated(now);
    final Event teasAdded2 = taskAdded(now);
    eventStore.append(taskAdded1);
    eventStore.append(projectCreated);
    eventStore.append(teasAdded2);
    final EventFilter taskAddedType = EventFilter.newBuilder().setEventType(of(TaskAdded.class).value()).build();
    final EventStreamQuery query = EventStreamQuery.newBuilder().addFilter(taskAddedType).build();
    final AtomicBoolean done = new AtomicBoolean(false);
    final Collection<Event> resultEvents = newConcurrentHashSet();
    eventStore.read(query, new ResponseObserver(resultEvents, done));
    assertDone(done);
    assertSize(2, resultEvents);
    assertContainsAll(resultEvents, taskAdded1, teasAdded2);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TaskAdded(io.spine.test.event.TaskAdded) Event(io.spine.core.Event) Timestamp(com.google.protobuf.Timestamp) Test(org.junit.Test)

Example 99 with Timestamp

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

the class IteratingCommandRouterShould method produce_a_command_on_routeNext.

@Test
public void produce_a_command_on_routeNext() throws Exception {
    /*
        This is a hack, aimed to resolve the wall-clock inaccuracy issue, that is randomly
        causing the test failure due to a fast execution.

        <p>The idea is to add some randomization to {@code nanoseconds} value of the
        current Timestamp obtained from the wall-clock provider.
        */
    Time.setProvider(new Time.Provider() {

        @Override
        public Timestamp getCurrentTime() {
            final Timestamp millis = Timestamps.fromMillis(System.currentTimeMillis());
            final Timestamp nanos = Timestamps.fromNanos(System.nanoTime());
            final Timestamp result = millis.toBuilder().setNanos(nanos.toBuilder().getNanos()).build();
            return result;
        }
    });
    final CommandRouted firstRouted = router().routeFirst();
    assertTrue(router().hasNext());
    final Command command = router().routeNext();
    // Test that 2nd command message is in the next routed command.
    assertEquals(messages().get(1), Commands.getMessage(command));
    // Verify that the context for the next routed command has been created, not just copied.
    final Command firstCommand = firstRouted.getSource();
    assertNotEquals(firstCommand.getContext().getActorContext().getTimestamp(), command.getContext().getActorContext().getTimestamp());
    // Revert the hack.
    Time.resetProvider();
}
Also used : Command(io.spine.core.Command) Time(io.spine.base.Time) Timestamp(com.google.protobuf.Timestamp) Test(org.junit.Test)

Example 100 with Timestamp

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

the class ProjectionRepositoryShould method convert_null_timestamp_to_default.

@Test
public void convert_null_timestamp_to_default() {
    final Timestamp timestamp = getCurrentTime();
    assertEquals(timestamp, ProjectionRepository.nullToDefault(timestamp));
    assertEquals(Timestamp.getDefaultInstance(), ProjectionRepository.nullToDefault(null));
}
Also used : Timestamp(com.google.protobuf.Timestamp) Test(org.junit.Test)

Aggregations

Timestamp (com.google.protobuf.Timestamp)120 Test (org.junit.Test)84 Duration (com.google.protobuf.Duration)24 Interval (io.spine.time.Interval)16 Command (io.spine.core.Command)9 AbstractZonedTimeTest (io.spine.time.AbstractZonedTimeTest)8 LocalTime (io.spine.time.LocalTime)8 Nullable (javax.annotation.Nullable)7 StringValue (com.google.protobuf.StringValue)6 Event (io.spine.core.Event)6 Message (com.google.protobuf.Message)5 Predicate (com.google.common.base.Predicate)4 TimeTests (io.spine.test.TimeTests)4 CommandContext (io.spine.core.CommandContext)3 EventContext (io.spine.core.EventContext)3 Version (io.spine.core.Version)3 Any (com.google.protobuf.Any)2 ByteString (com.google.protobuf.ByteString)2 PubsubMessage (com.google.pubsub.v1.PubsubMessage)2 PullRequest (com.google.pubsub.v1.PullRequest)2