Search in sources :

Example 51 with BInteger

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");
}
Also used : BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BInteger(org.ballerinalang.model.values.BInteger) CompileResult(org.ballerinalang.launcher.util.CompileResult) BString(org.ballerinalang.model.values.BString) Test(org.testng.annotations.Test)

Example 52 with BInteger

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");
}
Also used : BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BInteger(org.ballerinalang.model.values.BInteger) CompileResult(org.ballerinalang.launcher.util.CompileResult) BString(org.ballerinalang.model.values.BString) Test(org.testng.annotations.Test)

Example 53 with BInteger

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);
}
Also used : BMap(org.ballerinalang.model.values.BMap) BValue(org.ballerinalang.model.values.BValue) BString(org.ballerinalang.model.values.BString) BInteger(org.ballerinalang.model.values.BInteger) BString(org.ballerinalang.model.values.BString) Test(org.testng.annotations.Test)

Example 54 with BInteger

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.");
}
Also used : BValue(org.ballerinalang.model.values.BValue) BInteger(org.ballerinalang.model.values.BInteger) Test(org.testng.annotations.Test)

Example 55 with BInteger

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.");
}
Also used : BValue(org.ballerinalang.model.values.BValue) BInteger(org.ballerinalang.model.values.BInteger) BJSON(org.ballerinalang.model.values.BJSON) Test(org.testng.annotations.Test)

Aggregations

BInteger (org.ballerinalang.model.values.BInteger)364 BValue (org.ballerinalang.model.values.BValue)324 Test (org.testng.annotations.Test)305 BString (org.ballerinalang.model.values.BString)91 BFloat (org.ballerinalang.model.values.BFloat)55 BStruct (org.ballerinalang.model.values.BStruct)33 BBoolean (org.ballerinalang.model.values.BBoolean)24 BRefValueArray (org.ballerinalang.model.values.BRefValueArray)18 CompileResult (org.ballerinalang.launcher.util.CompileResult)12 BBlob (org.ballerinalang.model.values.BBlob)11 BeforeTest (org.testng.annotations.BeforeTest)11 BIntArray (org.ballerinalang.model.values.BIntArray)9 BMap (org.ballerinalang.model.values.BMap)9 BRefType (org.ballerinalang.model.values.BRefType)8 BStringArray (org.ballerinalang.model.values.BStringArray)8 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)8 BType (org.ballerinalang.model.types.BType)6 BStructType (org.ballerinalang.model.types.BStructType)4 BJSON (org.ballerinalang.model.values.BJSON)4 UnsupportedFieldTypeException (org.ballerinalang.net.grpc.exception.UnsupportedFieldTypeException)4