-
Notifications
You must be signed in to change notification settings - Fork 755
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41741 from poorna2152/worker_change_semantic
Add semantic support for worker `on fail` clause
- Loading branch information
Showing
4 changed files
with
187 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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
78 changes: 78 additions & 0 deletions
78
tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/worker/WorkerOnFailTest.java
This file contains 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,78 @@ | ||
/* | ||
* Copyright (c) 2023, WSO2 LLC. (https://www.wso2.com). | ||
* | ||
* WSO2 LLC. 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. | ||
*/ | ||
|
||
package org.ballerinalang.test.worker; | ||
|
||
import org.ballerinalang.test.BCompileUtil; | ||
import org.ballerinalang.test.BRunUtil; | ||
import org.ballerinalang.test.CompileResult; | ||
import org.testng.Assert; | ||
import org.testng.annotations.BeforeClass; | ||
import org.testng.annotations.Test; | ||
|
||
import java.util.Arrays; | ||
|
||
/** | ||
* Tests the worker on fail clause. | ||
*/ | ||
public class WorkerOnFailTest { | ||
|
||
private CompileResult result; | ||
|
||
@BeforeClass | ||
public void setup() { | ||
this.result = BCompileUtil.compile("test-src/workers/worker-on-fail.bal"); | ||
Assert.assertEquals(result.getErrorCount(), 0, Arrays.asList(result.getDiagnostics()).toString()); | ||
} | ||
|
||
@Test | ||
public void simpleOnFailTest() { | ||
Object returns = BRunUtil.invoke(result, "testOnFailInWorker"); | ||
long ret = (long) returns; | ||
Assert.assertEquals(ret, -1); | ||
} | ||
|
||
@Test | ||
public void doOnFailInsideWorker() { | ||
Object returns = BRunUtil.invoke(result, "testDoOnFailInsideWorker"); | ||
long ret = (long) returns; | ||
Assert.assertEquals(ret, 3); | ||
} | ||
|
||
@Test | ||
public void returnWithinOnFail() { | ||
Object returns = BRunUtil.invoke(result, "testReturnWithinOnFail"); | ||
long ret = (long) returns; | ||
Assert.assertEquals(ret, -1); | ||
} | ||
|
||
@Test | ||
public void onFailWorkerWithVariable() { | ||
Object returns = BRunUtil.invoke(result, "testOnFailWorkerWithVariable"); | ||
long ret = (long) returns; | ||
Assert.assertEquals(ret, 0); | ||
} | ||
|
||
@Test | ||
public void workerOnFailWithSend() { | ||
Object returns = BRunUtil.invoke(result, "testWorkerOnFailWithSend"); | ||
long ret = (long) returns; | ||
Assert.assertEquals(ret, 1); | ||
} | ||
|
||
} |
89 changes: 89 additions & 0 deletions
89
tests/jballerina-unit-test/src/test/resources/test-src/workers/worker-on-fail.bal
This file contains 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,89 @@ | ||
function testOnFailInWorker() returns int { | ||
int[] vals = returnIntArr(); | ||
int key = returnOne(); | ||
int val = 0; | ||
|
||
worker A { | ||
int? index = vals.indexOf(key); | ||
if index != () { | ||
val = vals[index]; | ||
} else { | ||
check error("value not found"); | ||
} | ||
} on fail { | ||
val = -1; | ||
} | ||
wait A; | ||
return val; | ||
} | ||
|
||
function testDoOnFailInsideWorker() returns int { | ||
int val = 0; | ||
worker A { | ||
do { | ||
val += 1; | ||
fail error("error in do"); | ||
} on fail { | ||
val += 1; | ||
} | ||
fail error("error for worker"); | ||
} on fail { | ||
val += 1; | ||
} | ||
wait A; | ||
return val; | ||
} | ||
|
||
function testReturnWithinOnFail() returns int { | ||
int x = returnOne(); | ||
worker A returns string { | ||
if (x == 1) { | ||
check error("one"); | ||
} | ||
return "not one"; | ||
} on fail error e { | ||
return e.message(); | ||
} | ||
string str = wait A; | ||
return str == "one" ? -1 : 0; | ||
} | ||
|
||
function testOnFailWorkerWithVariable() returns int { | ||
int x = 0; | ||
worker A { | ||
do { | ||
x += 1; | ||
fail error("error in do"); | ||
} on fail { | ||
x += 1; | ||
} | ||
fail error("error in worker"); | ||
} on fail error e { | ||
if e.message() == "error in worker" { | ||
x -= 2; | ||
} else { | ||
x -= -1; | ||
} | ||
} | ||
wait A; | ||
return x; | ||
} | ||
|
||
function testWorkerOnFailWithSend() returns int { | ||
worker A { | ||
int x = 1; | ||
x -> B; | ||
check error("testWorkerOnFailWithSend"); | ||
} on fail var err { | ||
_ = err.message(); | ||
} | ||
|
||
worker B returns int { | ||
return <- A; | ||
} | ||
return wait B; | ||
} | ||
|
||
function returnOne() returns int => 1; | ||
|
||
function returnIntArr() returns int[] => [2, 3, 4, 5]; |