Skip to content

Commit

Permalink
Add integration test for step.invoke (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
KiKoS0 authored Sep 11, 2024
1 parent 19a5515 commit 0d4e9c0
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.inngest.springbootdemo.testfunctions;

import com.inngest.*;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class MultiplyMatrixFunction extends InngestFunction {
@NotNull
@Override
public InngestFunctionConfigBuilder config(InngestFunctionConfigBuilder builder) {
return builder
.id("MultiplyMatrixFunction")
.name("Multiply Matrix")
.triggerEvent("test/multiply.matrix");
}


@Override
public List<List<Double>> execute(FunctionContext ctx, Step step) {
List<List<Integer>> A = (List<List<Integer>>) ctx.getEvent().getData().get("matrixA");
List<List<Integer>> B = (List<List<Integer>>) ctx.getEvent().getData().get("matrixB");

return step.run("multiply-matrix", () ->
{
List<List<Double>> result = new ArrayList<>();
for (List<Integer> integers : A) {
List<Double> row = new ArrayList<>();
for (int j = 0; j < B.get(0).size(); j++) {
double sum = 0;
for (int k = 0; k < integers.size(); k++) {
sum += integers.get(k) * B.get(k).get(j);
}
row.add(sum);
}
result.add(row);
}
return result;
}, List.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.inngest.springbootdemo.testfunctions;

import com.inngest.*;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;

public class Scale2DObjectFunction extends InngestFunction {
@NotNull
@Override
public InngestFunctionConfigBuilder config(InngestFunctionConfigBuilder builder) {
return builder
.id("Scale2DObjectFunction")
.name("Scale 2D Object")
.triggerEvent("test/scale2d.object");
}

@Override
public List<List<Double>> execute(FunctionContext ctx, Step step) {
List<List<Integer>> matrix = (List<List<Integer>>) ctx.getEvent().getData().get("matrix");
int scaleX = (int) ctx.getEvent().getData().get("scaleX");
int scaleY = (int) ctx.getEvent().getData().get("scaleY");

List<List<Integer>> scalingMatrix = new ArrayList<>(Arrays.asList(
new ArrayList<>(Arrays.asList(scaleX, 0)),
new ArrayList<>(Arrays.asList(0, scaleY))
));

LinkedHashMap<String, Object> eventData = new LinkedHashMap<String, Object>() {{
put("matrixA", matrix);
put("matrixB", scalingMatrix);
}};

return step.invoke(
"multiply-matrix",
"spring_test_demo",
"MultiplyMatrixFunction",
eventData,
null,
List.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ protected HashMap<String, InngestFunction> functions() {
addInngestFunction(functions, new DebouncedFunction());
addInngestFunction(functions, new PriorityFunction());
addInngestFunction(functions, new IdempotentFunction());
addInngestFunction(functions, new Scale2DObjectFunction());
addInngestFunction(functions, new MultiplyMatrixFunction());

return functions;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ static SendEventsResponse sendEvent(Inngest inngest, String eventName) {
return sendEvent(inngest, eventName, new HashMap());
}

static SendEventsResponse sendEvent(Inngest inngest, String eventName, Map<String, String> data) {
static SendEventsResponse sendEvent(Inngest inngest, String eventName, Map<String, Object> data) {
InngestEvent event = new InngestEvent(eventName, data);
SendEventsResponse response = inngest.send(event);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
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.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;

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

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

static int sleepTime = 5000;

@Autowired
private Inngest client;

@Test
void testShouldInvokeScaleObjectFunctionAndReturnCorrectResult() throws Exception {
ArrayList<ArrayList<Integer>> square = new ArrayList<>(Arrays.asList(
new ArrayList<>(Arrays.asList(1, 1)),
new ArrayList<>(Arrays.asList(1, 2)),
new ArrayList<>(Arrays.asList(2, 1)),
new ArrayList<>(Arrays.asList(2, 2))
));

LinkedHashMap<String, Object> eventData = new LinkedHashMap<String, Object>() {{
put("matrix", square);
put("scaleX", 2);
put("scaleY", 3);
}};

String eventId = InngestFunctionTestHelpers.sendEvent(client, "test/scale2d.object", eventData).getIds()[0];

Thread.sleep(sleepTime);

RunEntry<Object> run = devServer.runsByEvent(eventId).first();

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

assertEquals(new ArrayList<>(Arrays.asList(
new ArrayList<>(Arrays.asList(2, 3)),
new ArrayList<>(Arrays.asList(2, 6)),
new ArrayList<>(Arrays.asList(4, 3)),
new ArrayList<>(Arrays.asList(4, 6))
)), run.getOutput());
}
}

0 comments on commit 0d4e9c0

Please sign in to comment.