-
Notifications
You must be signed in to change notification settings - Fork 118
feat(ffm): add support for async Swift functions via CompletableFuture #870
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
9ce9c74
5f18d62
f9bb3b9
2881074
1f98e18
013c345
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| description: | ||
| --- | ||
|
|
||
| Review the current staged changes and draft a Pull Request description. Before drafting, verify that the code includes both source and runtime tests. Check specifically if the Samples/FFM sample has been updated with runtime tests for any substantial new functionality. Also, verify that the supported feature list in the documentation has been updated. If any of these are missing, list them clearly as action items before generating the PR draft. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2026 Apple Inc. and the Swift.org project authors | ||
| // Licensed under Apache License v2.0 | ||
| // | ||
| // See LICENSE.txt for license information | ||
| // See CONTRIBUTORS.txt for the list of Swift.org project authors | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| package com.example.swift; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This directory should not have new files; please link into existing tests in samples. These will never run or be validated. This is explained by
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So you should symlink into the Samples/SwiftJavaExtractFFMSampleApp/src/test/java/com/example/swift/MySwiftLibraryTest.java and put the snippets there |
||
|
|
||
| import com.example.swift.MySwiftClass; | ||
| import com.example.swift.MySwiftLibrary; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.swift.swiftkit.core.AllocatingSwiftArena; | ||
| import org.swift.swiftkit.ffm.generated.SwiftJavaErrorException; | ||
|
|
||
| import java.time.Duration; | ||
| import java.util.Optional; | ||
| import java.util.OptionalDouble; | ||
| import java.util.OptionalInt; | ||
| import java.util.OptionalLong; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.concurrent.ExecutionException; | ||
| import java.util.concurrent.Future; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| public class AsyncTest { | ||
| @Test | ||
| void asyncSum() throws Exception { | ||
| // snippet.asyncUsageJava | ||
| Future<Long> future = MySwiftLibrary.asyncSum(10, 12); | ||
|
|
||
| Long result = future.get(); | ||
| assertEquals(22, result); | ||
| // snippet.end | ||
| } | ||
|
|
||
| @Test | ||
| void asyncSleep() throws Exception { | ||
| // snippet.asyncSleepUsageJava | ||
| Future<Void> future = MySwiftLibrary.asyncSleep(); | ||
| future.get(); | ||
| // snippet.end | ||
| } | ||
|
|
||
| @Test | ||
| void asyncCopy() throws Exception { | ||
| // snippet.asyncCopyUsageJava | ||
| try (var arena = AllocatingSwiftArena.ofConfined()) { | ||
| MySwiftClass obj = MySwiftClass.init(10, 5, arena); | ||
| Future<MySwiftClass> future = MySwiftLibrary.asyncCopy(obj, arena); | ||
|
|
||
| MySwiftClass result = future.get(); | ||
|
|
||
| assertEquals(10, result.getX()); | ||
| assertEquals(5, result.getY()); | ||
| } | ||
| // snippet.end | ||
| } | ||
|
|
||
| @Test | ||
| void asyncThrows() { | ||
| Future<Void> future = MySwiftLibrary.asyncThrows(); | ||
|
|
||
| ExecutionException ex = assertThrows(ExecutionException.class, future::get); | ||
|
|
||
| Throwable cause = ex.getCause(); | ||
| assertNotNull(cause); | ||
| assertEquals(SwiftJavaErrorException.class, cause.getClass()); | ||
| assertTrue(cause.getMessage().contains("swiftError")); | ||
| } | ||
|
|
||
| @Test | ||
| void asyncOptional() throws Exception { | ||
| Future<OptionalLong> future = MySwiftLibrary.asyncOptional(42); | ||
| assertEquals(OptionalLong.of(42), future.get()); | ||
| } | ||
|
|
||
| @Test | ||
| void asyncString() throws Exception { | ||
| Future<String> future = MySwiftLibrary.asyncString("hey"); | ||
| assertEquals("hey", future.get()); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.