Example #1
0
  private static StreamTask<?, ?> createMockTaskWithTimer(
      final ScheduledExecutorService timerService, final Object lock) {
    StreamTask<?, ?> task = mock(StreamTask.class);
    when(task.getAccumulatorMap()).thenReturn(new HashMap<String, Accumulator<?, ?>>());
    when(task.getName()).thenReturn("Test task name");
    when(task.getExecutionConfig()).thenReturn(new ExecutionConfig());
    when(task.getEnvironment())
        .thenReturn(
            new MockEnvironment("MockTask", 3 * 1024 * 1024, new MockInputSplitProvider(), 1024));
    when(task.getCheckpointLock()).thenReturn(lock);

    doAnswer(
            new Answer<Void>() {
              @Override
              public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
                final Long timestamp = (Long) invocationOnMock.getArguments()[0];
                final Triggerable target = (Triggerable) invocationOnMock.getArguments()[1];
                timerService.schedule(
                    new Callable<Object>() {
                      @Override
                      public Object call() throws Exception {
                        synchronized (lock) {
                          target.trigger(timestamp);
                        }
                        return null;
                      }
                    },
                    timestamp - System.currentTimeMillis(),
                    TimeUnit.MILLISECONDS);
                return null;
              }
            })
        .when(task)
        .registerTimer(anyLong(), any(Triggerable.class));

    // ugly Java generic hacks to get the generic state backend into the mock
    @SuppressWarnings("unchecked")
    OngoingStubbing<StateBackend<?>> stubbing =
        (OngoingStubbing<StateBackend<?>>) (OngoingStubbing<?>) when(task.getStateBackend());
    stubbing.thenReturn(MemoryStateBackend.defaultInstance());

    return task;
  }
  private static StreamTask<?, ?> createMockTask() {
    StreamTask<?, ?> task = mock(StreamTask.class);
    when(task.getAccumulatorMap()).thenReturn(new HashMap<String, Accumulator<?, ?>>());
    when(task.getName()).thenReturn("Test task name");
    when(task.getExecutionConfig()).thenReturn(new ExecutionConfig());

    Environment env = mock(Environment.class);
    when(env.getTaskInfo()).thenReturn(new TaskInfo("Test task name", 0, 1, 0));
    when(env.getUserClassLoader())
        .thenReturn(AggregatingAlignedProcessingTimeWindowOperatorTest.class.getClassLoader());

    when(task.getEnvironment()).thenReturn(env);

    // ugly java generic hacks to get the state backend into the mock
    @SuppressWarnings("unchecked")
    OngoingStubbing<StateBackend<?>> stubbing =
        (OngoingStubbing<StateBackend<?>>) (OngoingStubbing<?>) when(task.getStateBackend());
    stubbing.thenReturn(MemoryStateBackend.defaultInstance());

    return task;
  }