Skip to content

Commit

Permalink
chore: Java tests for catch<Any>
Browse files Browse the repository at this point in the history
fixes: #2427
  • Loading branch information
stuartwdouglas committed Aug 19, 2024
1 parent a66e555 commit dbfe253
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 18 deletions.
5 changes: 2 additions & 3 deletions backend/controller/pubsub/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,7 @@ func TestRetry(t *testing.T) {
1),

// check that there was one successful attempt to catchAny
// CatchRequest<Any> is not supported in java yet
in.IfLanguage("go", in.QueryRow("ftl",
in.QueryRow("ftl",
fmt.Sprintf(`
SELECT COUNT(*)
FROM async_calls
Expand All @@ -152,7 +151,7 @@ func TestRetry(t *testing.T) {
AND catching = true
AND origin = '%s'
`, dal.AsyncOriginPubSub{Subscription: schema.RefKey{Module: "subscriber", Name: "doomedSubscription2"}}.String()),
1)),
1),
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

public class Publisher {


@Export
@TopicDefinition("testTopic")
interface TestTopic extends Topic<PubSubEvent> {
Expand All @@ -32,9 +31,9 @@ void publishTen(TestTopic testTopic) throws Exception {

@Verb
void publishOne(TestTopic testTopic) throws Exception {
var t = java.time.ZonedDateTime.now();
Log.infof("Publishing %s", t);
testTopic.publish(new PubSubEvent().setTime(t));
var t = java.time.ZonedDateTime.now();
Log.infof("Publishing %s", t);
testTopic.publish(new PubSubEvent().setTime(t));
}

@Verb
Expand All @@ -44,4 +43,3 @@ void publishOneToTopic2(Topic2 topic2) throws Exception {
topic2.publish(new PubSubEvent().setTime(t));
}
}

Original file line number Diff line number Diff line change
@@ -1,36 +1,41 @@
package xyz.block.ftl.java.test.subscriber;

import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import ftl.builtin.CatchRequest;
import ftl.publisher.PubSubEvent;
import ftl.publisher.TestTopicSubscription;
import ftl.publisher.Topic2Subscription;
import io.quarkus.logging.Log;
import xyz.block.ftl.Retry;
import xyz.block.ftl.Subscription;
import xyz.block.ftl.TopicDefinition;
import xyz.block.ftl.Verb;
import xyz.block.ftl.VerbName;

import java.util.concurrent.atomic.AtomicInteger;

public class Subscriber {

private static final AtomicInteger catchCount = new AtomicInteger();

@TestTopicSubscription
void consume(PubSubEvent event) throws Exception {
Log.infof("Subscriber is consuming %s", event.getTime());
}

@Subscription(
topic = "topic2",
module = "publisher",
name = "doomedSubscription"
)
@Subscription(topic = "topic2", module = "publisher", name = "doomedSubscription")
@Retry(count = 2, minBackoff = "1s", maxBackoff = "1s", catchVerb = "catch")
public void consumeButFailAndRetry(PubSubEvent event) {
throw new RuntimeException("always error: event " + event.getTime());
}

@Subscription(topic = "topic2", module = "publisher", name = "doomedSubscription2")
@Retry(count = 1, minBackoff = "1s", maxBackoff = "1s", catchVerb = "catchAny")
public void consumeButFailAndCatchAny(PubSubEvent event) {
throw new RuntimeException("always error: event " + event.getTime());
}

@Verb
@VerbName("catch")
public void catchVerb(CatchRequest<PubSubEvent> req) {
Expand All @@ -41,5 +46,30 @@ public void catchVerb(CatchRequest<PubSubEvent> req) {
throw new RuntimeException("catching error");
}
}
}

@Verb
public void catchAny(CatchRequest<Object> req) {
if (!"subscriber".equals(req.getVerb().getModule())) {
throw new IllegalArgumentException(String.format("unexpected verb module: %s", req.getVerb().getModule()));
}
if (!"consumeButFailAndCatchAny".equals(req.getVerb().getName())) {
throw new IllegalArgumentException(String.format("unexpected verb name: %s", req.getVerb().getName()));
}
if (!"publisher.PubSubEvent".equals(req.getRequestType())) {
throw new IllegalArgumentException(String.format("unexpected request type: %s", req.getRequestType()));
}
if (!(req.getRequest()instanceof Map<?,?>)) {
throw new IllegalArgumentException(
String.format("expected request to be a Map: %s", req.getRequest().getClass().getName()));
}
var request = (Map<?, ?>) req.getRequest();
var time = request.get("time");
if (time == null) {
throw new IllegalArgumentException("expected request to have a time key");
}
if (!(time instanceof String)) {
throw new IllegalArgumentException("expected request to have a time value of type string");
}
}

}

0 comments on commit dbfe253

Please sign in to comment.