Skip to content

Commit

Permalink
Merge pull request #41612 from gabilang/fix-future-val-toString
Browse files Browse the repository at this point in the history
Fix NPE with 'toString' method of FutureValue
  • Loading branch information
warunalakshitha authored Nov 9, 2023
2 parents 024b80c + 91fa859 commit c5e3e0f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.ballerina.runtime.api.values.BTypedesc;
import io.ballerina.runtime.internal.scheduling.Strand;
import io.ballerina.runtime.internal.types.BFutureType;
import io.ballerina.runtime.internal.util.StringUtils;

import java.util.Map;
import java.util.StringJoiner;
Expand Down Expand Up @@ -68,7 +69,7 @@ public String stringValue(BLink parent) {
StringJoiner sj = new StringJoiner(",", "{", "}");
sj.add("isDone:" + isDone);
if (isDone) {
sj.add("result:" + result.toString());
sj.add("result:" + StringUtils.getStringVal(result, parent));
}
if (panic != null) {
sj.add("panic:" + panic.getLocalizedMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
package org.ballerinalang.test.types.string;

import org.ballerinalang.test.BCompileUtil;
import org.ballerinalang.test.BRunUtil;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

/**
Expand Down Expand Up @@ -78,6 +80,16 @@ public void testArrayValueToString() {
testAndAssert("testArrayValueToString", 30);
}

@Test(dataProvider = "functionsToTestToString")
public void testFutureValueToString(String functionName) {
BRunUtil.invoke(result, functionName);
}

@DataProvider(name = "functionsToTestToString")
public String[] toStringTestFunctions() {
return new String[]{"testFutureValueToString", "testFutureValueToStringWithNilReturn"};
}

@AfterClass
public void tearDown() {
result = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

import ballerina/lang.runtime;
import ballerina/lang.value;
import ballerina/test;

function testDecimalToString() returns int {
decimal res3 = 8;
Expand Down Expand Up @@ -74,3 +77,23 @@ function testArrayValueToString() returns int {
string[][] arr2 = [["h🤷llo", "h🤷llo", "h🤷llo"], ["h🤷llo", "h🤷llo", "h🤷llo"]];
return arr2[0].toString().length() + arr2[0][1].toString().length();
}

function testFutureValueToString() {
future<boolean> f = start isEven(2);
runtime:sleep(1);
string s = f.toString();
test:assertEquals(s, "future {isDone:true,result:true}");
}

function isEven(int n) returns boolean {
return n % 2 == 0;
}

function testFutureValueToStringWithNilReturn() {
future<()> f = start addName();
runtime:sleep(1);
string s = f.toString();
test:assertEquals(s, "future {isDone:true,result:}");
}

function addName() {}

0 comments on commit c5e3e0f

Please sign in to comment.