use of org.ballerinalang.model.values.BRefValueArray in project ballerina by ballerina-lang.
the class GetCallStack method execute.
@Override
public void execute(Context context) {
final BRefValueArray bRefValueArray = BLangVMErrors.generateCallStack(context.getParentWorkerExecutionContext(), context.getCallableUnitInfo());
context.setReturnValues(bRefValueArray);
}
use of org.ballerinalang.model.values.BRefValueArray in project ballerina by ballerina-lang.
the class Sprintf method execute.
@Override
public final void execute(final Context context) {
String format = context.getStringArgument(0);
BRefValueArray args = (BRefValueArray) context.getRefArgument(0);
StringBuilder result = new StringBuilder();
// k records number of format specifiers seen so far, used to read respective array element
for (int i = 0, j = 0, k = 0; i < format.length(); i++) {
if (format.charAt(i) == '%' && ((i + 1) < format.length())) {
// skip % character
j = i + 1;
if (k >= args.size()) {
// there's not enough arguments
throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.NOT_ENOUGH_FORMAT_ARGUMENTS);
}
StringBuilder padding = new StringBuilder();
while (Character.isDigit(format.charAt(j)) || format.charAt(j) == '.') {
padding.append(format.charAt(j));
j += 1;
}
switch(format.charAt(j)) {
case 'b':
result.append(String.format("%" + padding + "b", args.get(k).value()));
break;
case 'd':
result.append(String.format("%" + padding + "d", args.get(k).value()));
break;
case 'f':
result.append(String.format("%" + padding + "f", args.get(k).value()));
break;
case 'x':
result.append(String.format("%" + padding + "x", args.get(k).value()));
break;
case 'X':
result.append(String.format("%" + padding + "X", args.get(k).value()));
break;
case 'o':
result.append(String.format("%" + padding + "o", args.get(k).value()));
break;
case 'B':
result.append(Integer.toBinaryString(Integer.parseInt(args.get(k).stringValue())));
break;
// fall through
case 'p':
case 'l':
case 'L':
case 'j':
case 't':
case 'r':
case 'a':
case 's':
result.append(String.format("%" + padding + "s", args.get(k).stringValue()));
break;
case '%':
result.append("%");
break;
default:
// format string not supported
throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.INVALID_FORMAT_SPECIFIER, format.charAt(j));
}
if (format.charAt(j) == '%') {
// special case %%, don't count as a format specifier
i++;
} else {
k++;
i = j;
}
continue;
}
// no match, copy and continue
result.append(format.charAt(i));
}
context.setReturnValues(new BString(result.toString()));
}
use of org.ballerinalang.model.values.BRefValueArray in project ballerina by ballerina-lang.
the class MultipartDataSource method serializeBodyPart.
/**
* Serialize body parts including nested parts within them.
*
* @param outputStream Represent the outputstream that the body parts will be written to
* @param parentBoundaryString Represent the parent boundary string
* @param parentBodyPart Represent parent body part
*/
private void serializeBodyPart(OutputStream outputStream, String parentBoundaryString, BStruct parentBodyPart) {
final Writer writer = new BufferedWriter(new OutputStreamWriter(outputStream, Charset.defaultCharset()));
BRefValueArray childParts = parentBodyPart.getNativeData(BODY_PARTS) != null ? (BRefValueArray) parentBodyPart.getNativeData(BODY_PARTS) : null;
try {
if (childParts == null) {
return;
}
boolean isFirst = true;
for (int i = 0; i < childParts.size(); i++) {
BStruct childPart = (BStruct) childParts.get(i);
// Write leading boundary string
if (isFirst) {
isFirst = false;
writer.write(DASH_BOUNDARY);
} else {
writer.write(CRLF_POST_DASH);
}
writer.write(parentBoundaryString);
writer.write(CRLF);
checkForNestedParts(writer, childPart);
writeBodyContent(outputStream, childPart);
}
writeFinalBoundaryString(writer, parentBoundaryString);
} catch (IOException e) {
log.error("Error occurred while writing body parts to outputstream", e.getMessage());
}
}
use of org.ballerinalang.model.values.BRefValueArray in project ballerina by ballerina-lang.
the class CircuitBreakerTest method testTrialRunFailure.
/**
* Test case scenario:
* - Initially the circuit is healthy and functioning normally.
* - Backend service becomes unavailable and eventually, the failure threshold is exceeded.
* - Requests afterwards are immediately failed, with a 503 response.
* - After the reset timeout expires, the circuit goes to HALF_OPEN state and a trial request is sent.
* - The backend service is not available and therefore, the request fails again and the circuit goes back to OPEN.
*/
@Test
public void testTrialRunFailure() {
// Expected HTTP status codes from circuit breaker responses.
int[] expectedStatusCodes = new int[] { 200, 502, 503, 502, 503, 502 };
BValue[] returnVals = BRunUtil.invoke(compileResult, "testTrialRunFailure");
Assert.assertEquals(returnVals.length, 2);
BRefValueArray responses = (BRefValueArray) returnVals[0];
BRefValueArray errs = (BRefValueArray) returnVals[1];
for (int i = 0; i < responses.size(); i++) {
long statusCode;
// indexes are consisted with the HttpClientError Responses.
if (i < CB_CLIENT_TOP_MOST_SUCCESS_INDEX || i == CB_CLIENT_FAILURE_CASE_ERROR_INDEX) {
BStruct res = (BStruct) responses.get(i);
statusCode = res.getIntField(0);
Assert.assertEquals(statusCode, expectedStatusCodes[i], "Status code does not match.");
} else {
// the request which resulted in an error
Assert.assertNotNull(errs.get(i));
BStruct err = (BStruct) errs.get(i);
statusCode = err.getIntField(0);
// error for requests which were failed immediately.
if (statusCode == 0) {
String msg = err.getStringField(0);
Assert.assertTrue(msg != null && msg.startsWith(CB_ERROR_MSG), "Invalid error message from circuit breaker.");
} else {
Assert.assertEquals(statusCode, 503);
}
}
}
}
use of org.ballerinalang.model.values.BRefValueArray in project ballerina by ballerina-lang.
the class CircuitBreakerTest method testHttpStatusCodeFailure.
/**
* Test case scenario:
* - Initially the circuit is healthy and functioning normally.
* - Backend service respond with HTTP status code configured to consider as failures responses.
* eventually the failure threshold is exceeded.
* - Requests afterwards are immediately failed, with a 503 response.
* - After the reset timeout expires, the circuit goes to HALF_OPEN state and a trial request is sent.
* - The backend service is not available and therefore, the request fails again and the circuit goes back to OPEN.
*/
@Test(description = "Test case for Circuit Breaker HTTP status codes.")
public void testHttpStatusCodeFailure() {
// Expected HTTP status codes from circuit breaker responses.
int[] expectedStatusCodes = new int[] { 200, 500, 503, 500, 503, 503 };
BValue[] returnVals = BRunUtil.invoke(compileResult, "testHttpStatusCodeFailure");
Assert.assertEquals(returnVals.length, 2);
BRefValueArray responses = (BRefValueArray) returnVals[0];
BRefValueArray errs = (BRefValueArray) returnVals[1];
for (int i = 0; i < responses.size(); i++) {
long statusCode;
// indexes are consisted with the HttpClientError Responses.
if (i < CB_CLIENT_TOP_MOST_SUCCESS_INDEX) {
BStruct res = (BStruct) responses.get(i);
statusCode = res.getIntField(0);
Assert.assertEquals(statusCode, expectedStatusCodes[i], "Status code does not match.");
} else {
// the request which resulted in an error
Assert.assertNotNull(errs.get(i));
BStruct err = (BStruct) errs.get(i);
statusCode = err.getIntField(0);
// error for requests which were failed immediately.
if (statusCode == 0) {
String msg = err.getStringField(0);
Assert.assertTrue(msg != null && msg.startsWith(CB_ERROR_MSG), "Invalid error message from circuit breaker.");
} else {
Assert.assertEquals(statusCode, 500, "Incorrect status code.");
}
}
}
}
Aggregations