-
Notifications
You must be signed in to change notification settings - Fork 24
Add a test with a component with async context switching #1726
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mikhailshilkov
wants to merge
1
commit into
main
Choose a base branch
from
mikhailshilkov/async-component-test
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
tests/integration/provider-maven/src/main/java/com/pulumi/example/provider/SampleAsync.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| package com.pulumi.example.provider; | ||
|
|
||
| import com.pulumi.core.annotations.Import; | ||
| import com.pulumi.core.annotations.Export; | ||
| import com.pulumi.core.Output; | ||
| import com.pulumi.random.RandomString; | ||
| import com.pulumi.random.RandomStringArgs; | ||
| import com.pulumi.resources.ComponentResource; | ||
| import com.pulumi.resources.ComponentResourceOptions; | ||
| import com.pulumi.resources.CustomResourceOptions; | ||
| import com.pulumi.resources.ResourceArgs; | ||
| import com.pulumi.std.inputs.AbsArgs; | ||
| import com.pulumi.std.StdFunctions; | ||
| import com.pulumi.std.inputs.AbsPlainArgs; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import java.net.http.HttpClient; | ||
| import java.net.http.HttpRequest; | ||
| import java.net.http.HttpResponse; | ||
| import java.net.URI; | ||
|
|
||
| class SampleAsyncArgs extends ResourceArgs { | ||
| @Import(name="length", required=true) | ||
| private Output<Integer> length; | ||
|
|
||
| public Output<Integer> length() { | ||
| return this.length; | ||
| } | ||
|
|
||
| private SampleAsyncArgs() {} | ||
|
|
||
| public SampleAsyncArgs(Output<Integer> length) { | ||
| this.length = length; | ||
| } | ||
| } | ||
|
|
||
| // This component is a test case for a number of async operations to make sure they work | ||
| // correctly for our context-aware completable future implementation. | ||
| class SampleAsync extends ComponentResource { | ||
| @Export(name="value", refs={String.class}, tree="[0]") | ||
| public final Output<String> value; | ||
|
|
||
| public SampleAsync(String name, SampleAsyncArgs args, ComponentResourceOptions opts) { | ||
| super("javap:index:SampleAsync", name, null, opts); | ||
|
|
||
| var resOpts = CustomResourceOptions.builder() | ||
| .parent(this) | ||
| .build(); | ||
|
|
||
| // First async operation for length | ||
| var asyncLength = Output.of(CompletableFuture.supplyAsync(() -> { | ||
| try { | ||
| Thread.sleep(2000); | ||
| return -5.0; | ||
| } catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| throw new RuntimeException("Async operation interrupted", e); | ||
| } | ||
| })); | ||
|
|
||
| // Invoke a remote function. | ||
| var absLength = StdFunctions.abs(AbsArgs.builder().input(asyncLength).build()); | ||
| var absLengthInt = absLength.applyValue(d -> d.result().intValue()); | ||
|
|
||
| // Invoke a remote function with a plain value. | ||
| var absPlainLength = StdFunctions.absPlain(AbsPlainArgs.builder().input(-1.0).build()); | ||
| var absPlainLengthInt = Output.of(absPlainLength).applyValue(d -> d.result().intValue()); | ||
|
|
||
| var totalLength = Output.tuple(args.length(), absLengthInt, absPlainLengthInt).applyValue(values -> values.t1 + values.t2 + values.t3); | ||
|
|
||
| // Create the random string with modified length. | ||
| var randomString = new RandomString(name + "1", | ||
| RandomStringArgs.builder() | ||
| .length(totalLength) | ||
| .special(false) | ||
| .build(), resOpts); | ||
|
|
||
|
|
||
| // Make HTTP request based on the input length | ||
| this.value = Output.tuple(randomString.result(), totalLength) | ||
| .applyValue(values -> { | ||
| String randomStr = values.t1; | ||
| Integer todoId = (values.t2 % 10) + 1; | ||
|
|
||
| var randomString2 = new RandomString(name + "2", | ||
| RandomStringArgs.builder() | ||
| .length(totalLength) | ||
| .special(false) | ||
| .build(), resOpts); | ||
|
|
||
| try { | ||
| HttpClient client = HttpClient.newHttpClient(); | ||
| HttpRequest request = HttpRequest.newBuilder() | ||
| .uri(URI.create("https://jsonplaceholder.typicode.com/todos/" + todoId)) | ||
| .build(); | ||
|
|
||
| HttpResponse<String> response = client.send(request, | ||
| HttpResponse.BodyHandlers.ofString()); | ||
| return randomStr + " - Todo #" + todoId + ": " + response.body(); | ||
| } catch (Exception e) { | ||
| throw new RuntimeException("HTTP request failed", e); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does Java have something like Go's test HTTP server? Or is there another way to simulate this asyncronicity? It's not ideal to make an actual HTTP request in our tests, especially to a site we don't control.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could do a Java http server but I don't think I have the right lifecycle to start/stop it...
We could do something fake like
but I'm not sure it catches all the right problems?
The TODO thing is specifically designed for this case: "Free fake and reliable API for testing and prototyping". I think we are relatively safe and get a real test with some networking and latency, which may affect the asynchronous behavior?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Every network call in the tests are a potential source for flakes/CI breakages, so I'd really rather not introduce the dependency. Even the most stable services tend to break sometimes, in which case CI is gonna be red.