From 28aaa05e8248d016b05e7535cd4d27a78697b509 Mon Sep 17 00:00:00 2001 From: hindujaB Date: Wed, 4 Oct 2023 13:03:52 +0530 Subject: [PATCH 1/3] Fix getType usage for stop handler --- .../runtime/internal/scheduling/RuntimeRegistry.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/scheduling/RuntimeRegistry.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/scheduling/RuntimeRegistry.java index 1a3ec5d602ca..9ce89176033f 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/scheduling/RuntimeRegistry.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/scheduling/RuntimeRegistry.java @@ -17,6 +17,7 @@ package io.ballerina.runtime.internal.scheduling; import io.ballerina.runtime.api.async.Callback; +import io.ballerina.runtime.api.utils.TypeUtils; import io.ballerina.runtime.api.values.BError; import io.ballerina.runtime.api.values.BFunctionPointer; import io.ballerina.runtime.api.values.BObject; @@ -91,7 +92,8 @@ private synchronized void invokeStopHandlerFunction(Strand strand, Scheduler sch BFunctionPointer bFunctionPointer = stopHandlerStack.pop(); StopHandlerCallback callback = new StopHandlerCallback(strand, scheduler); final FutureValue future = scheduler.createFuture(strand, callback, null, - ((BFunctionType) bFunctionPointer.getType()).retType, null, strand.getMetadata()); + ((BFunctionType) TypeUtils.getImpliedType(bFunctionPointer.getType())).retType, null, + strand.getMetadata()); scheduler.scheduleLocal(new Object[]{strand}, bFunctionPointer, strand, future); } From 1d25cad088d1aecbc4a1f990b29dc62a1264cec2 Mon Sep 17 00:00:00 2001 From: hindujaB Date: Wed, 4 Oct 2023 13:04:35 +0530 Subject: [PATCH 2/3] Add unit test --- .../test-src/runtime/api/stop_handler/main.bal | 18 ++++++++++++------ .../api/stop_handler/modules/moduleA/main.bal | 3 +-- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/runtime/api/stop_handler/main.bal b/tests/jballerina-unit-test/src/test/resources/test-src/runtime/api/stop_handler/main.bal index 5660560faa34..c789f0be1303 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/runtime/api/stop_handler/main.bal +++ b/tests/jballerina-unit-test/src/test/resources/test-src/runtime/api/stop_handler/main.bal @@ -20,26 +20,27 @@ import ballerina/test; import stop_handler.moduleA; function stopHandlerFunc1() returns error? { - runtime:sleep(1); + runtime:sleep(2.5); moduleA:incrementCount(); - moduleA:assertCount(10); + moduleA:assertCount(11); } function stopHandlerFunc2() returns error? { + runtime:sleep(1); moduleA:incrementCount(); - moduleA:assertCount(9); + moduleA:assertCount(10); } function stopHandlerFunc3() returns error? { runtime:sleep(0.5); moduleA:incrementCount(); - moduleA:assertCount(7); + moduleA:assertCount(8); } function stopHandlerFunc4() returns error? { runtime:sleep(1.5); moduleA:incrementCount(); - moduleA:assertCount(8); + moduleA:assertCount(9); } function init() { @@ -57,7 +58,12 @@ public function main() { moduleA:assertCount(2); runtime:registerListener(lo); runtime:onGracefulStop(stopHandlerFunc3); - + runtime:StopHandler inlineStopHandler = function() returns error? { + runtime:sleep(2); + moduleA:incrementCount(); + moduleA:assertCount(7); + }; + runtime:onGracefulStop(inlineStopHandler); checkpanic lo.'start(); error? v = lo.gracefulStop(); if (v is error) { diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/runtime/api/stop_handler/modules/moduleA/main.bal b/tests/jballerina-unit-test/src/test/resources/test-src/runtime/api/stop_handler/modules/moduleA/main.bal index 088aca81c2b2..a7231d21d255 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/runtime/api/stop_handler/modules/moduleA/main.bal +++ b/tests/jballerina-unit-test/src/test/resources/test-src/runtime/api/stop_handler/modules/moduleA/main.bal @@ -13,7 +13,6 @@ // 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/test; @@ -22,7 +21,7 @@ int count = 0; function stopHandlerFunc1() returns error? { runtime:sleep(1); incrementCount(); - assertCount(11); + assertCount(12); } function stopHandlerFunc2() returns error? { From 843d7e0d9b3fc6c1a07b8ff4e721a72c0a9f42e9 Mon Sep 17 00:00:00 2001 From: hindujaB Date: Wed, 4 Oct 2023 13:55:20 +0530 Subject: [PATCH 3/3] Move stop handler unit test to integration test --- .../packaging/ModuleExecutionFlowTests.java | 2 + .../packaging/stop_handler_execution/main.bal | 12 ++- .../test/runtime/api/RuntimeAPITest.java | 1 - .../runtime/api/stop_handler/Ballerina.toml | 4 - .../runtime/api/stop_handler/main.bal | 74 --------------- .../api/stop_handler/modules/moduleA/main.bal | 95 ------------------- 6 files changed, 11 insertions(+), 177 deletions(-) delete mode 100644 tests/jballerina-unit-test/src/test/resources/test-src/runtime/api/stop_handler/Ballerina.toml delete mode 100644 tests/jballerina-unit-test/src/test/resources/test-src/runtime/api/stop_handler/main.bal delete mode 100644 tests/jballerina-unit-test/src/test/resources/test-src/runtime/api/stop_handler/modules/moduleA/main.bal diff --git a/tests/jballerina-integration-test/src/test/java/org/ballerinalang/test/packaging/ModuleExecutionFlowTests.java b/tests/jballerina-integration-test/src/test/java/org/ballerinalang/test/packaging/ModuleExecutionFlowTests.java index dd9722b53eb8..e29f1801e14e 100644 --- a/tests/jballerina-integration-test/src/test/java/org/ballerinalang/test/packaging/ModuleExecutionFlowTests.java +++ b/tests/jballerina-integration-test/src/test/java/org/ballerinalang/test/packaging/ModuleExecutionFlowTests.java @@ -140,6 +140,8 @@ public void testStopHandlerExecution() throws BallerinaTestException { LogLeecher infoLeecher1 = new LogLeecher("Stopped stopHandlerFunc3"); LogLeecher infoLeecher2 = new LogLeecher("Stopped stopHandlerFunc2"); LogLeecher infoLeecher3 = new LogLeecher("Stopped stopHandlerFunc1"); + LogLeecher infoLeecher4 = new LogLeecher("Stopped inlineStopHandler"); + serverInstance.addLogLeecher(infoLeecher4); serverInstance.addLogLeecher(infoLeecher1); serverInstance.addLogLeecher(infoLeecher2); serverInstance.addLogLeecher(infoLeecher3); diff --git a/tests/jballerina-integration-test/src/test/resources/packaging/stop_handler_execution/main.bal b/tests/jballerina-integration-test/src/test/resources/packaging/stop_handler_execution/main.bal index 41637860fe75..54bd4a2819be 100644 --- a/tests/jballerina-integration-test/src/test/resources/packaging/stop_handler_execution/main.bal +++ b/tests/jballerina-integration-test/src/test/resources/packaging/stop_handler_execution/main.bal @@ -22,19 +22,19 @@ int count = 0; function stopHandlerFunc1() returns error? { incrementCount(); - assertCount(5); + assertCount(6); println("Stopped stopHandlerFunc1"); } function stopHandlerFunc2() returns error? { incrementCount(); - assertCount(4); + assertCount(5); println("Stopped stopHandlerFunc2"); } function stopHandlerFunc3() returns error? { incrementCount(); - assertCount(3); + assertCount(4); println("Stopped stopHandlerFunc3"); } @@ -49,6 +49,12 @@ public function main() { incrementCount(); assertCount(2); runtime:onGracefulStop(stopHandlerFunc3); + runtime:StopHandler inlineStopHandler = function() returns error? { + incrementCount(); + assertCount(3); + println("Stopped inlineStopHandler"); + }; + runtime:onGracefulStop(inlineStopHandler); runtime:sleep(3); } diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/runtime/api/RuntimeAPITest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/runtime/api/RuntimeAPITest.java index 19480a6932ef..7ca9edaca1d8 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/runtime/api/RuntimeAPITest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/runtime/api/RuntimeAPITest.java @@ -45,7 +45,6 @@ public Object[] packageNameProvider() { "invalid_values", "async", "utils", - "stop_handler", "identifier_utils", "environment" }; diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/runtime/api/stop_handler/Ballerina.toml b/tests/jballerina-unit-test/src/test/resources/test-src/runtime/api/stop_handler/Ballerina.toml deleted file mode 100644 index 1b09d3767f68..000000000000 --- a/tests/jballerina-unit-test/src/test/resources/test-src/runtime/api/stop_handler/Ballerina.toml +++ /dev/null @@ -1,4 +0,0 @@ -[package] -org= "testorg" -name="stop_handler" -version= "1.0.0" diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/runtime/api/stop_handler/main.bal b/tests/jballerina-unit-test/src/test/resources/test-src/runtime/api/stop_handler/main.bal deleted file mode 100644 index c789f0be1303..000000000000 --- a/tests/jballerina-unit-test/src/test/resources/test-src/runtime/api/stop_handler/main.bal +++ /dev/null @@ -1,74 +0,0 @@ -// Copyright (c) 2022 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. -// -// WSO2 Inc. licenses this file to you under the Apache License, -// Version 2.0 (the "License"); you may not use this file except -// in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// 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/test; - -import stop_handler.moduleA; - -function stopHandlerFunc1() returns error? { - runtime:sleep(2.5); - moduleA:incrementCount(); - moduleA:assertCount(11); -} - -function stopHandlerFunc2() returns error? { - runtime:sleep(1); - moduleA:incrementCount(); - moduleA:assertCount(10); -} - -function stopHandlerFunc3() returns error? { - runtime:sleep(0.5); - moduleA:incrementCount(); - moduleA:assertCount(8); -} - -function stopHandlerFunc4() returns error? { - runtime:sleep(1.5); - moduleA:incrementCount(); - moduleA:assertCount(9); -} - -function init() { - moduleA:incrementCount(); - moduleA:assertCount(1); - runtime:onGracefulStop(stopHandlerFunc1); - runtime:onGracefulStop(stopHandlerFunc2); - runtime:onGracefulStop(stopHandlerFunc4); -} - -final moduleA:ListenerObj lo = new moduleA:ListenerObj("ModDyncListener"); - -public function main() { - moduleA:incrementCount(); - moduleA:assertCount(2); - runtime:registerListener(lo); - runtime:onGracefulStop(stopHandlerFunc3); - runtime:StopHandler inlineStopHandler = function() returns error? { - runtime:sleep(2); - moduleA:incrementCount(); - moduleA:assertCount(7); - }; - runtime:onGracefulStop(inlineStopHandler); - checkpanic lo.'start(); - error? v = lo.gracefulStop(); - if (v is error) { - test:assertEquals(v.message(), "listener in main stopped"); - } - runtime:deregisterListener(lo); - moduleA:main(); -} diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/runtime/api/stop_handler/modules/moduleA/main.bal b/tests/jballerina-unit-test/src/test/resources/test-src/runtime/api/stop_handler/modules/moduleA/main.bal deleted file mode 100644 index a7231d21d255..000000000000 --- a/tests/jballerina-unit-test/src/test/resources/test-src/runtime/api/stop_handler/modules/moduleA/main.bal +++ /dev/null @@ -1,95 +0,0 @@ -// Copyright (c) 2022 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. -// -// WSO2 Inc. licenses this file to you under the Apache License, -// Version 2.0 (the "License"); you may not use this file except -// in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// 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/test; - -int count = 0; - -function stopHandlerFunc1() returns error? { - runtime:sleep(1); - incrementCount(); - assertCount(12); -} - -function stopHandlerFunc2() returns error? { - incrementCount(); - assertCount(6); -} - -function init() { - runtime:onGracefulStop(stopHandlerFunc1); -} - -public class ListenerObj { - - *runtime:DynamicListener; - private string name = ""; - - public function init(string name) { - self.name = name; - } - - public function 'start() returns error? { - if (self.name == "ModDyncListener") { - incrementCount(); - assertCount(3); - } - } - - public function gracefulStop() returns error? { - if (self.name == "ModDyncListener") { - runtime:sleep(1.5); - incrementCount(); - assertCount(4); - return error("listener in main stopped"); - } else if (self.name == "ModDyncListenerA") { - return error("listener in moduleA stopped"); - } - } - - public function immediateStop() { - } - - public function attach(service object {} s, string[]|string? name = ()) returns error? { - } - - public function detach(service object {} s) returns error? { - } -} - -final ListenerObj lo = new ListenerObj("ModDyncListenerA"); - -public function main() { - incrementCount(); - assertCount(5); - runtime:registerListener(lo); - runtime:onGracefulStop(stopHandlerFunc2); - - checkpanic lo.'start(); - error? v = lo.gracefulStop(); - if (v is error) { - test:assertEquals(v.message(), "listener in moduleA stopped"); - } - runtime:deregisterListener(lo); -} - -public function incrementCount() { - count += 1; -} - -public function assertCount(int val) { - test:assertEquals(count, val); -}