use of org.ballerinalang.model.values.BInteger in project ballerina by ballerina-lang.
the class TimerTest method testExecutionWithWorkersAndErrorFn.
@Test(description = "Tests running a timer started within workers where the onTrigger function generates an error")
public void testExecutionWithWorkersAndErrorFn() {
CompileResult timerCompileResult = BCompileUtil.compileAndSetup("test-src/task/timer-workers.bal");
printDiagnostics(timerCompileResult);
int w1InitialDelay = 500;
int w1Interval = 1000;
int w2InitialDelay = 800;
int w2Interval = 1000;
String w1ErrMsg = "w1: Timer error";
String w2ErrMsg = "w2: Timer error";
BValue[] returns = BRunUtil.invokeStateful(timerCompileResult, "scheduleTimer", new BValue[] { new BInteger(w1InitialDelay), new BInteger(w1Interval), new BInteger(w2InitialDelay), new BInteger(w2Interval), new BString(w1ErrMsg), new BString(w2ErrMsg) });
String w1TaskId = returns[0].stringValue();
String w2TaskId = returns[1].stringValue();
// A non-null task ID should be returned
assertNotEquals(w1TaskId, "", "Invalid task ID from worker w1");
// A non-null task ID should be returned
assertNotEquals(w2TaskId, "", "Invalid task ID from worker w2");
await().atMost(10, SECONDS).until(() -> {
BValue[] errors = BRunUtil.invokeStateful(timerCompileResult, "getErrors");
return errors != null && errors[0] != null && !errors[0].stringValue().isEmpty() && errors[1] != null && !errors[1].stringValue().isEmpty();
});
// Now test whether the onError Ballerina function got called
BValue[] error = BRunUtil.invokeStateful(timerCompileResult, "getErrors");
assertNotNull(error[0], "Expected error not returned.");
assertEquals(error[0].stringValue(), w1ErrMsg, "Expected error message not returned.");
assertEquals(error[1].stringValue(), w2ErrMsg, "Expected error message not returned.");
// Now let's try stopping the tasks
BValue[] stopResult = BRunUtil.invokeStateful(timerCompileResult, "stopTasks", new BValue[] { new BString(w1TaskId), new BString(w2TaskId) });
assertNull(stopResult[0], "Task stopping on worker w1 resulted in an error");
assertNull(stopResult[1], "Task stopping on worker w2 resulted in an error");
// One more check to see whether the task really stopped
BValue[] counts = BRunUtil.invokeStateful(timerCompileResult, "getCounts");
assertEquals(((BInteger) counts[0]).intValue(), -1, "Count hasn't been reset");
assertEquals(((BInteger) counts[1]).intValue(), -1, "Count hasn't been reset");
}
use of org.ballerinalang.model.values.BInteger in project ballerina by ballerina-lang.
the class TimerTest method testSimpleExecutionWithWorkers.
@Test(description = "Tests running a timer started within workers")
public void testSimpleExecutionWithWorkers() {
CompileResult timerCompileResult = BCompileUtil.compileAndSetup("test-src/task/timer-workers.bal");
printDiagnostics(timerCompileResult);
int w1InitialDelay = 500;
int w1Interval = 1000;
int w2InitialDelay = 800;
int w2Interval = 2000;
BValue[] returns = BRunUtil.invokeStateful(timerCompileResult, "scheduleTimer", new BValue[] { new BInteger(w1InitialDelay), new BInteger(w1Interval), new BInteger(w2InitialDelay), new BInteger(w2Interval), new BString(""), new BString("") });
String w1TaskId = returns[0].stringValue();
String w2TaskId = returns[1].stringValue();
// A non-null task ID should be returned
assertNotEquals(w1TaskId, "", "Invalid task ID from worker w1");
// A non-null task ID should be returned
assertNotEquals(w2TaskId, "", "Invalid task ID from worker w2");
await().atMost(30, SECONDS).until(() -> {
BValue[] counts = BRunUtil.invokeStateful(timerCompileResult, "getCounts");
return counts != null && counts[0] != null && counts[1] != null && ((BInteger) counts[0]).intValue() >= 5 && ((BInteger) counts[1]).intValue() >= 5;
});
// Now let's try stopping the tasks
BValue[] stopResult = BRunUtil.invokeStateful(timerCompileResult, "stopTasks", new BValue[] { new BString(w1TaskId), new BString(w2TaskId) });
assertNull(stopResult[0], "Task stopping on worker w1 resulted in an error");
assertNull(stopResult[1], "Task stopping on worker w2 resulted in an error");
// One more check to see whether the task really stopped
BValue[] counts = BRunUtil.invokeStateful(timerCompileResult, "getCounts");
assertEquals(((BInteger) counts[0]).intValue(), -1, "Count hasn't been reset");
assertEquals(((BInteger) counts[1]).intValue(), -1, "Count hasn't been reset");
}
use of org.ballerinalang.model.values.BInteger in project ballerina by ballerina-lang.
the class StructTest method testBasicStruct.
@Test(description = "Test Basic struct operations")
public void testBasicStruct() {
BValue[] returns = BRunUtil.invoke(compileResult, "testCreateStruct");
Assert.assertTrue(returns[0] instanceof BString);
Assert.assertEquals(returns[0].stringValue(), "Jack");
Assert.assertTrue(returns[1] instanceof BMap);
BMap<String, ?> adrsMap = ((BMap) returns[1]);
Assert.assertEquals(adrsMap.get("country"), new BString("USA"));
Assert.assertEquals(adrsMap.get("state"), new BString("CA"));
Assert.assertTrue(returns[2] instanceof BInteger);
Assert.assertEquals(((BInteger) returns[2]).intValue(), 25);
}
use of org.ballerinalang.model.values.BInteger in project ballerina by ballerina-lang.
the class BAnyTypeSuccessScenariosTest method variableDefTest.
@Test(description = "Test variable init with any")
public void variableDefTest() {
BValue[] returns = BRunUtil.invoke(result, "variableDefTest", new BValue[0]);
Assert.assertEquals(returns.length, 1);
Assert.assertSame(returns[0].getClass(), BInteger.class);
BInteger intVal = (BInteger) returns[0];
Assert.assertEquals(intVal.intValue(), 5, "Invalid int value returned.");
}
use of org.ballerinalang.model.values.BInteger in project ballerina by ballerina-lang.
the class BAnyTypeSuccessScenariosTest method testMultipleReturnWithAny.
@Test(description = "Test Multiple returns with any")
public void testMultipleReturnWithAny() {
BValue[] returns = BRunUtil.invoke(result, "multipleReturnWithAny", new BValue[0]);
Assert.assertEquals(returns.length, 2);
Assert.assertSame(returns[0].getClass(), BJSON.class);
Assert.assertSame(returns[1].getClass(), BInteger.class);
BJSON json = (BJSON) returns[0];
BInteger intVal = (BInteger) returns[1];
Assert.assertEquals(json.stringValue(), "{\"PropertyName\":\"Value\"}", "Invalid json value returned.");
Assert.assertEquals(intVal.intValue(), 7, "Invalid int value returned.");
}
Aggregations