Skip to content

Commit 447bfa8

Browse files
Add a test with a component with async context switching
1 parent 139f29b commit 447bfa8

File tree

2 files changed

+107
-2
lines changed

2 files changed

+107
-2
lines changed

tests/integration/provider-maven/example/Pulumi.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ resources:
99
type: javap:index:HelloWorld
1010
properties:
1111
length: 12
12-
hello2:
13-
type: javap:index:HelloWorld
12+
sampleAsync:
13+
type: javap:index:SampleAsync
1414
properties:
1515
length: 22
1616
outputs:
1717
value: ${hello.value}
18+
sampleAsyncValue: ${sampleAsync.value}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package com.pulumi.example.provider;
2+
3+
import com.pulumi.core.annotations.Import;
4+
import com.pulumi.core.annotations.Export;
5+
import com.pulumi.core.Output;
6+
import com.pulumi.random.RandomString;
7+
import com.pulumi.random.RandomStringArgs;
8+
import com.pulumi.resources.ComponentResource;
9+
import com.pulumi.resources.ComponentResourceOptions;
10+
import com.pulumi.resources.CustomResourceOptions;
11+
import com.pulumi.resources.ResourceArgs;
12+
import com.pulumi.std.inputs.AbsArgs;
13+
import com.pulumi.std.StdFunctions;
14+
import com.pulumi.std.inputs.AbsPlainArgs;
15+
import java.util.concurrent.CompletableFuture;
16+
import java.net.http.HttpClient;
17+
import java.net.http.HttpRequest;
18+
import java.net.http.HttpResponse;
19+
import java.net.URI;
20+
21+
class SampleAsyncArgs extends ResourceArgs {
22+
@Import(name="length", required=true)
23+
private Output<Integer> length;
24+
25+
public Output<Integer> length() {
26+
return this.length;
27+
}
28+
29+
private SampleAsyncArgs() {}
30+
31+
public SampleAsyncArgs(Output<Integer> length) {
32+
this.length = length;
33+
}
34+
}
35+
36+
// This component is a test case for a number of async operations to make sure they work
37+
// correctly for our context-aware completable future implementation.
38+
class SampleAsync extends ComponentResource {
39+
@Export(name="value", refs={String.class}, tree="[0]")
40+
public final Output<String> value;
41+
42+
public SampleAsync(String name, SampleAsyncArgs args, ComponentResourceOptions opts) {
43+
super("javap:index:SampleAsync", name, null, opts);
44+
45+
var resOpts = CustomResourceOptions.builder()
46+
.parent(this)
47+
.build();
48+
49+
// First async operation for length
50+
var asyncLength = Output.of(CompletableFuture.supplyAsync(() -> {
51+
try {
52+
Thread.sleep(2000);
53+
return -5.0;
54+
} catch (InterruptedException e) {
55+
Thread.currentThread().interrupt();
56+
throw new RuntimeException("Async operation interrupted", e);
57+
}
58+
}));
59+
60+
// Invoke a remote function.
61+
var absLength = StdFunctions.abs(AbsArgs.builder().input(asyncLength).build());
62+
var absLengthInt = absLength.applyValue(d -> d.result().intValue());
63+
64+
// Invoke a remote function with a plain value.
65+
var absPlainLength = StdFunctions.absPlain(AbsPlainArgs.builder().input(-1.0).build());
66+
var absPlainLengthInt = Output.of(absPlainLength).applyValue(d -> d.result().intValue());
67+
68+
var totalLength = Output.tuple(args.length(), absLengthInt, absPlainLengthInt).applyValue(values -> values.t1 + values.t2 + values.t3);
69+
70+
// Create the random string with modified length.
71+
var randomString = new RandomString(name + "1",
72+
RandomStringArgs.builder()
73+
.length(totalLength)
74+
.special(false)
75+
.build(), resOpts);
76+
77+
78+
// Make HTTP request based on the input length
79+
this.value = Output.tuple(randomString.result(), totalLength)
80+
.applyValue(values -> {
81+
String randomStr = values.t1;
82+
Integer todoId = (values.t2 % 10) + 1;
83+
84+
var randomString2 = new RandomString(name + "2",
85+
RandomStringArgs.builder()
86+
.length(totalLength)
87+
.special(false)
88+
.build(), resOpts);
89+
90+
try {
91+
HttpClient client = HttpClient.newHttpClient();
92+
HttpRequest request = HttpRequest.newBuilder()
93+
.uri(URI.create("https://jsonplaceholder.typicode.com/todos/" + todoId))
94+
.build();
95+
96+
HttpResponse<String> response = client.send(request,
97+
HttpResponse.BodyHandlers.ofString());
98+
return randomStr + " - Todo #" + todoId + ": " + response.body();
99+
} catch (Exception e) {
100+
throw new RuntimeException("HTTP request failed", e);
101+
}
102+
});
103+
}
104+
}

0 commit comments

Comments
 (0)