Search in sources :

Example 6 with MockContext

use of org.apache.samza.context.MockContext in project samza by apache.

the class TestOperatorImplGraph method setup.

@Before
public void setup() {
    this.context = new MockContext();
    // individual tests can override this config if necessary
    when(this.context.getJobContext().getConfig()).thenReturn(mock(Config.class));
    TaskModel taskModel = mock(TaskModel.class);
    when(taskModel.getTaskName()).thenReturn(new TaskName("task 0"));
    when(this.context.getTaskContext().getTaskModel()).thenReturn(taskModel);
    when(this.context.getTaskContext().getTaskMetricsRegistry()).thenReturn(new MetricsRegistryMap());
    when(this.context.getContainerContext().getContainerMetricsRegistry()).thenReturn(new MetricsRegistryMap());
}
Also used : MockContext(org.apache.samza.context.MockContext) TaskName(org.apache.samza.container.TaskName) MapConfig(org.apache.samza.config.MapConfig) Config(org.apache.samza.config.Config) JobConfig(org.apache.samza.config.JobConfig) StreamConfig(org.apache.samza.config.StreamConfig) MetricsRegistryMap(org.apache.samza.metrics.MetricsRegistryMap) TaskModel(org.apache.samza.job.model.TaskModel) Before(org.junit.Before)

Example 7 with MockContext

use of org.apache.samza.context.MockContext in project samza by apache.

the class TestOperatorImpl method setup.

@Before
public void setup() {
    this.context = new MockContext();
    this.internalTaskContext = mock(InternalTaskContext.class);
    when(this.internalTaskContext.getContext()).thenReturn(this.context);
    // might be necessary in the future
    when(this.internalTaskContext.fetchObject(EndOfStreamStates.class.getName())).thenReturn(mock(EndOfStreamStates.class));
    when(this.internalTaskContext.fetchObject(WatermarkStates.class.getName())).thenReturn(mock(WatermarkStates.class));
    when(this.context.getTaskContext().getTaskMetricsRegistry()).thenReturn(new MetricsRegistryMap());
    when(this.context.getTaskContext().getTaskModel()).thenReturn(mock(TaskModel.class));
    when(this.context.getContainerContext().getContainerMetricsRegistry()).thenReturn(new MetricsRegistryMap());
}
Also used : InternalTaskContext(org.apache.samza.context.InternalTaskContext) MockContext(org.apache.samza.context.MockContext) MetricsRegistryMap(org.apache.samza.metrics.MetricsRegistryMap) TaskModel(org.apache.samza.job.model.TaskModel) Before(org.junit.Before)

Example 8 with MockContext

use of org.apache.samza.context.MockContext in project samza by apache.

the class TestStreamTableJoinOperatorImpl method testJoinFunctionIsInvokedOnlyOnce.

/**
 * Ensure join function is not invoked more than once when join function returns null on the first invocation
 */
@Test
public void testJoinFunctionIsInvokedOnlyOnce() {
    final String tableId = "testTable";
    final CountDownLatch joinInvokedLatch = new CountDownLatch(1);
    StreamTableJoinOperatorSpec mockJoinOpSpec = mock(StreamTableJoinOperatorSpec.class);
    when(mockJoinOpSpec.getTableId()).thenReturn(tableId);
    when(mockJoinOpSpec.getArgs()).thenReturn(new Object[0]);
    when(mockJoinOpSpec.getJoinFn()).thenReturn(new StreamTableJoinFunction<String, KV<String, String>, KV<String, String>, String>() {

        @Override
        public String apply(KV<String, String> message, KV<String, String> record) {
            joinInvokedLatch.countDown();
            return null;
        }

        @Override
        public String getMessageKey(KV<String, String> message) {
            return message.getKey();
        }

        @Override
        public String getRecordKey(KV<String, String> record) {
            return record.getKey();
        }
    });
    ReadWriteUpdateTable table = mock(ReadWriteUpdateTable.class);
    when(table.getAsync("1")).thenReturn(CompletableFuture.completedFuture("r1"));
    Context context = new MockContext();
    when(context.getTaskContext().getUpdatableTable(tableId)).thenReturn(table);
    MessageCollector mockMessageCollector = mock(MessageCollector.class);
    TaskCoordinator mockTaskCoordinator = mock(TaskCoordinator.class);
    StreamTableJoinOperatorImpl streamTableJoinOperator = new StreamTableJoinOperatorImpl(mockJoinOpSpec, context);
    // Table has the key
    streamTableJoinOperator.handleMessage(KV.of("1", "m1"), mockMessageCollector, mockTaskCoordinator);
    assertEquals("Join function should only be invoked once", 0, joinInvokedLatch.getCount());
}
Also used : Context(org.apache.samza.context.Context) MockContext(org.apache.samza.context.MockContext) ReadWriteUpdateTable(org.apache.samza.table.ReadWriteUpdateTable) MockContext(org.apache.samza.context.MockContext) TaskCoordinator(org.apache.samza.task.TaskCoordinator) KV(org.apache.samza.operators.KV) CountDownLatch(java.util.concurrent.CountDownLatch) MessageCollector(org.apache.samza.task.MessageCollector) StreamTableJoinOperatorSpec(org.apache.samza.operators.spec.StreamTableJoinOperatorSpec) Test(org.junit.Test)

Example 9 with MockContext

use of org.apache.samza.context.MockContext in project samza by apache.

the class TestRemoteTableEndToEnd method createMockContext.

private Context createMockContext() {
    MetricsRegistry metricsRegistry = mock(MetricsRegistry.class);
    doReturn(new Counter("")).when(metricsRegistry).newCounter(anyString(), anyString());
    doReturn(new Timer("")).when(metricsRegistry).newTimer(anyString(), anyString());
    Context context = new MockContext();
    doReturn(new MapConfig()).when(context.getJobContext()).getConfig();
    doReturn(metricsRegistry).when(context.getContainerContext()).getContainerMetricsRegistry();
    return context;
}
Also used : Context(org.apache.samza.context.Context) MockContext(org.apache.samza.context.MockContext) MetricsRegistry(org.apache.samza.metrics.MetricsRegistry) Counter(org.apache.samza.metrics.Counter) MockContext(org.apache.samza.context.MockContext) Timer(org.apache.samza.metrics.Timer) MapConfig(org.apache.samza.config.MapConfig)

Example 10 with MockContext

use of org.apache.samza.context.MockContext in project samza by apache.

the class TestCachingTable method initTables.

private void initTables(boolean isTimerMetricsDisabled, ReadWriteUpdateTable... tables) {
    Map<String, String> config = new HashMap<>();
    if (isTimerMetricsDisabled) {
        config.put(MetricsConfig.METRICS_TIMER_ENABLED, "false");
    }
    Context context = new MockContext();
    doReturn(new MapConfig(config)).when(context.getJobContext()).getConfig();
    metricsRegistry = mock(MetricsRegistry.class);
    doReturn(mock(Timer.class)).when(metricsRegistry).newTimer(anyString(), anyString());
    doReturn(mock(Counter.class)).when(metricsRegistry).newCounter(anyString(), anyString());
    doReturn(mock(Gauge.class)).when(metricsRegistry).newGauge(anyString(), any());
    doReturn(metricsRegistry).when(context.getContainerContext()).getContainerMetricsRegistry();
    Arrays.asList(tables).forEach(t -> t.init(context));
}
Also used : MockContext(org.apache.samza.context.MockContext) Context(org.apache.samza.context.Context) MetricsRegistry(org.apache.samza.metrics.MetricsRegistry) NoOpMetricsRegistry(org.apache.samza.util.NoOpMetricsRegistry) MockContext(org.apache.samza.context.MockContext) Counter(org.apache.samza.metrics.Counter) Timer(org.apache.samza.metrics.Timer) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Matchers.anyString(org.mockito.Matchers.anyString) MapConfig(org.apache.samza.config.MapConfig) Gauge(org.apache.samza.metrics.Gauge)

Aggregations

MockContext (org.apache.samza.context.MockContext)11 MapConfig (org.apache.samza.config.MapConfig)7 Context (org.apache.samza.context.Context)7 HashMap (java.util.HashMap)4 TaskModel (org.apache.samza.job.model.TaskModel)4 MetricsRegistryMap (org.apache.samza.metrics.MetricsRegistryMap)4 Counter (org.apache.samza.metrics.Counter)3 MetricsRegistry (org.apache.samza.metrics.MetricsRegistry)3 Timer (org.apache.samza.metrics.Timer)3 ReadWriteUpdateTable (org.apache.samza.table.ReadWriteUpdateTable)3 Before (org.junit.Before)3 Partition (org.apache.samza.Partition)2 TaskName (org.apache.samza.container.TaskName)2 Gauge (org.apache.samza.metrics.Gauge)2 KV (org.apache.samza.operators.KV)2 StreamTableJoinOperatorSpec (org.apache.samza.operators.spec.StreamTableJoinOperatorSpec)2 IntegerSerde (org.apache.samza.serializers.IntegerSerde)2 KVSerde (org.apache.samza.serializers.KVSerde)2 SystemStreamPartition (org.apache.samza.system.SystemStreamPartition)2 MessageCollector (org.apache.samza.task.MessageCollector)2