From 0e3e118489a687db96eb34f1f33980f3ce247729 Mon Sep 17 00:00:00 2001 From: amckenzie Date: Wed, 19 Jun 2019 14:26:35 +0100 Subject: [PATCH] Update to use the latest system commands --- .../cakeshop/it/CatchupPerformanceIT.java | 90 ++++++------------- .../it/helpers/ProcessedEventCounter.java | 34 +++++++ .../it/helpers/PublishedEventCounter.java | 48 ---------- .../it/helpers/RecipeTableInspector.java | 88 ------------------ pom.xml | 10 +-- 5 files changed, 65 insertions(+), 205 deletions(-) create mode 100644 example-context/example-service/example-it/src/test/java/uk/gov/justice/services/example/cakeshop/it/helpers/ProcessedEventCounter.java delete mode 100644 example-context/example-service/example-it/src/test/java/uk/gov/justice/services/example/cakeshop/it/helpers/PublishedEventCounter.java delete mode 100644 example-context/example-service/example-it/src/test/java/uk/gov/justice/services/example/cakeshop/it/helpers/RecipeTableInspector.java diff --git a/example-context/example-service/example-it/src/test/java/uk/gov/justice/services/example/cakeshop/it/CatchupPerformanceIT.java b/example-context/example-service/example-it/src/test/java/uk/gov/justice/services/example/cakeshop/it/CatchupPerformanceIT.java index 528cd62b..4b7b2a63 100644 --- a/example-context/example-service/example-it/src/test/java/uk/gov/justice/services/example/cakeshop/it/CatchupPerformanceIT.java +++ b/example-context/example-service/example-it/src/test/java/uk/gov/justice/services/example/cakeshop/it/CatchupPerformanceIT.java @@ -13,20 +13,17 @@ import uk.gov.justice.services.eventsourcing.repository.jdbc.event.EventStreamJdbsRepositoryFactory; import uk.gov.justice.services.eventsourcing.repository.jdbc.eventstream.EventStreamJdbcRepository; import uk.gov.justice.services.eventsourcing.repository.jdbc.exception.InvalidPositionException; -import uk.gov.justice.services.eventstore.management.catchup.commands.CatchupCommand; +import uk.gov.justice.services.eventstore.management.shuttercatchup.commands.ShutterCatchupCommand; import uk.gov.justice.services.example.cakeshop.it.helpers.CakeshopEventGenerator; import uk.gov.justice.services.example.cakeshop.it.helpers.DatabaseManager; import uk.gov.justice.services.example.cakeshop.it.helpers.PositionInStreamIterator; -import uk.gov.justice.services.example.cakeshop.it.helpers.PublishedEventCounter; -import uk.gov.justice.services.example.cakeshop.it.helpers.RecipeTableInspector; +import uk.gov.justice.services.example.cakeshop.it.helpers.ProcessedEventCounter; import uk.gov.justice.services.example.cakeshop.it.helpers.RestEasyClientFactory; import uk.gov.justice.services.jmx.system.command.client.SystemCommanderClient; import uk.gov.justice.services.jmx.system.command.client.SystemCommanderClientFactory; import uk.gov.justice.services.test.utils.core.messaging.Poller; import uk.gov.justice.services.test.utils.persistence.DatabaseCleaner; -import java.util.ArrayList; -import java.util.List; import java.util.Optional; import java.util.UUID; @@ -46,8 +43,7 @@ public class CatchupPerformanceIT { private final EventStreamJdbsRepositoryFactory eventStreamJdbcRepositoryFactory = new EventStreamJdbsRepositoryFactory(); private final EventStreamJdbcRepository eventStreamJdbcRepository = eventStreamJdbcRepositoryFactory.getEventStreamJdbcRepository(eventStoreDataSource); - private final RecipeTableInspector recipeTableInspector = new RecipeTableInspector(viewStoreDataSource); - private final PublishedEventCounter publishedEventCounter = new PublishedEventCounter(eventStoreDataSource); + private final ProcessedEventCounter processedEventCounter = new ProcessedEventCounter(viewStoreDataSource); private static final String HOST = getHost(); private static final int PORT = valueOf(getProperty("random.management.port")); @@ -68,6 +64,7 @@ public void before() { databaseCleaner.cleanEventStoreTables(contextName); cleanViewstoreTables(); + databaseCleaner.cleanSystemTables(contextName); } @After @@ -82,10 +79,10 @@ public void shouldReplayAndFindRecipesInViewStore() throws Exception { final int numberOfEventsPerStream = 100; final int totalEvents = numberOfStreams * numberOfEventsPerStream; - final List streamIds = addEventsToEventLog(numberOfStreams, numberOfEventsPerStream); + addEventsToEventLog(numberOfStreams, numberOfEventsPerStream); final Optional numberOfEvents = longPoller.pollUntilFound(() -> { - final int eventCount = publishedEventCounter.countPublishedEvents(); + final int eventCount = processedEventCounter.countProcessedEvents(); if (eventCount == totalEvents) { return of(eventCount); } @@ -99,58 +96,39 @@ public void shouldReplayAndFindRecipesInViewStore() throws Exception { fail("Failed to insert " + totalEvents + " events"); } - for (final UUID streamId : streamIds) { - - final Optional eventCount = longPoller.pollUntilFound(() -> { - final long eventsPerStream = recipeTableInspector.countEventsPerStream(streamId); - if (eventsPerStream == numberOfEventsPerStream) { - return of(eventsPerStream); - } - - return empty(); - }); + cleanViewstoreTables(); - if (!eventCount.isPresent()) { - fail("Expected " + numberOfEventsPerStream + " events but found " + recipeTableInspector.countEventsPerStream(streamId) + " in stream " + streamId); + longPoller.pollUntilFound(() -> { + final int eventCount = processedEventCounter.countProcessedEvents(); + if (eventCount == 0) { + return of(eventCount); } - } - cleanViewstoreTables(); + return empty(); + }); runCatchup(); - final Optional numberOfReplayedRecipesOptional = checkExpectedNumberOfRecipes(numberOfStreams); - - if (!numberOfReplayedRecipesOptional.isPresent()) { - fail(); - } - - - for (final UUID streamId : streamIds) { - - final Optional eventCount = longPoller.pollUntilFound(() -> { - final long eventsPerStream = recipeTableInspector.countEventsPerStream(streamId); - if (eventsPerStream == numberOfEventsPerStream) { - return of(eventsPerStream); - } + final Optional numberOfReplayedEvents = longPoller.pollUntilFound(() -> { + final int eventCount = processedEventCounter.countProcessedEvents(); + if (eventCount == totalEvents) { + return of(eventCount); + } - return empty(); - }); + return empty(); + }); - if (!eventCount.isPresent()) { - fail(); - } + if (numberOfReplayedEvents.isPresent()) { + System.out.println("Successfully caught up " + numberOfEvents.get() + " events"); + } else { + fail("Failed to catchup " + totalEvents + " events."); } - - publishedEventCounter.truncatePublishQueue(); } - private List addEventsToEventLog(final int numberOfStreams, final int numberOfEventsPerStream) throws InvalidPositionException { + private void addEventsToEventLog(final int numberOfStreams, final int numberOfEventsPerStream) throws InvalidPositionException { final CakeshopEventGenerator cakeshopEventGenerator = new CakeshopEventGenerator(); - final List streamIds = new ArrayList<>(); - for (int seed = 0; seed < numberOfStreams; seed++) { final PositionInStreamIterator positionInStreamIterator = new PositionInStreamIterator(); @@ -158,8 +136,6 @@ private List addEventsToEventLog(final int numberOfStreams, final int numb final Event recipeAddedEvent = cakeshopEventGenerator.createRecipeAddedEvent(seed, positionInStreamIterator); final UUID recipeId = recipeAddedEvent.getStreamId(); - streamIds.add(recipeId); - eventStreamJdbcRepository.insert(recipeId); eventJdbcRepository.insert(recipeAddedEvent); @@ -168,30 +144,16 @@ private List addEventsToEventLog(final int numberOfStreams, final int numb eventJdbcRepository.insert(recipeRenamedEvent); } } - - return streamIds; } private void runCatchup() throws Exception { try (final SystemCommanderClient systemCommanderClient = systemCommanderClientFactory.create(HOST, PORT)) { - systemCommanderClient.getRemote().call(new CatchupCommand()); + systemCommanderClient.getRemote().call(new ShutterCatchupCommand()); } } - private Optional checkExpectedNumberOfRecipes(final int numberOfStreams) { - return longPoller.pollUntilFound(() -> { - final int numberOfRecipes = recipeTableInspector.countNumberOfRecipes(); - - if (numberOfRecipes == numberOfStreams) { - return of(numberOfRecipes); - } - - return empty(); - }); - } - private void cleanViewstoreTables() { final String contextName = "framework"; diff --git a/example-context/example-service/example-it/src/test/java/uk/gov/justice/services/example/cakeshop/it/helpers/ProcessedEventCounter.java b/example-context/example-service/example-it/src/test/java/uk/gov/justice/services/example/cakeshop/it/helpers/ProcessedEventCounter.java new file mode 100644 index 00000000..d4c40ec8 --- /dev/null +++ b/example-context/example-service/example-it/src/test/java/uk/gov/justice/services/example/cakeshop/it/helpers/ProcessedEventCounter.java @@ -0,0 +1,34 @@ +package uk.gov.justice.services.example.cakeshop.it.helpers; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +import javax.sql.DataSource; + +public class ProcessedEventCounter { + + private final DataSource viewStoreDataSource; + + public ProcessedEventCounter(final DataSource viewStoreDataSource) { + this.viewStoreDataSource = viewStoreDataSource; + } + + + public int countProcessedEvents() { + + final String sql = "SELECT COUNT (*) FROM processed_event"; + + try(final Connection connection = viewStoreDataSource.getConnection(); + final PreparedStatement preparedStatement = connection.prepareStatement(sql); + final ResultSet resultSet = preparedStatement.executeQuery()) { + + resultSet.next(); + + return resultSet.getInt(1); + } catch (final SQLException e) { + throw new RuntimeException("Failed to run query '" + sql + "' against the view store", e); + } + } +} diff --git a/example-context/example-service/example-it/src/test/java/uk/gov/justice/services/example/cakeshop/it/helpers/PublishedEventCounter.java b/example-context/example-service/example-it/src/test/java/uk/gov/justice/services/example/cakeshop/it/helpers/PublishedEventCounter.java deleted file mode 100644 index abc6345e..00000000 --- a/example-context/example-service/example-it/src/test/java/uk/gov/justice/services/example/cakeshop/it/helpers/PublishedEventCounter.java +++ /dev/null @@ -1,48 +0,0 @@ -package uk.gov.justice.services.example.cakeshop.it.helpers; - -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; - -import javax.sql.DataSource; - -public class PublishedEventCounter { - - private final DataSource eventStoreDataSource; - - public PublishedEventCounter(final DataSource eventStoreDataSource) { - this.eventStoreDataSource = eventStoreDataSource; - } - - - public int countPublishedEvents() { - - final String sql = "SELECT COUNT (*) FROM published_event"; - - try(final Connection connection = eventStoreDataSource.getConnection(); - final PreparedStatement preparedStatement = connection.prepareStatement(sql); - final ResultSet resultSet = preparedStatement.executeQuery()) { - - resultSet.next(); - - return resultSet.getInt(1); - } catch (final SQLException e) { - throw new RuntimeException("Failed to run query '" + sql + "' against the event store", e); - } - } - - public void truncatePublishQueue() { - - final String sql = "DELETE FROM publish_queue"; - - try(final Connection connection = eventStoreDataSource.getConnection(); - final PreparedStatement preparedStatement = connection.prepareStatement(sql)) { - - preparedStatement.executeUpdate(); - - } catch (final SQLException e) { - throw new RuntimeException("Failed to run query '" + sql + "' against the event store", e); - } - } -} diff --git a/example-context/example-service/example-it/src/test/java/uk/gov/justice/services/example/cakeshop/it/helpers/RecipeTableInspector.java b/example-context/example-service/example-it/src/test/java/uk/gov/justice/services/example/cakeshop/it/helpers/RecipeTableInspector.java deleted file mode 100644 index 53e4ed53..00000000 --- a/example-context/example-service/example-it/src/test/java/uk/gov/justice/services/example/cakeshop/it/helpers/RecipeTableInspector.java +++ /dev/null @@ -1,88 +0,0 @@ -package uk.gov.justice.services.example.cakeshop.it.helpers; - - -import uk.gov.justice.services.example.cakeshop.persistence.entity.Recipe; - -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.List; -import java.util.UUID; - -import javax.sql.DataSource; - -public class RecipeTableInspector { - - private final DataSource viewStoreDataSource; - - public RecipeTableInspector(final DataSource viewStoreDataSource) { - this.viewStoreDataSource = viewStoreDataSource; - } - - public int countNumberOfRecipes() { - - final String sql = "SELECT COUNT (*) FROM recipe"; - - try(final Connection connection = viewStoreDataSource.getConnection(); - final PreparedStatement preparedStatement = connection.prepareStatement(sql); - final ResultSet resultSet = preparedStatement.executeQuery()) { - - resultSet.next(); - - return resultSet.getInt(1); - } catch (final SQLException e) { - throw new RuntimeException("Failed to run query '" + sql + "' against the view store", e); - } - } - - public List getAllRecipes() { - final String sql = "SELECT id, name, gluten_free, photo_id FROM recipe"; - - try(final Connection connection = viewStoreDataSource.getConnection(); - final PreparedStatement preparedStatement = connection.prepareStatement(sql); - final ResultSet resultSet = preparedStatement.executeQuery()) { - - - final List recipes = new ArrayList<>(); - while (resultSet.next()) { - final UUID id = (UUID) resultSet.getObject("id"); - final String name = resultSet.getString("name"); - final boolean glutenFree = resultSet.getBoolean("gluten_free"); - final UUID photoId = (UUID) resultSet.getObject("photo_id"); - - final Recipe recipe = new Recipe(id, name, glutenFree, photoId); - - recipes.add(recipe); - } - - return recipes; - } catch (final SQLException e) { - throw new RuntimeException("Failed to run query '" + sql + "' against the view store", e); - } - } - - public long countEventsPerStream(final UUID streamId) { - - final String sql = "SELECT position FROM stream_status WHERE stream_id = ? AND component = ?"; - - try(final Connection connection = viewStoreDataSource.getConnection(); - final PreparedStatement preparedStatement = connection.prepareStatement(sql)) { - - preparedStatement.setObject(1, streamId); - preparedStatement.setString(2, "EVENT_LISTENER"); - - try(final ResultSet resultSet = preparedStatement.executeQuery()) { - if(resultSet.next()) { - return resultSet.getLong(1); - } else { - return 0L; - } - } - - } catch (final SQLException e) { - throw new RuntimeException("Failed to run query '" + sql + "' against the event store", e); - } - } -} diff --git a/pom.xml b/pom.xml index 03839676..fbdfe235 100644 --- a/pom.xml +++ b/pom.xml @@ -30,13 +30,13 @@ cake-shop - + 4.0.0-M25 - 6.0.0-M35 - 2.0.0-M39 - 2.0.0-M27 + 6.0.0-M37 + 2.0.0-M41 + 2.0.0-M28 1.17.8 - + 2.0.2 1.19.0 1.23.0