Search in sources :

Example 6 with WindowConfig

use of org.apache.pulsar.common.functions.WindowConfig in project pulsar by apache.

the class WindowFunctionExecutorTest method testPrepareLateTupleStreamWithoutTs.

@Test
public void testPrepareLateTupleStreamWithoutTs() throws Exception {
    context = mock(Context.class);
    doReturn("test-function").when(context).getFunctionName();
    doReturn("test-namespace").when(context).getNamespace();
    doReturn("test-tenant").when(context).getTenant();
    doReturn(Collections.singleton("test-source-topic")).when(context).getInputTopics();
    doReturn("test-sink-topic").when(context).getOutputTopic();
    WindowConfig windowConfig = new WindowConfig();
    windowConfig.setWindowLengthDurationMs(20L);
    windowConfig.setSlidingIntervalDurationMs(10L);
    windowConfig.setLateDataTopic("$late");
    windowConfig.setMaxLagMs(5L);
    windowConfig.setWatermarkEmitIntervalMs(10L);
    windowConfig.setActualWindowFunctionClassName(TestFunction.class.getName());
    doReturn(Optional.of(new Gson().fromJson(new Gson().toJson(windowConfig), Map.class))).when(context).getUserConfigValue(WindowConfig.WINDOW_CONFIG_KEY);
    try {
        testWindowedPulsarFunction.process(10L, context);
        fail();
    } catch (IllegalArgumentException e) {
        assertEquals(e.getMessage(), "Late data topic can be defined only when specifying a " + "timestamp extractor class");
    }
}
Also used : WindowContext(org.apache.pulsar.functions.api.WindowContext) Context(org.apache.pulsar.functions.api.Context) WindowConfig(org.apache.pulsar.common.functions.WindowConfig) Gson(com.google.gson.Gson) Test(org.testng.annotations.Test)

Example 7 with WindowConfig

use of org.apache.pulsar.common.functions.WindowConfig in project pulsar by apache.

the class WindowFunctionExecutorTest method testExecuteWithWrongJavaWindowFunctionType.

@Test(expectedExceptions = IllegalArgumentException.class)
public void testExecuteWithWrongJavaWindowFunctionType() throws Exception {
    WindowConfig windowConfig = new WindowConfig();
    windowConfig.setActualWindowFunctionClassName(TestWrongFunction.class.getName());
    doReturn(Optional.of(new Gson().fromJson(new Gson().toJson(windowConfig), Map.class))).when(context).getUserConfigValue(WindowConfig.WINDOW_CONFIG_KEY);
    testWindowedPulsarFunction.process(10L, context);
}
Also used : WindowConfig(org.apache.pulsar.common.functions.WindowConfig) Gson(com.google.gson.Gson) Test(org.testng.annotations.Test)

Example 8 with WindowConfig

use of org.apache.pulsar.common.functions.WindowConfig in project pulsar by yahoo.

the class WindowConfigUtilsTest method testSettingSlidingCountWindow.

@Test
public void testSettingSlidingCountWindow() throws Exception {
    final Object[][] args = new Object[][] { { -1, 10 }, { 10, -1 }, { 0, 10 }, { 10, 0 }, { 0, 0 }, { -1, -1 }, { 5, 10 }, { 1, 1 }, { 10, 5 }, { 100, 10 }, { 100, 100 }, { 200, 100 }, { 500, 100 }, { null, null }, { null, 1 }, { 1, null }, { null, -1 }, { -1, null } };
    for (Object[] arg : args) {
        Object arg0 = arg[0];
        Object arg1 = arg[1];
        try {
            Integer windowLengthCount = null;
            if (arg0 != null) {
                windowLengthCount = (Integer) arg0;
            }
            Integer slidingIntervalCount = null;
            if (arg1 != null) {
                slidingIntervalCount = (Integer) arg1;
            }
            WindowConfig windowConfig = new WindowConfig();
            windowConfig.setWindowLengthCount(windowLengthCount);
            windowConfig.setSlidingIntervalCount(slidingIntervalCount);
            WindowConfigUtils.validate(windowConfig);
            if (arg0 == null) {
                fail(String.format("Window length cannot be null -- " + "windowLengthCount: %s slidingIntervalCount: %s", arg0, arg1));
            }
            if ((Integer) arg0 <= 0) {
                fail(String.format("Window length cannot be zero or less -- " + "windowLengthCount: %s slidingIntervalCount: %s", arg0, arg1));
            }
            if (arg1 != null && (Integer) arg1 <= 0) {
                fail(String.format("Sliding interval length cannot be zero or less -- " + "windowLengthCount: %s slidingIntervalCount: %s", arg0, arg1));
            }
        } catch (IllegalArgumentException e) {
            if (arg0 != null && arg1 != null && (Integer) arg0 > 0 && (Integer) arg1 > 0) {
                fail(String.format("Exception: %s thrown on valid input -- windowLengthCount: %s " + "slidingIntervalCount: %s", e.getMessage(), arg0, arg1));
            }
        }
    }
}
Also used : WindowConfig(org.apache.pulsar.common.functions.WindowConfig) Test(org.testng.annotations.Test)

Example 9 with WindowConfig

use of org.apache.pulsar.common.functions.WindowConfig in project pulsar by yahoo.

the class WindowConfigUtilsTest method testSettingTumblingTimeWindow.

@Test
public void testSettingTumblingTimeWindow() throws Exception {
    final Object[] args = new Object[] { -1L, 0L, 1L, 2L, 5L, 10L, null };
    for (Object arg : args) {
        Object arg0 = arg;
        try {
            Long windowLengthDuration = null;
            if (arg0 != null) {
                windowLengthDuration = (Long) arg0;
            }
            WindowConfig windowConfig = new WindowConfig();
            windowConfig.setWindowLengthDurationMs(windowLengthDuration);
            WindowConfigUtils.validate(windowConfig);
            if (arg0 == null) {
                fail(String.format("Window count duration cannot be null -- windowLengthDuration: %s", arg0));
            }
            if ((Long) arg0 <= 0) {
                fail(String.format("Window length cannot be zero or less -- windowLengthDuration: %s", arg0));
            }
        } catch (IllegalArgumentException e) {
            if (arg0 != null && (Long) arg0 > 0) {
                fail(String.format("Exception: %s thrown on valid input -- windowLengthDuration: %s", e.getMessage(), arg0));
            }
        }
    }
}
Also used : WindowConfig(org.apache.pulsar.common.functions.WindowConfig) Test(org.testng.annotations.Test)

Example 10 with WindowConfig

use of org.apache.pulsar.common.functions.WindowConfig in project pulsar by yahoo.

the class WindowConfigUtilsTest method testSettingTumblingCountWindow.

@Test
public void testSettingTumblingCountWindow() throws Exception {
    final Object[] args = new Object[] { -1, 0, 1, 2, 5, 10, null };
    for (Object arg : args) {
        Object arg0 = arg;
        try {
            Integer windowLengthCount = null;
            if (arg0 != null) {
                windowLengthCount = (Integer) arg0;
            }
            WindowConfig windowConfig = new WindowConfig();
            windowConfig.setWindowLengthCount(windowLengthCount);
            WindowConfigUtils.validate(windowConfig);
            if (arg0 == null) {
                fail(String.format("Window length cannot be null -- windowLengthCount: %s", arg0));
            }
            if ((Integer) arg0 <= 0) {
                fail(String.format("Window length cannot be zero or less -- windowLengthCount: %s", arg0));
            }
        } catch (IllegalArgumentException e) {
            if (arg0 != null && (Integer) arg0 > 0) {
                fail(String.format("Exception: %s thrown on valid input -- windowLengthCount: %s", e.getMessage(), arg0));
            }
        }
    }
}
Also used : WindowConfig(org.apache.pulsar.common.functions.WindowConfig) Test(org.testng.annotations.Test)

Aggregations

WindowConfig (org.apache.pulsar.common.functions.WindowConfig)51 Test (org.testng.annotations.Test)33 Gson (com.google.gson.Gson)24 FunctionConfig (org.apache.pulsar.common.functions.FunctionConfig)15 HashMap (java.util.HashMap)12 ConsumerConfig (org.apache.pulsar.common.functions.ConsumerConfig)12 ProducerConfig (org.apache.pulsar.common.functions.ProducerConfig)9 Function (org.apache.pulsar.functions.proto.Function)9 File (java.io.File)6 Type (java.lang.reflect.Type)6 Map (java.util.Map)6 Resources (org.apache.pulsar.common.functions.Resources)6 Context (org.apache.pulsar.functions.api.Context)6 WindowContext (org.apache.pulsar.functions.api.WindowContext)6 IdentityFunction (org.apache.pulsar.functions.api.utils.IdentityFunction)6 FunctionDetails (org.apache.pulsar.functions.proto.Function.FunctionDetails)6 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 TypeToken (com.google.gson.reflect.TypeToken)3 MalformedURLException (java.net.MalformedURLException)3