-
-
Notifications
You must be signed in to change notification settings - Fork 109
Unit Tests for Metrics #1439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Unit Tests for Metrics #1439
Changes from all commits
c75dc22
7ba857d
29bc42e
7b7a676
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,6 +44,26 @@ public void count(String event) { | |
| count(event, Map.of()); | ||
| } | ||
|
|
||
| /** | ||
| * Track an event execution with dimensions provided. | ||
| * | ||
| * @param event the event to save | ||
| * @param dimensions the dimensions to save | ||
| */ | ||
| public void count(String event, Map<String, Object> dimensions) { | ||
| count(event, dimensions, true); | ||
| } | ||
|
|
||
| /** | ||
| * Track an event execution with flag for async execution. | ||
| * | ||
| * @param event the event to save | ||
| * @param doAsync the async flag | ||
| */ | ||
| public void count(String event, boolean doAsync) { | ||
| count(event, Map.of(), doAsync); | ||
| } | ||
|
|
||
|
Comment on lines
+57
to
+66
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove that, we dont want to expose that
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what do you mean by ??
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (talked about it in discord) |
||
| /** | ||
| * Track an event execution with additional contextual data. | ||
| * | ||
|
|
@@ -54,14 +74,19 @@ public void count(String event) { | |
| * and analyzing events later. Note: A value for a metric should be a Java primitive | ||
| * (String, int, double, long float). | ||
| */ | ||
| public void count(String event, Map<String, Object> dimensions) { | ||
| void count(String event, Map<String, Object> dimensions, boolean doAsync) { | ||
| logger.debug("Counting new record for event: {}", event); | ||
|
|
||
| Instant happenedAt = Instant.now(); | ||
| String serializedDimensions = serializeDimensions(dimensions); | ||
| String serializedDimensions = dimensions.isEmpty() ? null : serializeDimensions(dimensions); | ||
|
|
||
| Runnable task = () -> processEvent(event, happenedAt, serializedDimensions); | ||
|
|
||
| service.submit(() -> processEvent(event, happenedAt, | ||
| dimensions.isEmpty() ? null : serializedDimensions)); | ||
| if (doAsync) { | ||
| service.submit(task); | ||
| } else { | ||
| task.run(); | ||
| } | ||
| } | ||
|
|
||
| private static String serializeDimensions(Map<String, Object> dimensions) { | ||
|
|
@@ -72,12 +97,6 @@ private static String serializeDimensions(Map<String, Object> dimensions) { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * | ||
| * @param event the event to save | ||
| * @param happenedAt the moment when the event is dispatched | ||
| * @param dimensionsJson optional JSON-serialized dimensions, or null | ||
| */ | ||
|
Comment on lines
-75
to
-80
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dont remove that
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok and I'll add a description |
||
| private void processEvent(String event, Instant happenedAt, @Nullable String dimensionsJson) { | ||
| database.write(context -> context.newRecord(MetricEvents.METRIC_EVENTS) | ||
| .setEvent(event) | ||
|
|
@@ -86,4 +105,14 @@ private void processEvent(String event, Instant happenedAt, @Nullable String dim | |
| .insert()); | ||
| } | ||
|
|
||
| /** | ||
| * Exposes the underlying executor service. | ||
| * <p> | ||
| * Intended for test teardown only. | ||
| * | ||
| * @return the executor service backing this instance | ||
| */ | ||
| public ExecutorService getExecutorService() { | ||
| return service; | ||
| } | ||
|
Comment on lines
+115
to
+117
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. undo, this won't be needed (see other comment why)
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (not done yet) |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package org.togetherjava.tjbot.features; | ||
|
|
||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import org.togetherjava.tjbot.db.Database; | ||
| import org.togetherjava.tjbot.db.generated.tables.MetricEvents; | ||
| import org.togetherjava.tjbot.db.generated.tables.records.MetricEventsRecord; | ||
| import org.togetherjava.tjbot.features.analytics.Metrics; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
|
|
||
| final class MetricsTests { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this test is too much in terms of complexity for no tangible benefit. lets step back and create something simpler that beginners can understand and maintain while providing pretty much the same test-safety in terms of relevant coverage.
@Test
void basicEventCount() {
// GIVEN a test event
String expectedEvent = "test";
Instant expectedHappenedAt = Instant.now();
// WHEN counting the event
metrics.countBlocking(expectedEvent);
// THEN the event was saved in the DB
Foo entry = database...
String actualEvent = entry.event();
Instant actualHappenedAt = entry.happenedAt();
assertEquals(expectedEvent, actualEvent);
assertCloseEnough(expectedHappenedAt, actualHappenedAt);
}
private static void assertCloseEnough(Instant expected, Instant actual) {
... // assert that its within 1min difference
} (double check the correct order of expected vs actual in assert, i never can remember it, lol)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the guide. However, i don't see this a good idea: creating new method inside the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is not meaningful to test the true Please do it, thanks.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (not done yet)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we have to create an assertion method
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (talked about it in discord) |
||
| private Database database; | ||
| private Metrics metrics; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| database = Database.createMemoryDatabase(MetricEvents.METRIC_EVENTS); | ||
| metrics = new Metrics(database); | ||
| } | ||
|
|
||
| @AfterEach | ||
| void tearDown() { | ||
| metrics.getExecutorService().shutdownNow(); | ||
| } | ||
|
|
||
| @Test | ||
| void countWithDoAsyncFalsePersists() { | ||
|
|
||
| String testEvent = "metrics_test_event"; | ||
|
|
||
| metrics.count(testEvent, false); | ||
|
|
||
| MetricEventsRecord savedRecord = | ||
| database.read(context -> context.selectFrom(MetricEvents.METRIC_EVENTS).fetchOne()); | ||
|
|
||
| assertNotNull(savedRecord); | ||
|
|
||
| assertEquals(testEvent, savedRecord.get(MetricEvents.METRIC_EVENTS.EVENT)); | ||
| assertNull(savedRecord.get(MetricEvents.METRIC_EVENTS.DIMENSIONS)); | ||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please dont change the javadoc, it was this:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was absolutely no javadoc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was. The method that was previously public facing is now not public facing anymore and the method you made public facing has a different, less detailled, javadoc:
