Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into fix-worker
Browse files Browse the repository at this point in the history
  • Loading branch information
lochana-chathura committed Apr 15, 2024
2 parents 47e31ba + e76d71b commit 79f92df
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import io.ballerina.runtime.internal.scheduling.Strand;
import io.ballerina.runtime.internal.scheduling.WorkerDataChannel;

import java.util.Objects;

/**
* Native implementation of lang.internal:WorkerChannels.
*
Expand All @@ -35,10 +37,11 @@ public class WorkerChannels {
* @param channelIds channel IDs of the channels to be closed
*/
public static void autoClose(BString[] channelIds) {
Strand parent = Scheduler.getStrand().parent;
Strand currentStrand = Scheduler.getStrand();
Strand channelHoldingStrand = Objects.requireNonNullElse(currentStrand.parent, currentStrand);
for (BString channelId : channelIds) {
String channelName = channelId.getValue() + ":" + (parent.functionInvocation - 1);
WorkerDataChannel workerDataChannel = parent.wdChannels.getWorkerDataChannel(channelName);
String channelName = channelId.getValue() + ":" + (channelHoldingStrand.functionInvocation - 1);
WorkerDataChannel workerDataChannel = channelHoldingStrand.wdChannels.getWorkerDataChannel(channelName);
workerDataChannel.autoClose();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ private XMLToRecordConverter() {}
private static final String XMLDATA = "xmldata";
private static final String COLON = ":";

public static XMLToRecordResponse convertXMLToRecord(String xmlValue, boolean isRecordTypeDesc, boolean isClosed,
boolean forceFormatRecordFields,
String textFieldName, boolean withNameSpaces) {
public static XMLToRecordResponse convert(String xmlValue, boolean isRecordTypeDesc, boolean isClosed,
boolean forceFormatRecordFields,
String textFieldName, boolean withNameSpaces) {
Map<String, NonTerminalNode> recordToTypeDescNodes = new LinkedHashMap<>();
Map<String, AnnotationNode> recordToAnnotationNodes = new LinkedHashMap<>();
Map<String, Element> recordToElementNodes = new LinkedHashMap<>();
Expand Down Expand Up @@ -180,12 +180,12 @@ public static XMLToRecordResponse convertXMLToRecord(String xmlValue, boolean is
* @param xmlValue The XML value to be converted to a record.
* @param isRecordTypeDesc Whether the record is a type descriptor.
* @param isClosed Whether the record is closed or not.
* @param textFieldName Whether to force format the result.
* @param forceFormatRecordFields Whether to force format the result.
* @return {@link XMLToRecordResponse} The response object containing the converted record.
*/
public static XMLToRecordResponse convert(String xmlValue, boolean isRecordTypeDesc, boolean isClosed,
boolean textFieldName) {
return convertXMLToRecord(xmlValue, isRecordTypeDesc, isClosed, textFieldName, null, true);
boolean forceFormatRecordFields) {
return convert(xmlValue, isRecordTypeDesc, isClosed, forceFormatRecordFields, null, true);
}

private static void generateRecords(Element xmlElement, boolean isClosed,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,11 @@ public CompletableFuture<XMLToRecordResponse> convert(XMLToRecordRequest request
boolean isRecordTypeDesc = request.getIsRecordTypeDesc();
boolean isClosed = request.getIsClosed();
boolean forceFormatRecordFields = request.getForceFormatRecordFields();
String textFieldName = request.getTextFieldName();
boolean withNameSpace = request.getIsWithNameSpace();

return XMLToRecordConverter.convert(xmlValue, isRecordTypeDesc, isClosed, forceFormatRecordFields);
return XMLToRecordConverter.convert(xmlValue, isRecordTypeDesc, isClosed, forceFormatRecordFields,
textFieldName, withNameSpace);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,48 +25,44 @@
*/
public class XMLToRecordRequest {

private String xmlValue;
private boolean isRecordTypeDesc;
private boolean isClosed;
private boolean forceFormatRecordFields;
private final String xmlValue;
private final boolean isRecordTypeDesc;
private final boolean isClosed;
private final boolean forceFormatRecordFields;
private final String textFieldName;
private final boolean withNameSpace;

public XMLToRecordRequest(String xmlValue, boolean isRecordTypeDesc, boolean isClosed,
boolean forceFormatRecordFields) {
boolean forceFormatRecordFields, String textFieldName, boolean withNameSpace) {
this.xmlValue = xmlValue;
this.isRecordTypeDesc = isRecordTypeDesc;
this.isClosed = isClosed;
this.forceFormatRecordFields = forceFormatRecordFields;
this.textFieldName = textFieldName;
this.withNameSpace = withNameSpace;
}

public String getXmlValue() {
return xmlValue;
}

public void setXmlValue(String xmlValue) {
this.xmlValue = xmlValue;
}

public boolean getIsRecordTypeDesc() {
return isRecordTypeDesc;
}

public void setIsRecordTypeDesc(boolean isRecordTypeDesc) {
this.isRecordTypeDesc = isRecordTypeDesc;
}

public boolean getIsClosed() {
return isClosed;
}

public void setIsClosed(boolean isClosed) {
this.isClosed = isClosed;
}

public boolean getForceFormatRecordFields() {
return forceFormatRecordFields;
}

public void setForceFormatRecordFields(boolean forceFormatRecordFields) {
this.forceFormatRecordFields = forceFormatRecordFields;
public String getTextFieldName() {
return textFieldName;
}

public boolean getIsWithNameSpace() {
return withNameSpace;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ public class XMLToRecordConverterTests {
private final Path sample24Bal = RES_DIR.resolve(BAL_DIR)
.resolve("sample_24.bal");

private final Path sample25XML = RES_DIR.resolve(XML_DIR)
.resolve("sample_25.xml");
private final Path sample25Bal = RES_DIR.resolve(BAL_DIR)
.resolve("sample_25.bal");

private static final String XMLToRecordServiceEP = "xmlToRecord/convert";


Expand Down Expand Up @@ -341,7 +346,7 @@ public void testWithMultipleAttributes() throws IOException {
@Test(description = "testXMLWithNamespacesWithoutNamespaceAnnotation")
public void testXMLWithNamespacesWithoutNamespaceAttribute() throws IOException {
String xmlFileContent = Files.readString(sample19XML);
String generatedCodeBlock = XMLToRecordConverter.convertXMLToRecord(xmlFileContent, false, false, false,
String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false,
"amount", false).getCodeBlock().replaceAll("\\s+", "");
String expectedCodeBlock = Files.readString(sample19Bal).replaceAll("\\s+", "");
Assert.assertEquals(generatedCodeBlock, expectedCodeBlock);
Expand All @@ -350,7 +355,7 @@ public void testXMLWithNamespacesWithoutNamespaceAttribute() throws IOException
@Test(description = "testXMLWithMultipleAttributesAndNamespacesWithoutAnnotations")
public void testXMLWithMultipleAttributesAndNamespacesWithoutAnnotations() throws IOException {
String xmlFileContent = Files.readString(sample20XML);
String generatedCodeBlock = XMLToRecordConverter.convertXMLToRecord(xmlFileContent, false, false, false,
String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false,
null, false).getCodeBlock().replaceAll("\\s+", "");
String expectedCodeBlock = Files.readString(sample20Bal).replaceAll("\\s+", "");
Assert.assertEquals(generatedCodeBlock, expectedCodeBlock);
Expand All @@ -359,7 +364,7 @@ public void testXMLWithMultipleAttributesAndNamespacesWithoutAnnotations() throw
@Test(description = "testXMLWithMultipleAttributesAndNamespacesWithAnnotations")
public void testXMLWithMultipleAttributesAndNamespacesWithAnnotations() throws IOException {
String xmlFileContent = Files.readString(sample21XML);
String generatedCodeBlock = XMLToRecordConverter.convertXMLToRecord(xmlFileContent, false, false, false,
String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false,
null, true).getCodeBlock().replaceAll("\\s+", "");
String expectedCodeBlock = Files.readString(sample21Bal).replaceAll("\\s+", "");
Assert.assertEquals(generatedCodeBlock, expectedCodeBlock);
Expand All @@ -368,7 +373,7 @@ public void testXMLWithMultipleAttributesAndNamespacesWithAnnotations() throws I
@Test(description = "testXMLWithoutNamespacePrefix")
public void testXMLWithoutNamespacePrefix() throws IOException {
String xmlFileContent = Files.readString(sample22XML);
String generatedCodeBlock = XMLToRecordConverter.convertXMLToRecord(xmlFileContent, false, false, false,
String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false,
null, true).getCodeBlock().replaceAll("\\s+", "");
String expectedCodeBlock = Files.readString(sample22Bal).replaceAll("\\s+", "");
Assert.assertEquals(generatedCodeBlock, expectedCodeBlock);
Expand All @@ -377,7 +382,7 @@ public void testXMLWithoutNamespacePrefix() throws IOException {
@Test(description = "testXMLWithConflictingElementAndAttributeNames")
public void testXMLWithConflictingElementAndAttributeNames() throws IOException {
String xmlFileContent = Files.readString(sample23XML);
String generatedCodeBlock = XMLToRecordConverter.convertXMLToRecord(xmlFileContent, false, false, false,
String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false,
null, true).getCodeBlock().replaceAll("\\s+", "");
String expectedCodeBlock = Files.readString(sample23Bal).replaceAll("\\s+", "");
Assert.assertEquals(generatedCodeBlock, expectedCodeBlock);
Expand All @@ -386,7 +391,7 @@ public void testXMLWithConflictingElementAndAttributeNames() throws IOException
@Test(description = "testXMLWithoutNamespaces")
public void testXMLWithoutNamespaces() throws IOException {
String xmlFileContent = Files.readString(sample24XML);
String generatedCodeBlock = XMLToRecordConverter.convertXMLToRecord(xmlFileContent, false, false, false,
String generatedCodeBlock = XMLToRecordConverter.convert(xmlFileContent, false, false, false,
null, false).getCodeBlock().replaceAll("\\s+", "");
String expectedCodeBlock = Files.readString(sample24Bal).replaceAll("\\s+", "");
Assert.assertEquals(generatedCodeBlock, expectedCodeBlock);
Expand All @@ -397,11 +402,25 @@ public void testXMLToRecordService() throws IOException, ExecutionException, Int
Endpoint serviceEndpoint = TestUtil.initializeLanguageSever();
String xmlValue = Files.readString(sample0XML);

XMLToRecordRequest request = new XMLToRecordRequest(xmlValue, false, false, false);
XMLToRecordRequest request = new XMLToRecordRequest(xmlValue, false, false, false, null, true);
CompletableFuture<?> result = serviceEndpoint.request(XMLToRecordServiceEP, request);
XMLToRecordResponse response = (XMLToRecordResponse) result.get();
String generatedCodeBlock = response.getCodeBlock().replaceAll("\\s+", "");
String expectedCodeBlock = Files.readString(sample0Bal).replaceAll("\\s+", "");
Assert.assertEquals(generatedCodeBlock, expectedCodeBlock);
}

@Test(description = "Test xml record request with text field name and without namespace")
public void testXMLToRecordServiceWithFieldNameAndWithoutNamespace()
throws IOException, ExecutionException, InterruptedException {
Endpoint serviceEndpoint = TestUtil.initializeLanguageSever();
String xmlValue = Files.readString(sample25XML);

XMLToRecordRequest request = new XMLToRecordRequest(xmlValue, false, false, false, "__text", false);
CompletableFuture<?> result = serviceEndpoint.request(XMLToRecordServiceEP, request);
XMLToRecordResponse response = (XMLToRecordResponse) result.get();
String generatedCodeBlock = response.getCodeBlock().replaceAll("\\s+", "");
String expectedCodeBlock = Files.readString(sample25Bal).replaceAll("\\s+", "");
Assert.assertEquals(generatedCodeBlock, expectedCodeBlock);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
type Book_Title record {
string __text;
@xmldata:Attribute
string edition;
@xmldata:Attribute
string lang;
};

type Book_Author record {
string __text;
@xmldata:Attribute
string gender;
@xmldata:Attribute
string nationality;
};

type Book_Book record {
Book_Title title;
Book_Author author;
string publish_date;
string description;
};

type Books record {
string genre;
Book_Book book;
};

@xmldata:Name {value: "library"}
type Library record {
Books books;
@xmldata:Attribute
string genre;
};
11 changes: 11 additions & 0 deletions misc/xml-to-record-converter/src/test/resources/xml/sample_25.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<library xmlns:book="http://example.com/books" genre="genre">
<books>
<genre>Programming</genre>
<book:book>
<book:title lang="en" edition="1st">XML Developer's Guide</book:title>
<book:author gender="male" nationality="American">Gambardella, Matthew</book:author>
<book:publish_date>2000-10-01</book:publish_date>
<book:description>An in-depth look at creating applications with XML.</book:description>
</book:book>
</books>
</library>
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private List<LogLeecher> getProfilerLogLeechers(String htmlFilePath) {
new LogLeecher(" Instrumented function count: "),
new LogLeecher("[5/6] Running executable..."),
new LogLeecher("[6/6] Generating output..."),
new LogLeecher(" Execution time: [1-5] seconds ", true, LogLeecher.LeecherType.INFO),
new LogLeecher(" Execution time: [1-9] seconds ", true, LogLeecher.LeecherType.INFO),
new LogLeecher(" Output: "),
new LogLeecher(htmlFilePath));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,12 @@ public void testForkWithWorkersInSameFunction() {
long returnInt = (long) returns;
Assert.assertEquals(returnInt, 10);
}

@Test(description = "Test fork within if condition")
public void testForkWithinIfCondition() {
CompileResult result = BCompileUtil.compile("test-src/workers/fork-within-if-condition.bal");
Object returns = BRunUtil.invoke(result, "testForkWithinIfCondition");
Assert.assertTrue(returns instanceof Long);
Assert.assertEquals(((Long) returns).intValue(), 1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) 2024 WSO2 LLC. (http://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.

function testForkWithinIfCondition() returns int {
boolean isForked = false;
if isForked {
fork {
worker A {
5 -> B;
string value = <- B;
}

worker B {
int value = <- A;
"a" -> A;
}
}
return 0;
}
return 1;
}

0 comments on commit 79f92df

Please sign in to comment.