Skip to content

Commit

Permalink
chore(ci): update to jdk 17 (#34)
Browse files Browse the repository at this point in the history
* update to jdk 17 without using any features

* re ignore test it fails in CI but not locally

* use some more modern java

* add missing imports

---------

Co-authored-by: Harry Waye <[email protected]>
  • Loading branch information
pauldambra and hazzadous authored Mar 17, 2023
1 parent cc38e4d commit 8f1e9a5
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 60 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ jobs:
- name: Checkout the repository
uses: actions/checkout@v3

- name: Set up JDK 8
- name: Set up JDK
uses: actions/setup-java@v3
with:
java-version: 8
java-version: 17
distribution: 'adopt'
server-id: ossrh
server-username: MAVEN_USERNAME
Expand Down
4 changes: 2 additions & 2 deletions posthog/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
</scm>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>

<dependencies>
Expand Down
8 changes: 4 additions & 4 deletions posthog/src/main/java/com/posthog/java/HttpSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
import okhttp3.Response;

public class HttpSender implements Sender {
private String apiKey;
private String host;
private OkHttpClient client;
private final String apiKey;
private final String host;
private final OkHttpClient client;
private int maxRetries;
private Duration initialRetryInterval;

Expand Down Expand Up @@ -140,7 +140,7 @@ public Boolean send(List<JSONObject> events) {
}

private String getRequestBody(List<JSONObject> events) {
JSONObject jsonObject = new JSONObject();
var jsonObject = new JSONObject();
try {
jsonObject.put("api_key", apiKey);
jsonObject.put("batch", events);
Expand Down
12 changes: 6 additions & 6 deletions posthog/src/main/java/com/posthog/java/PostHog.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.json.JSONObject;

public class PostHog {
private QueueManager queueManager;
private final QueueManager queueManager;
private Thread queueManagerThread;

private static abstract class BuilderBase {
Expand Down Expand Up @@ -107,7 +107,7 @@ public void capture(String distinctId, String event) {
* set without overwriting previous values.
*/
public void identify(String distinctId, Map<String, Object> properties, Map<String, Object> propertiesSetOnce) {
Map<String, Object> props = new HashMap<String, Object>();
var props = new HashMap<String, Object>();
if (properties != null) {
props.put("$set", properties);
}
Expand Down Expand Up @@ -137,7 +137,7 @@ public void identify(String distinctId, Map<String, Object> properties) {
* overriden.
*/
public void alias(String distinctId, String alias) {
Map<String, Object> props = new HashMap<String, Object>() {
var props = new HashMap<String, Object>() {
{
put("distinct_id", distinctId);
put("alias", alias);
Expand All @@ -153,7 +153,7 @@ public void alias(String distinctId, String alias) {
* @param properties an array with any person properties you'd like to set.
*/
public void set(String distinctId, Map<String, Object> properties) {
Map<String, Object> props = new HashMap<String, Object>() {
var props = new HashMap<String, Object>() {
{
put("$set", properties);
}
Expand All @@ -169,7 +169,7 @@ public void set(String distinctId, Map<String, Object> properties) {
* Previous values will not be overwritten.
*/
public void setOnce(String distinctId, Map<String, Object> properties) {
Map<String, Object> props = new HashMap<String, Object>() {
var props = new HashMap<String, Object>() {
{
put("$set_once", properties);
}
Expand All @@ -178,7 +178,7 @@ public void setOnce(String distinctId, Map<String, Object> properties) {
}

private JSONObject getEventJson(String event, String distinctId, Map<String, Object> properties) {
JSONObject eventJson = new JSONObject();
var eventJson = new JSONObject();
try {
// Ensure that we generate an identifier for this event such that we can e.g.
// deduplicate server-side any duplicates we may receive.
Expand Down
20 changes: 10 additions & 10 deletions posthog/src/main/java/com/posthog/java/QueueManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
import org.json.JSONObject;

public class QueueManager implements Runnable {
class QueuePtr {
static class QueuePtr {
// TODO: not sure if there's a datastructure in Java that gives me these
// operations efficiently, specifically retrieveAndReset
// Not sure if LinkedList and JSONObject are good choices here
public List<JSONObject> ptr = new LinkedList<JSONObject>();
public List<JSONObject> ptr = new LinkedList<>();

public QueuePtr() {
};
}

public synchronized int size() {
return ptr.size();
Expand All @@ -35,19 +35,19 @@ public synchronized List<JSONObject> retrieveAndReset() {
return Collections.emptyList();
}
List<JSONObject> cur = ptr;
ptr = new LinkedList<JSONObject>();
ptr = new LinkedList<>();
return cur;
}
}

private QueuePtr queue = new QueuePtr();
private final QueuePtr queue = new QueuePtr();
private volatile boolean stop = false;
private Instant sendAfter;
// builder inputs
private Sender sender;
private int maxQueueSize;
private Duration maxTimeInQueue;
private int sleepMs;
private final Sender sender;
private final int maxQueueSize;
private final Duration maxTimeInQueue;
private final int sleepMs;

public static class Builder {
// required
Expand Down Expand Up @@ -102,7 +102,7 @@ public int queueSize() {
}

public void sendAll() {
List<JSONObject> toSend = queue.retrieveAndReset();
var toSend = queue.retrieveAndReset();
updateSendAfter(); // after queue reset but before sending as the latter could take a long time
if (!toSend.isEmpty()) {
sender.send(toSend);
Expand Down
33 changes: 16 additions & 17 deletions posthog/src/test/java/com/posthog/java/HttpSenderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,20 @@

import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;

public class HttpSenderTest {

public MockWebServer mockWebServer;
private HttpSender sender;
private String apiKey = "UNIT_TESTING_API_KEY";

@Before
public void setUp() throws IOException {
mockWebServer = new MockWebServer();
mockWebServer.start();

String httpUrl = mockWebServer.url("").toString();
String host = httpUrl.substring(0, httpUrl.length() - 1); // strip trailing /
var httpUrl = mockWebServer.url("").toString();
var host = httpUrl.substring(0, httpUrl.length() - 1); // strip trailing /
var apiKey = "UNIT_TESTING_API_KEY";
sender = new HttpSender.Builder(apiKey).host(host).maxRetries(1).build();
}

Expand All @@ -47,11 +46,11 @@ public void testEmpty() {
@Test
public void testOneItem() throws InterruptedException {
mockWebServer.enqueue(new MockResponse());
JSONObject json = new JSONObject("{'key': 'value'}");
List<JSONObject> input = new ArrayList<JSONObject>();
var json = new JSONObject("{'key': 'value'}");
var input = new ArrayList<JSONObject>();
input.add(json);
sender.send(input);
RecordedRequest request = mockWebServer.takeRequest();
var request = mockWebServer.takeRequest();
assertEquals("/batch", request.getPath());
assertThatJson("{\"api_key\":\"UNIT_TESTING_API_KEY\",\"batch\":[{\"key\":\"value\"}]}")
.isEqualTo(request.getBody().readUtf8());
Expand All @@ -60,15 +59,15 @@ public void testOneItem() throws InterruptedException {
@Test
public void testMultipleItems() throws InterruptedException {
mockWebServer.enqueue(new MockResponse());
JSONObject json = new JSONObject("{'key': 'value'}");
JSONObject json2 = new JSONObject("{'key2': 'value2'}");
JSONObject json3 = new JSONObject("{'key3': 'value3'}");
var json = new JSONObject("{'key': 'value'}");
var json2 = new JSONObject("{'key2': 'value2'}");
var json3 = new JSONObject("{'key3': 'value3'}");
List<JSONObject> input = new ArrayList<JSONObject>();
input.add(json);
input.add(json2);
input.add(json3);
sender.send(input);
RecordedRequest request = mockWebServer.takeRequest();
var request = mockWebServer.takeRequest();
assertEquals("/batch", request.getPath());
assertThatJson("{\"api_key\":\"UNIT_TESTING_API_KEY\",\"batch\":"
+ "[{\"key\":\"value\"},{\"key2\":\"value2\"},{\"key3\":\"value3\"}]}")
Expand Down Expand Up @@ -98,9 +97,9 @@ public void testOnlyMakesOneRequestOnSuccess() throws InterruptedException {
assertEquals(success, true);

// Now verify that we only
RecordedRequest firstRequest = mockWebServer.takeRequest(0, TimeUnit.MILLISECONDS);
var firstRequest = mockWebServer.takeRequest(0, TimeUnit.MILLISECONDS);
assertEquals(firstRequest.getPath(), "/batch");
RecordedRequest secondRequest = mockWebServer.takeRequest(0, TimeUnit.MILLISECONDS);
var secondRequest = mockWebServer.takeRequest(0, TimeUnit.MILLISECONDS);
assertEquals(secondRequest, null);
}

Expand All @@ -116,9 +115,9 @@ public void testDoesNotRetryOnClientErrors() throws InterruptedException {
assertEquals(success, false);

// Now verify that we only
RecordedRequest firstRequest = mockWebServer.takeRequest(0, TimeUnit.MILLISECONDS);
var firstRequest = mockWebServer.takeRequest(0, TimeUnit.MILLISECONDS);
assertEquals(firstRequest.getPath(), "/batch");
RecordedRequest secondRequest = mockWebServer.takeRequest(0, TimeUnit.MILLISECONDS);
var secondRequest = mockWebServer.takeRequest(0, TimeUnit.MILLISECONDS);
assertEquals(secondRequest, null);
}

Expand All @@ -134,9 +133,9 @@ public void testReturnFalseOnRetriesExhausted() throws InterruptedException {
assertEquals(success, false);

// Verify we made two requests
RecordedRequest firstRequest = mockWebServer.takeRequest(0, TimeUnit.MILLISECONDS);
var firstRequest = mockWebServer.takeRequest(0, TimeUnit.MILLISECONDS);
assertEquals(firstRequest.getPath(), "/batch");
RecordedRequest secondRequest = mockWebServer.takeRequest(0, TimeUnit.MILLISECONDS);
var secondRequest = mockWebServer.takeRequest(0, TimeUnit.MILLISECONDS);
assertEquals(secondRequest.getPath(), "/batch");
}
}
36 changes: 18 additions & 18 deletions posthog/src/test/java/com/posthog/java/PostHogTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void testCaptureSimple() {
ph.shutdown();
assertEquals(1, sender.calls.size());
assertEquals(1, sender.calls.get(0).size());
JSONObject json = sender.calls.get(0).get(0);
var json = sender.calls.get(0).get(0);
// Assert JSON includes the expected distinct_id, event, and timestamp, ignoring
// any extraneus properties.
assertThatJson("{\"distinct_id\":\"test id\",\"event\":\"test event\",\"timestamp\":\"" + instantExpected
Expand Down Expand Up @@ -85,7 +85,7 @@ public void testEnsureEventHasGeneratedUuid() {

@Test
public void testCaptureWithProperties() {
ph.capture("test id", "test event", new HashMap<String, Object>() {
ph.capture("test id", "test event", new HashMap<>() {
{
put("movie_id", 123);
put("category", "romcom");
Expand All @@ -94,15 +94,15 @@ public void testCaptureWithProperties() {
ph.shutdown();
assertEquals(1, sender.calls.size());
assertEquals(1, sender.calls.get(0).size());
JSONObject json = sender.calls.get(0).get(0);
var json = sender.calls.get(0).get(0);
assertThatJson("{\"distinct_id\":\"test id\",\"event\":\"test event\""
+ ",\"properties\":{\"movie_id\":123,\"category\":\"romcom\"},\"timestamp\":\"" + instantExpected
+ "\"}").isEqualTo(new JSONObject(json, "distinct_id", "event", "properties", "timestamp").toString());
}

@Test
public void testIdentifySimple() {
ph.identify("test id", new HashMap<String, Object>() {
ph.identify("test id", new HashMap<>() {
{
put("email", "[email protected]");
put("proUser", false);
Expand All @@ -111,7 +111,7 @@ public void testIdentifySimple() {
ph.shutdown();
assertEquals(1, sender.calls.size());
assertEquals(1, sender.calls.get(0).size());
JSONObject json = sender.calls.get(0).get(0);
var json = sender.calls.get(0).get(0);
assertThatJson("{\"distinct_id\":\"test id\",\"event\":\"$identify\""
+ ",\"properties\":{\"$set\":{\"email\":\"[email protected]\",\"proUser\":false}},\"timestamp\":\""
+ instantExpected + "\"}")
Expand All @@ -120,12 +120,12 @@ public void testIdentifySimple() {

@Test
public void testIdentifyWithSetOnce() {
ph.identify("test id", new HashMap<String, Object>() {
ph.identify("test id", new HashMap<>() {
{
put("email", "[email protected]");
put("proUser", false);
}
}, new HashMap<String, Object>() {
}, new HashMap<>() {
{
put("first_location", "colorado");
put("first_number", 5);
Expand All @@ -134,7 +134,7 @@ public void testIdentifyWithSetOnce() {
ph.shutdown();
assertEquals(1, sender.calls.size());
assertEquals(1, sender.calls.get(0).size());
JSONObject json = sender.calls.get(0).get(0);
var json = sender.calls.get(0).get(0);
assertThatJson("{\"distinct_id\":\"test id\",\"event\":\"$identify\""
+ ",\"properties\":{\"$set\":{\"email\":\"[email protected]\",\"proUser\":false}"
+ ",\"$set_once\":{\"first_location\":\"colorado\",\"first_number\":5}" + "},\"timestamp\":\""
Expand All @@ -145,7 +145,7 @@ public void testIdentifyWithSetOnce() {

@Test
public void testSet() {
ph.set("test id", new HashMap<String, Object>() {
ph.set("test id", new HashMap<>() {
{
put("email", "[email protected]");
put("proUser", false);
Expand All @@ -154,7 +154,7 @@ public void testSet() {
ph.shutdown();
assertEquals(1, sender.calls.size());
assertEquals(1, sender.calls.get(0).size());
JSONObject json = sender.calls.get(0).get(0);
var json = sender.calls.get(0).get(0);
assertThatJson("{\"distinct_id\":\"test id\",\"event\":\"$set\""
+ ",\"properties\":{\"$set\":{\"email\":\"[email protected]\",\"proUser\":false}},\"timestamp\":\""
+ instantExpected + "\"}")
Expand All @@ -163,7 +163,7 @@ public void testSet() {

@Test
public void testSetOnce() {
ph.setOnce("test id", new HashMap<String, Object>() {
ph.setOnce("test id", new HashMap<>() {
{
put("first_location", "colorado");
put("first_number", 5);
Expand All @@ -172,7 +172,7 @@ public void testSetOnce() {
ph.shutdown();
assertEquals(1, sender.calls.size());
assertEquals(1, sender.calls.get(0).size());
JSONObject json = sender.calls.get(0).get(0);
var json = sender.calls.get(0).get(0);
assertThatJson("{\"distinct_id\":\"test id\",\"event\":\"$set_once\""
+ ",\"properties\":{\"$set_once\":{\"first_location\":\"colorado\",\"first_number\":5}"
+ "},\"timestamp\":\"" + instantExpected + "\"}")
Expand All @@ -185,7 +185,7 @@ public void testAlias() {
ph.shutdown();
assertEquals(1, sender.calls.size());
assertEquals(1, sender.calls.get(0).size());
JSONObject json = sender.calls.get(0).get(0);
var json = sender.calls.get(0).get(0);
assertThatJson("{\"distinct_id\":\"test id\",\"event\":\"$create_alias\""
+ ",\"properties\":{\"distinct_id\":\"test id\",\"alias\":\"second id\"}" + ",\"timestamp\":\""
+ instantExpected + "\"}")
Expand Down Expand Up @@ -219,7 +219,7 @@ public void testQueueSize3() throws InterruptedException {
assertEquals(2, sender.calls.size());
assertEquals(3, sender.calls.get(0).size());
assertEquals(1, sender.calls.get(1).size());
JSONObject json = sender.calls.get(0).get(0);
var json = sender.calls.get(0).get(0);
assertThatJson(
"{\"distinct_id\":\"id1\",\"event\":\"first batch event\",\"timestamp\":\"" + instantExpected + "\"}")
.isEqualTo(new JSONObject(json, "distinct_id", "event", "timestamp").toString());
Expand All @@ -246,9 +246,9 @@ public void testMaxTimeInQueue() throws InterruptedException {
queueManager = new QueueManager.Builder(sender).sleepMs(0).maxTimeInQueue(Duration.ofDays(3))
.maxQueueSize(10000).build();
ph = new PostHog.BuilderWithCustomQueueManager(queueManager).build();
String originalInstant = "2020-02-02T02:02:02Z";
String secondInstant = "2020-02-03T02:02:02Z";
String thirdInstant = "2020-02-09T02:02:02Z";
var originalInstant = "2020-02-02T02:02:02Z";
var secondInstant = "2020-02-03T02:02:02Z";
var thirdInstant = "2020-02-09T02:02:02Z";
updateInstantNow(originalInstant);
ph.capture("id1", "first batch event");
updateInstantNow(secondInstant);
Expand All @@ -260,7 +260,7 @@ public void testMaxTimeInQueue() throws InterruptedException {
assertEquals(2, sender.calls.size());
assertEquals(2, sender.calls.get(0).size());
assertEquals(1, sender.calls.get(1).size());
JSONObject json = sender.calls.get(0).get(0);
var json = sender.calls.get(0).get(0);
assertThatJson(
"{\"distinct_id\":\"id1\",\"event\":\"first batch event\",\"timestamp\":\"" + originalInstant + "\"}")
.isEqualTo(new JSONObject(json, "distinct_id", "event", "timestamp").toString());
Expand Down
Loading

0 comments on commit 8f1e9a5

Please sign in to comment.