@Test
  public void testContinueOnSomeDbDirectoriesMissing() throws Exception {
    File targetDir1 = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
    File targetDir2 = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());

    try {
      assertTrue(targetDir1.mkdirs());
      assertTrue(targetDir2.mkdirs());

      if (!targetDir1.setWritable(false, false)) {
        System.err.println(
            "Cannot execute 'testContinueOnSomeDbDirectoriesMissing' because cannot mark directory non-writable");
        return;
      }

      RocksDBStateBackend rocksDbBackend = new RocksDBStateBackend(TEMP_URI);
      rocksDbBackend.setDbStoragePaths(targetDir1.getAbsolutePath(), targetDir2.getAbsolutePath());

      try {
        rocksDbBackend.initializeForJob(getMockEnvironment(), "foobar", IntSerializer.INSTANCE);
      } catch (Exception e) {
        e.printStackTrace();
        fail("Backend initialization failed even though some paths were available");
      }
    } finally {
      //noinspection ResultOfMethodCallIgnored
      targetDir1.setWritable(true, false);
      FileUtils.deleteDirectory(targetDir1);
      FileUtils.deleteDirectory(targetDir2);
    }
  }
Esempio n. 2
0
    @Test
    public void testProcessZuulFilter() {

      ZuulRunner runner = new ZuulRunner();
      runner = spy(runner);
      RequestContext context = spy(RequestContext.getCurrentContext());

      try {
        FilterProcessor.setProcessor(processor);
        RequestContext.testSetCurrentContext(context);
        when(servletResponse.getWriter()).thenReturn(writer);

        runner.init(servletRequest, servletResponse);
        verify(runner, times(1)).init(servletRequest, servletResponse);
        assertTrue(
            RequestContext.getCurrentContext().getRequest() instanceof HttpServletRequestWrapper);
        assertEquals(RequestContext.getCurrentContext().getResponse(), servletResponse);

        runner.preRoute();
        verify(processor, times(1)).preRoute();

        runner.postRoute();
        verify(processor, times(1)).postRoute();
        //                verify(context, times(1)).unset();

        runner.route();
        verify(processor, times(1)).route();
        RequestContext.testSetCurrentContext(null);

      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  @Test
  public void testKeyValueStateInWindowFunction() {
    final ScheduledExecutorService timerService = Executors.newSingleThreadScheduledExecutor();
    try {
      final CollectingOutput<Integer> out = new CollectingOutput<>(50);
      final Object lock = new Object();
      final StreamTask<?, ?> mockTask = createMockTaskWithTimer(timerService, lock);

      StatefulFunction.globalCounts.clear();

      // tumbling window that triggers every 20 milliseconds
      AccumulatingProcessingTimeWindowOperator<Integer, Integer, Integer> op =
          new AccumulatingProcessingTimeWindowOperator<>(
              new StatefulFunction(),
              identitySelector,
              IntSerializer.INSTANCE,
              IntSerializer.INSTANCE,
              50,
              50);

      op.setup(mockTask, createTaskConfig(identitySelector, IntSerializer.INSTANCE), out);
      op.open();

      synchronized (lock) {
        op.processElement(new StreamRecord<Integer>(1));
        op.processElement(new StreamRecord<Integer>(2));
      }
      out.waitForNElements(2, 60000);

      synchronized (lock) {
        op.processElement(new StreamRecord<Integer>(1));
        op.processElement(new StreamRecord<Integer>(2));
        op.processElement(new StreamRecord<Integer>(1));
        op.processElement(new StreamRecord<Integer>(1));
        op.processElement(new StreamRecord<Integer>(2));
        op.processElement(new StreamRecord<Integer>(2));
      }
      out.waitForNElements(8, 60000);

      List<Integer> result = out.getElements();
      assertEquals(8, result.size());

      Collections.sort(result);
      assertEquals(Arrays.asList(1, 1, 1, 1, 2, 2, 2, 2), result);

      assertEquals(4, StatefulFunction.globalCounts.get(1).intValue());
      assertEquals(4, StatefulFunction.globalCounts.get(2).intValue());

      synchronized (lock) {
        op.close();
      }
      op.dispose();
    } catch (Exception e) {
      e.printStackTrace();
      fail(e.getMessage());
    } finally {
      timerService.shutdown();
    }
  }
Esempio n. 4
0
 public static void load() {
   try {
     load(false);
   } catch (Exception e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   }
 }
Esempio n. 5
0
 @Theory
 public void testParseDate(TestData data) {
   try {
     assertThat(testee.parseDate(data.getFile()), is(equalTo(data.getExpectedDate())));
   } catch (Exception ex) {
     ex.printStackTrace(System.err);
     throw ex;
   }
 }
Esempio n. 6
0
 @Test
 public void testMostRecentFile() {
   try {
     assertThat(
         testee.getMostRecent(filesInTestDir), is(equalTo(DataItems.DIR_FILE_2030.getFile())));
   } catch (Exception ex) {
     ex.printStackTrace(System.err);
     throw ex;
   }
 }
  @Test
  public void testPropagateExceptionsFromClose() {
    final ScheduledExecutorService timerService = Executors.newSingleThreadScheduledExecutor();
    try {
      final CollectingOutput<Integer> out = new CollectingOutput<>();
      final Object lock = new Object();
      final StreamTask<?, ?> mockTask = createMockTaskWithTimer(timerService, lock);

      WindowFunction<Integer, Integer, Integer, TimeWindow> failingFunction =
          new FailingFunction(100);

      // the operator has a window time that is so long that it will not fire in this test
      final long hundredYears = 100L * 365 * 24 * 60 * 60 * 1000;
      AccumulatingProcessingTimeWindowOperator<Integer, Integer, Integer> op =
          new AccumulatingProcessingTimeWindowOperator<>(
              failingFunction,
              identitySelector,
              IntSerializer.INSTANCE,
              IntSerializer.INSTANCE,
              hundredYears,
              hundredYears);

      op.setup(mockTask, new StreamConfig(new Configuration()), out);
      op.open();

      for (int i = 0; i < 150; i++) {
        synchronized (lock) {
          op.processElement(new StreamRecord<Integer>(i));
        }
      }

      try {
        synchronized (lock) {
          op.close();
        }
        fail("This should fail with an exception");
      } catch (Exception e) {
        assertTrue(
            e.getMessage().contains("Artificial Test Exception")
                || (e.getCause() != null
                    && e.getCause().getMessage().contains("Artificial Test Exception")));
      }

      op.dispose();
    } catch (Exception e) {
      e.printStackTrace();
      fail(e.getMessage());
    } finally {
      timerService.shutdown();
    }
  }
  @Test
  public void testTumblingWindow() {
    final ScheduledExecutorService timerService = Executors.newSingleThreadScheduledExecutor();
    try {
      final int windowSize = 50;
      final CollectingOutput<Integer> out = new CollectingOutput<>(windowSize);
      final Object lock = new Object();
      final StreamTask<?, ?> mockTask = createMockTaskWithTimer(timerService, lock);

      // tumbling window that triggers every 20 milliseconds
      AccumulatingProcessingTimeWindowOperator<Integer, Integer, Integer> op =
          new AccumulatingProcessingTimeWindowOperator<>(
              validatingIdentityFunction,
              identitySelector,
              IntSerializer.INSTANCE,
              IntSerializer.INSTANCE,
              windowSize,
              windowSize);

      op.setup(mockTask, new StreamConfig(new Configuration()), out);
      op.open();

      final int numElements = 1000;

      for (int i = 0; i < numElements; i++) {
        synchronized (lock) {
          op.processElement(new StreamRecord<Integer>(i));
        }
        Thread.sleep(1);
      }

      synchronized (lock) {
        op.close();
      }
      op.dispose();

      // get and verify the result
      List<Integer> result = out.getElements();
      assertEquals(numElements, result.size());

      Collections.sort(result);
      for (int i = 0; i < numElements; i++) {
        assertEquals(i, result.get(i).intValue());
      }
    } catch (Exception e) {
      e.printStackTrace();
      fail(e.getMessage());
    } finally {
      timerService.shutdown();
    }
  }
  @Test
  public void testInvalidParameters() {
    try {
      assertInvalidParameter(-1L, -1L);
      assertInvalidParameter(10000L, -1L);
      assertInvalidParameter(-1L, 1000L);
      assertInvalidParameter(1000L, 2000L);

      // actual internal slide is too low here:
      assertInvalidParameter(1000L, 999L);
    } catch (Exception e) {
      e.printStackTrace();
      fail(e.getMessage());
    }
  }
  @Test
  public void testSlidingWindowSingleElements() {
    final ScheduledExecutorService timerService = Executors.newSingleThreadScheduledExecutor();

    try {
      final CollectingOutput<Integer> out = new CollectingOutput<>(50);
      final Object lock = new Object();
      final StreamTask<?, ?> mockTask = createMockTaskWithTimer(timerService, lock);

      // tumbling window that triggers every 20 milliseconds
      AccumulatingProcessingTimeWindowOperator<Integer, Integer, Integer> op =
          new AccumulatingProcessingTimeWindowOperator<>(
              validatingIdentityFunction,
              identitySelector,
              IntSerializer.INSTANCE,
              IntSerializer.INSTANCE,
              150,
              50);

      op.setup(mockTask, new StreamConfig(new Configuration()), out);
      op.open();

      synchronized (lock) {
        op.processElement(new StreamRecord<Integer>(1));
        op.processElement(new StreamRecord<Integer>(2));
      }

      // each element should end up in the output three times
      // wait until the elements have arrived 6 times in the output
      out.waitForNElements(6, 120000);

      List<Integer> result = out.getElements();
      assertEquals(6, result.size());

      Collections.sort(result);
      assertEquals(Arrays.asList(1, 1, 1, 2, 2, 2), result);

      synchronized (lock) {
        op.close();
      }
      op.dispose();
    } catch (Exception e) {
      e.printStackTrace();
      fail(e.getMessage());
    } finally {
      timerService.shutdown();
    }
  }
  @Test
  public void testEmitTrailingDataOnClose() {
    final ScheduledExecutorService timerService = Executors.newSingleThreadScheduledExecutor();
    try {
      final CollectingOutput<Integer> out = new CollectingOutput<>();
      final Object lock = new Object();
      final StreamTask<?, ?> mockTask = createMockTaskWithTimer(timerService, lock);

      // the operator has a window time that is so long that it will not fire in this test
      final long oneYear = 365L * 24 * 60 * 60 * 1000;
      AccumulatingProcessingTimeWindowOperator<Integer, Integer, Integer> op =
          new AccumulatingProcessingTimeWindowOperator<>(
              validatingIdentityFunction,
              identitySelector,
              IntSerializer.INSTANCE,
              IntSerializer.INSTANCE,
              oneYear,
              oneYear);

      op.setup(mockTask, new StreamConfig(new Configuration()), out);
      op.open();

      List<Integer> data = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
      for (Integer i : data) {
        synchronized (lock) {
          op.processElement(new StreamRecord<Integer>(i));
        }
      }

      synchronized (lock) {
        op.close();
      }
      op.dispose();

      // get and verify the result
      List<Integer> result = out.getElements();
      Collections.sort(result);
      assertEquals(data, result);
    } catch (Exception e) {
      e.printStackTrace();
      fail(e.getMessage());
    } finally {
      timerService.shutdown();
    }
  }
  public boolean tearDown() {
    /*
    List<MultiverseWorld> worlds = new ArrayList<MultiverseWorld>(core.getMVWorldManager()
            .getMVWorlds());
    for (MultiverseWorld world : worlds) {
        core.getMVWorldManager().deleteWorld(world.getName());
    }
    */

    Server maybeNullServer = getServer();
    PluginManager maybeNullPluginManager = maybeNullServer.getPluginManager();
    Plugin plugin = maybeNullPluginManager.getPlugin("Multiverse-Inventories");
    // Plugin plugin = getServer().getPluginManager().getPlugin("Multiverse-Inventories");
    MultiverseInventories inventories = (MultiverseInventories) plugin;
    inventories.onDisable();

    MockWorldFactory.clearWorlds();

    plugin = getServer().getPluginManager().getPlugin("Multiverse-Core");
    MultiverseCore core = (MultiverseCore) plugin;
    core.onDisable();

    try {
      Field serverField = Bukkit.class.getDeclaredField("server");
      serverField.setAccessible(true);
      serverField.set(Class.forName("org.bukkit.Bukkit"), null);
    } catch (Exception e) {
      Util.log(
          Level.SEVERE,
          "Error while trying to unregister the server from Bukkit. Has Bukkit changed?");
      e.printStackTrace();
      Assert.fail(e.getMessage());
      return false;
    }

    return true;
  }
Esempio n. 13
0
    @Test
    public void testUnsubscribeAfterTake() {
      Subscription s = mock(Subscription.class);
      TestObservable w = new TestObservable(s, "one", "two", "three");

      @SuppressWarnings("unchecked")
      Observer<String> aObserver = mock(Observer.class);
      Observable<String> take = Observable.create(take(w, 1));
      take.subscribe(aObserver);

      // wait for the Observable to complete
      try {
        w.t.join();
      } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
      }

      System.out.println("TestObservable thread finished");
      verify(aObserver, times(1)).onNext("one");
      verify(aObserver, never()).onNext("two");
      verify(aObserver, never()).onNext("three");
      verify(s, times(1)).unsubscribe();
    }
  @Test
  public void testReadDataFromEavesdrop() {

    try {
      String exampleString = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">";
      exampleString += "<html>";
      exampleString += "</html>";

      URLConnection connection = mock(URLConnection.class); // Create mock dependency: mock()

      InputStream i = new ByteArrayInputStream(exampleString.getBytes(StandardCharsets.UTF_8));

      when(connection.getInputStream()).thenReturn(i); // Setting up the expectations

      String result = englishEditor.readDataFromEavesdrop(connection);

      assertEquals(exampleString, result);

      verify(connection).getInputStream();

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  @Test
  public void checkpointRestoreWithPendingWindowSliding() {
    final ScheduledExecutorService timerService = Executors.newSingleThreadScheduledExecutor();
    try {
      final int factor = 4;
      final int windowSlide = 50;
      final int windowSize = factor * windowSlide;

      final CollectingOutput<Integer> out = new CollectingOutput<>(windowSlide);
      final Object lock = new Object();
      final StreamTask<?, ?> mockTask = createMockTaskWithTimer(timerService, lock);

      // sliding window (200 msecs) every 50 msecs
      AccumulatingProcessingTimeWindowOperator<Integer, Integer, Integer> op =
          new AccumulatingProcessingTimeWindowOperator<>(
              validatingIdentityFunction,
              identitySelector,
              IntSerializer.INSTANCE,
              IntSerializer.INSTANCE,
              windowSize,
              windowSlide);

      op.setup(mockTask, new StreamConfig(new Configuration()), out);
      op.open();

      // inject some elements
      final int numElements = 1000;
      final int numElementsFirst = 700;

      for (int i = 0; i < numElementsFirst; i++) {
        synchronized (lock) {
          op.processElement(new StreamRecord<Integer>(i));
        }
        Thread.sleep(1);
      }

      // draw a snapshot
      StreamTaskState state;
      List<Integer> resultAtSnapshot;
      synchronized (lock) {
        int beforeSnapShot = out.getElements().size();
        state = op.snapshotOperatorState(1L, System.currentTimeMillis());
        resultAtSnapshot = new ArrayList<>(out.getElements());
        int afterSnapShot = out.getElements().size();
        assertEquals(
            "operator performed computation during snapshot", beforeSnapShot, afterSnapShot);
      }

      assertTrue(resultAtSnapshot.size() <= factor * numElementsFirst);

      // inject the remaining elements - these should not influence the snapshot
      for (int i = numElementsFirst; i < numElements; i++) {
        synchronized (lock) {
          op.processElement(new StreamRecord<Integer>(i));
        }
        Thread.sleep(1);
      }

      op.dispose();

      // re-create the operator and restore the state
      final CollectingOutput<Integer> out2 = new CollectingOutput<>(windowSlide);
      op =
          new AccumulatingProcessingTimeWindowOperator<>(
              validatingIdentityFunction,
              identitySelector,
              IntSerializer.INSTANCE,
              IntSerializer.INSTANCE,
              windowSize,
              windowSlide);

      op.setup(mockTask, new StreamConfig(new Configuration()), out2);
      op.restoreState(state, 1);
      op.open();

      // inject again the remaining elements
      for (int i = numElementsFirst; i < numElements; i++) {
        synchronized (lock) {
          op.processElement(new StreamRecord<Integer>(i));
        }
        Thread.sleep(1);
      }

      // for a deterministic result, we need to wait until all pending triggers
      // have fired and emitted their results
      long deadline = System.currentTimeMillis() + 120000;
      do {
        Thread.sleep(20);
      } while (resultAtSnapshot.size() + out2.getElements().size() < factor * numElements
          && System.currentTimeMillis() < deadline);

      synchronized (lock) {
        op.close();
      }
      op.dispose();

      // get and verify the result
      List<Integer> finalResult = new ArrayList<>(resultAtSnapshot);
      finalResult.addAll(out2.getElements());
      assertEquals(factor * numElements, finalResult.size());

      Collections.sort(finalResult);
      for (int i = 0; i < factor * numElements; i++) {
        assertEquals(i / factor, finalResult.get(i).intValue());
      }
    } catch (Exception e) {
      e.printStackTrace();
      fail(e.getMessage());
    } finally {
      timerService.shutdown();
    }
  }
  @Test
  public void testWindowSizeAndSlide() {
    try {
      AccumulatingProcessingTimeWindowOperator<String, String, String> op;

      op =
          new AccumulatingProcessingTimeWindowOperator<>(
              mockFunction,
              mockKeySelector,
              StringSerializer.INSTANCE,
              StringSerializer.INSTANCE,
              5000,
              1000);
      assertEquals(5000, op.getWindowSize());
      assertEquals(1000, op.getWindowSlide());
      assertEquals(1000, op.getPaneSize());
      assertEquals(5, op.getNumPanesPerWindow());

      op =
          new AccumulatingProcessingTimeWindowOperator<>(
              mockFunction,
              mockKeySelector,
              StringSerializer.INSTANCE,
              StringSerializer.INSTANCE,
              1000,
              1000);
      assertEquals(1000, op.getWindowSize());
      assertEquals(1000, op.getWindowSlide());
      assertEquals(1000, op.getPaneSize());
      assertEquals(1, op.getNumPanesPerWindow());

      op =
          new AccumulatingProcessingTimeWindowOperator<>(
              mockFunction,
              mockKeySelector,
              StringSerializer.INSTANCE,
              StringSerializer.INSTANCE,
              1500,
              1000);
      assertEquals(1500, op.getWindowSize());
      assertEquals(1000, op.getWindowSlide());
      assertEquals(500, op.getPaneSize());
      assertEquals(3, op.getNumPanesPerWindow());

      op =
          new AccumulatingProcessingTimeWindowOperator<>(
              mockFunction,
              mockKeySelector,
              StringSerializer.INSTANCE,
              StringSerializer.INSTANCE,
              1200,
              1100);
      assertEquals(1200, op.getWindowSize());
      assertEquals(1100, op.getWindowSlide());
      assertEquals(100, op.getPaneSize());
      assertEquals(12, op.getNumPanesPerWindow());
    } catch (Exception e) {
      e.printStackTrace();
      fail(e.getMessage());
    }
  }
  public boolean setUp() {
    try {
      FileUtils.deleteFolder(invDirectory);
      FileUtils.deleteFolder(serverDirectory);
      invDirectory.mkdirs();
      Assert.assertTrue(invDirectory.exists());

      MockGateway.MOCK_STANDARD_METHODS = false;

      plugin = PowerMockito.spy(new MultiverseInventories());
      core = PowerMockito.spy(new MultiverseCore());

      // Let's let all MV files go to bin/test
      doReturn(invDirectory).when(plugin).getDataFolder();
      // Let's let all MV files go to bin/test
      doReturn(coreDirectory).when(core).getDataFolder();

      // Return a fake PDF file.
      PluginDescriptionFile pdf =
          PowerMockito.spy(
              new PluginDescriptionFile(
                  "Multiverse-Inventories",
                  "2.4-test",
                  "com.onarandombox.multiverseinventories.MultiverseInventories"));
      when(pdf.getAuthors()).thenReturn(new ArrayList<String>());
      doReturn(pdf).when(plugin).getDescription();
      doReturn(core).when(plugin).getCore();
      doReturn(true).when(plugin).isEnabled();
      PluginDescriptionFile pdfCore =
          PowerMockito.spy(
              new PluginDescriptionFile(
                  "Multiverse-Core", "2.2-Test", "com.onarandombox.MultiverseCore.MultiverseCore"));
      when(pdfCore.getAuthors()).thenReturn(new ArrayList<String>());
      doReturn(pdfCore).when(core).getDescription();
      doReturn(true).when(core).isEnabled();
      plugin.setServerFolder(serverDirectory);

      // Add Core to the list of loaded plugins
      JavaPlugin[] plugins = new JavaPlugin[] {plugin, core};

      // Mock the Plugin Manager
      PluginManager mockPluginManager = PowerMockito.mock(PluginManager.class);
      when(mockPluginManager.getPlugins()).thenReturn(plugins);
      when(mockPluginManager.getPlugin("Multiverse-Inventories")).thenReturn(plugin);
      when(mockPluginManager.getPlugin("Multiverse-Core")).thenReturn(core);
      when(mockPluginManager.getPermission(anyString())).thenReturn(null);

      // Make some fake folders to fool the fake MV into thinking these worlds exist
      File worldNormalFile = new File(plugin.getServerFolder(), "world");
      Util.log("Creating world-folder: " + worldNormalFile.getAbsolutePath());
      worldNormalFile.mkdirs();
      MockWorldFactory.makeNewMockWorld("world", Environment.NORMAL, WorldType.NORMAL);
      File worldNetherFile = new File(plugin.getServerFolder(), "world_nether");
      Util.log("Creating world-folder: " + worldNetherFile.getAbsolutePath());
      worldNetherFile.mkdirs();
      MockWorldFactory.makeNewMockWorld("world_nether", Environment.NETHER, WorldType.NORMAL);
      File worldSkylandsFile = new File(plugin.getServerFolder(), "world_the_end");
      Util.log("Creating world-folder: " + worldSkylandsFile.getAbsolutePath());
      worldSkylandsFile.mkdirs();
      MockWorldFactory.makeNewMockWorld("world_the_end", Environment.THE_END, WorldType.NORMAL);
      File world2File = new File(plugin.getServerFolder(), "world2");
      Util.log("Creating world-folder: " + world2File.getAbsolutePath());
      world2File.mkdirs();
      MockWorldFactory.makeNewMockWorld("world2", Environment.NORMAL, WorldType.NORMAL);

      // Initialize the Mock server.
      mockServer = mock(Server.class);
      when(mockServer.getName()).thenReturn("TestBukkit");
      Logger.getLogger("Minecraft").setParent(Util.logger);
      when(mockServer.getLogger()).thenReturn(Util.logger);
      when(mockServer.getWorldContainer()).thenReturn(worldsDirectory);
      when(plugin.getServer()).thenReturn(mockServer);
      when(core.getServer()).thenReturn(mockServer);
      when(mockServer.getPluginManager()).thenReturn(mockPluginManager);
      Answer<Player> playerAnswer =
          new Answer<Player>() {
            public Player answer(InvocationOnMock invocation) throws Throwable {
              String arg;
              try {
                arg = (String) invocation.getArguments()[0];
              } catch (Exception e) {
                return null;
              }
              Player player = players.get(arg);
              if (player == null) {
                player = new MockPlayer(arg, mockServer);
                players.put(arg, player);
              }
              return player;
            }
          };
      when(mockServer.getPlayer(anyString())).thenAnswer(playerAnswer);
      when(mockServer.getOfflinePlayer(anyString())).thenAnswer(playerAnswer);
      when(mockServer.getOfflinePlayers())
          .thenAnswer(
              new Answer<OfflinePlayer[]>() {
                public OfflinePlayer[] answer(InvocationOnMock invocation) throws Throwable {
                  return players.values().toArray(new Player[players.values().size()]);
                }
              });
      when(mockServer.getOnlinePlayers())
          .thenAnswer(
              new Answer<Player[]>() {
                public Player[] answer(InvocationOnMock invocation) throws Throwable {
                  return players.values().toArray(new Player[players.values().size()]);
                }
              });

      // Give the server some worlds
      when(mockServer.getWorld(anyString()))
          .thenAnswer(
              new Answer<World>() {
                public World answer(InvocationOnMock invocation) throws Throwable {
                  String arg;
                  try {
                    arg = (String) invocation.getArguments()[0];
                  } catch (Exception e) {
                    return null;
                  }
                  return MockWorldFactory.getWorld(arg);
                }
              });

      when(mockServer.getWorld(any(UUID.class)))
          .thenAnswer(
              new Answer<World>() {
                @Override
                public World answer(InvocationOnMock invocation) throws Throwable {
                  UUID arg;
                  try {
                    arg = (UUID) invocation.getArguments()[0];
                  } catch (Exception e) {
                    return null;
                  }
                  return MockWorldFactory.getWorld(arg);
                }
              });

      when(mockServer.getWorlds())
          .thenAnswer(
              new Answer<List<World>>() {
                public List<World> answer(InvocationOnMock invocation) throws Throwable {
                  return MockWorldFactory.getWorlds();
                }
              });

      when(mockServer.createWorld(Matchers.isA(WorldCreator.class)))
          .thenAnswer(
              new Answer<World>() {
                public World answer(InvocationOnMock invocation) throws Throwable {
                  WorldCreator arg;
                  try {
                    arg = (WorldCreator) invocation.getArguments()[0];
                  } catch (Exception e) {
                    return null;
                  }
                  // Add special case for creating null worlds.
                  // Not sure I like doing it this way, but this is a special case
                  if (arg.name().equalsIgnoreCase("nullworld")) {
                    return MockWorldFactory.makeNewNullMockWorld(
                        arg.name(), arg.environment(), arg.type());
                  }
                  return MockWorldFactory.makeNewMockWorld(
                      arg.name(), arg.environment(), arg.type());
                }
              });

      when(mockServer.unloadWorld(anyString(), anyBoolean())).thenReturn(true);

      // add mock scheduler
      BukkitScheduler mockScheduler = mock(BukkitScheduler.class);
      when(mockScheduler.scheduleSyncDelayedTask(any(Plugin.class), any(Runnable.class), anyLong()))
          .thenAnswer(
              new Answer<Integer>() {
                public Integer answer(InvocationOnMock invocation) throws Throwable {
                  Runnable arg;
                  try {
                    arg = (Runnable) invocation.getArguments()[1];
                  } catch (Exception e) {
                    return null;
                  }
                  arg.run();
                  return null;
                }
              });
      when(mockScheduler.scheduleSyncDelayedTask(any(Plugin.class), any(Runnable.class)))
          .thenAnswer(
              new Answer<Integer>() {
                public Integer answer(InvocationOnMock invocation) throws Throwable {
                  Runnable arg;
                  try {
                    arg = (Runnable) invocation.getArguments()[1];
                  } catch (Exception e) {
                    return null;
                  }
                  arg.run();
                  return null;
                }
              });
      when(mockServer.getScheduler()).thenReturn(mockScheduler);

      // Set InventoriesListener
      InventoriesListener il = PowerMockito.spy(new InventoriesListener(plugin));
      Field inventoriesListenerField =
          MultiverseInventories.class.getDeclaredField("inventoriesListener");
      inventoriesListenerField.setAccessible(true);
      inventoriesListenerField.set(plugin, il);

      // Set Core
      Field coreField = MultiverseInventories.class.getDeclaredField("core");
      coreField.setAccessible(true);
      coreField.set(plugin, core);

      // Set server
      Field serverfield = JavaPlugin.class.getDeclaredField("server");
      serverfield.setAccessible(true);
      serverfield.set(plugin, mockServer);

      // Set worldManager
      WorldManager wm = PowerMockito.spy(new WorldManager(core));
      Field worldmanagerfield = MultiverseCore.class.getDeclaredField("worldManager");
      worldmanagerfield.setAccessible(true);
      worldmanagerfield.set(core, wm);

      // Set playerListener
      MVPlayerListener pl = PowerMockito.spy(new MVPlayerListener(core));
      Field playerlistenerfield = MultiverseCore.class.getDeclaredField("playerListener");
      playerlistenerfield.setAccessible(true);
      playerlistenerfield.set(core, pl);

      // Set entityListener
      MVEntityListener el = PowerMockito.spy(new MVEntityListener(core));
      Field entitylistenerfield = MultiverseCore.class.getDeclaredField("entityListener");
      entitylistenerfield.setAccessible(true);
      entitylistenerfield.set(core, el);

      // Set weatherListener
      MVWeatherListener wl = PowerMockito.spy(new MVWeatherListener(core));
      Field weatherlistenerfield = MultiverseCore.class.getDeclaredField("weatherListener");
      weatherlistenerfield.setAccessible(true);
      weatherlistenerfield.set(core, wl);

      // Init our command sender
      final Logger commandSenderLogger = Logger.getLogger("CommandSender");
      commandSenderLogger.setParent(Util.logger);
      commandSender = mock(CommandSender.class);
      doAnswer(
              new Answer<Void>() {
                public Void answer(InvocationOnMock invocation) throws Throwable {
                  commandSenderLogger.info(
                      ChatColor.stripColor((String) invocation.getArguments()[0]));
                  return null;
                }
              })
          .when(commandSender)
          .sendMessage(anyString());
      when(commandSender.getServer()).thenReturn(mockServer);
      when(commandSender.getName()).thenReturn("MockCommandSender");
      when(commandSender.isPermissionSet(anyString())).thenReturn(true);
      when(commandSender.isPermissionSet(Matchers.isA(Permission.class))).thenReturn(true);
      when(commandSender.hasPermission(anyString())).thenReturn(true);
      when(commandSender.hasPermission(Matchers.isA(Permission.class))).thenReturn(true);
      when(commandSender.addAttachment(plugin)).thenReturn(null);
      when(commandSender.isOp()).thenReturn(true);

      Bukkit.setServer(mockServer);

      // Load Multiverse Core
      core.onLoad();
      plugin.onLoad();

      // Enable it.
      core.onEnable();
      plugin.onEnable();

      return true;
    } catch (Exception e) {
      e.printStackTrace();
    }

    return false;
  }
  @Test
  public void testWindowTriggerTimeAlignment() {
    try {
      @SuppressWarnings("unchecked")
      final Output<StreamRecord<String>> mockOut = mock(Output.class);
      final StreamTask<?, ?> mockTask = createMockTask();

      AccumulatingProcessingTimeWindowOperator<String, String, String> op;

      op =
          new AccumulatingProcessingTimeWindowOperator<>(
              mockFunction,
              mockKeySelector,
              StringSerializer.INSTANCE,
              StringSerializer.INSTANCE,
              5000,
              1000);
      op.setup(mockTask, new StreamConfig(new Configuration()), mockOut);
      op.open();
      assertTrue(op.getNextSlideTime() % 1000 == 0);
      assertTrue(op.getNextEvaluationTime() % 1000 == 0);
      op.dispose();

      op =
          new AccumulatingProcessingTimeWindowOperator<>(
              mockFunction,
              mockKeySelector,
              StringSerializer.INSTANCE,
              StringSerializer.INSTANCE,
              1000,
              1000);
      op.setup(mockTask, new StreamConfig(new Configuration()), mockOut);
      op.open();
      assertTrue(op.getNextSlideTime() % 1000 == 0);
      assertTrue(op.getNextEvaluationTime() % 1000 == 0);
      op.dispose();

      op =
          new AccumulatingProcessingTimeWindowOperator<>(
              mockFunction,
              mockKeySelector,
              StringSerializer.INSTANCE,
              StringSerializer.INSTANCE,
              1500,
              1000);
      op.setup(mockTask, new StreamConfig(new Configuration()), mockOut);
      op.open();
      assertTrue(op.getNextSlideTime() % 500 == 0);
      assertTrue(op.getNextEvaluationTime() % 1000 == 0);
      op.dispose();

      op =
          new AccumulatingProcessingTimeWindowOperator<>(
              mockFunction,
              mockKeySelector,
              StringSerializer.INSTANCE,
              StringSerializer.INSTANCE,
              1200,
              1100);
      op.setup(mockTask, new StreamConfig(new Configuration()), mockOut);
      op.open();
      assertTrue(op.getNextSlideTime() % 100 == 0);
      assertTrue(op.getNextEvaluationTime() % 1100 == 0);
      op.dispose();
    } catch (Exception e) {
      e.printStackTrace();
      fail(e.getMessage());
    }
  }
  @Test
  public void testSlidingWindow() {
    final ScheduledExecutorService timerService = Executors.newSingleThreadScheduledExecutor();
    try {
      final CollectingOutput<Integer> out = new CollectingOutput<>(50);
      final Object lock = new Object();
      final StreamTask<?, ?> mockTask = createMockTaskWithTimer(timerService, lock);

      // tumbling window that triggers every 20 milliseconds
      AccumulatingProcessingTimeWindowOperator<Integer, Integer, Integer> op =
          new AccumulatingProcessingTimeWindowOperator<>(
              validatingIdentityFunction,
              identitySelector,
              IntSerializer.INSTANCE,
              IntSerializer.INSTANCE,
              150,
              50);

      op.setup(mockTask, new StreamConfig(new Configuration()), out);
      op.open();

      final int numElements = 1000;

      for (int i = 0; i < numElements; i++) {
        synchronized (lock) {
          op.processElement(new StreamRecord<Integer>(i));
        }
        Thread.sleep(1);
      }

      synchronized (lock) {
        op.close();
      }
      op.dispose();

      // get and verify the result
      List<Integer> result = out.getElements();

      // if we kept this running, each element would be in the result three times (for each slide).
      // we are closing the window before the final panes are through three times, so we may have
      // less
      // elements.
      if (result.size() < numElements || result.size() > 3 * numElements) {
        fail("Wrong number of results: " + result.size());
      }

      Collections.sort(result);
      int lastNum = -1;
      int lastCount = -1;

      for (int num : result) {
        if (num == lastNum) {
          lastCount++;
          assertTrue(lastCount <= 3);
        } else {
          lastNum = num;
          lastCount = 1;
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
      fail(e.getMessage());
    } finally {
      timerService.shutdown();
    }
  }
  @Test
  public void checkpointRestoreWithPendingWindowTumbling() {
    final ScheduledExecutorService timerService = Executors.newSingleThreadScheduledExecutor();
    try {
      final int windowSize = 200;
      final CollectingOutput<Integer> out = new CollectingOutput<>(windowSize);
      final Object lock = new Object();
      final StreamTask<?, ?> mockTask = createMockTaskWithTimer(timerService, lock);

      // tumbling window that triggers every 50 milliseconds
      AccumulatingProcessingTimeWindowOperator<Integer, Integer, Integer> op =
          new AccumulatingProcessingTimeWindowOperator<>(
              validatingIdentityFunction,
              identitySelector,
              IntSerializer.INSTANCE,
              IntSerializer.INSTANCE,
              windowSize,
              windowSize);

      op.setup(mockTask, new StreamConfig(new Configuration()), out);
      op.open();

      // inject some elements
      final int numElementsFirst = 700;
      final int numElements = 1000;
      for (int i = 0; i < numElementsFirst; i++) {
        synchronized (lock) {
          op.processElement(new StreamRecord<Integer>(i));
        }
        Thread.sleep(1);
      }

      // draw a snapshot and dispose the window
      StreamTaskState state;
      List<Integer> resultAtSnapshot;
      synchronized (lock) {
        int beforeSnapShot = out.getElements().size();
        state = op.snapshotOperatorState(1L, System.currentTimeMillis());
        resultAtSnapshot = new ArrayList<>(out.getElements());
        int afterSnapShot = out.getElements().size();
        assertEquals(
            "operator performed computation during snapshot", beforeSnapShot, afterSnapShot);
        assertTrue(afterSnapShot <= numElementsFirst);
      }

      // inject some random elements, which should not show up in the state
      for (int i = 0; i < 300; i++) {
        synchronized (lock) {
          op.processElement(new StreamRecord<Integer>(i + numElementsFirst));
        }
        Thread.sleep(1);
      }

      op.dispose();

      // re-create the operator and restore the state
      final CollectingOutput<Integer> out2 = new CollectingOutput<>(windowSize);
      op =
          new AccumulatingProcessingTimeWindowOperator<>(
              validatingIdentityFunction,
              identitySelector,
              IntSerializer.INSTANCE,
              IntSerializer.INSTANCE,
              windowSize,
              windowSize);

      op.setup(mockTask, new StreamConfig(new Configuration()), out2);
      op.restoreState(state, 1);
      op.open();

      // inject some more elements
      for (int i = numElementsFirst; i < numElements; i++) {
        synchronized (lock) {
          op.processElement(new StreamRecord<Integer>(i));
        }
        Thread.sleep(1);
      }

      synchronized (lock) {
        op.close();
      }
      op.dispose();

      // get and verify the result
      List<Integer> finalResult = new ArrayList<>(resultAtSnapshot);
      finalResult.addAll(out2.getElements());
      assertEquals(numElements, finalResult.size());

      Collections.sort(finalResult);
      for (int i = 0; i < numElements; i++) {
        assertEquals(i, finalResult.get(i).intValue());
      }
    } catch (Exception e) {
      e.printStackTrace();
      fail(e.getMessage());
    } finally {
      timerService.shutdown();
    }
  }