Skip to content

Commit

Permalink
Add integration test for the general case of deserializing a subclass
Browse files Browse the repository at this point in the history
This shows that the issue is not limited to StepError/Exception.
  • Loading branch information
albertchae committed Nov 7, 2024
1 parent fc3c642 commit e42333f
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.inngest.springbootdemo.testfunctions;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.inngest.FunctionContext;
import com.inngest.InngestFunction;
import com.inngest.InngestFunctionConfigBuilder;
import com.inngest.Step;
import org.jetbrains.annotations.NotNull;

class Dog {
@JsonProperty("legs")
public int legs;

public Dog(@JsonProperty("legs") int legs) {
this.legs = legs;
}
}

class Corgi extends Dog {
@JsonProperty("stumpy")
public boolean stumpy;

public Corgi(@JsonProperty("legs") int legs, @JsonProperty("stumpy") boolean stumpy) {
super(legs);

this.stumpy = stumpy;
}
}

public class DeserializeSubclassFunction extends InngestFunction {
@NotNull
@Override
public InngestFunctionConfigBuilder config(InngestFunctionConfigBuilder builder) {
return builder
.id("DeserializeSubclassFunction")
.name("Deserialize subclass function")
.triggerEvent("test/deserialize.subclass")
.retries(0);
}

@Override
public String execute(FunctionContext ctx, Step step) {
Dog corgi = step.run("get-corgi", () -> new Corgi(4, true), Dog.class);

assert(((Corgi) corgi).stumpy == true);

return "Successfully cast Corgi";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ protected HashMap<String, InngestFunction> functions() {
addInngestFunction(functions, new RetriableErrorFunction());
addInngestFunction(functions, new ZeroRetriesFunction());
addInngestFunction(functions, new InvokeFailureFunction());
addInngestFunction(functions, new DeserializeSubclassFunction());
addInngestFunction(functions, new TryCatchGenericExceptionFunction());
addInngestFunction(functions, new TryCatchRunFunction());
addInngestFunction(functions, new ThrottledFunction());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.inngest.springbootdemo;

import com.inngest.Inngest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.LinkedHashMap;

import static org.junit.jupiter.api.Assertions.*;

@IntegrationTest
@Execution(ExecutionMode.CONCURRENT)
class DeserializationIntegrationTest {
@Autowired
private DevServerComponent devServer;

static int sleepTime = 5000;

@Autowired
private Inngest client;

@Test
void testShouldDeserializeSubclassCorrectly() throws Exception {
String eventId = InngestFunctionTestHelpers.sendEvent(client, "test/deserialize.subclass").getIds()[0];

Thread.sleep(sleepTime);

RunEntry<Object> run = devServer.runsByEvent(eventId).first();
Object output = run.getOutput();
if (output instanceof LinkedHashMap) {
fail("Run threw an exception serialized into a LinkedHashMap:" + output);
}
String outputString = (String) output;

assertEquals("Completed", run.getStatus() );
assertNotNull(run.getEnded_at());

assertEquals("Successfully cast Corgi", outputString);
}
}

0 comments on commit e42333f

Please sign in to comment.