@Test public void testBufferTransportThreadWillRestartIfHung() { int frequency = Engine.BUFFER_TRANSPORT_THREAD_HUNG_DETECTION_FREQUENCY_IN_MILLISECONDS; int threshold = Engine.BUFFER_TRANSPORT_THREAD_HUNG_DETECTION_THRESOLD_IN_MILLISECONDS; final AtomicBoolean done = new AtomicBoolean(false); try { Engine.BUFFER_TRANSPORT_THREAD_HUNG_DETECTION_FREQUENCY_IN_MILLISECONDS = 100; Engine.BUFFER_TRANSPORT_THREAD_HUNG_DETECTION_THRESOLD_IN_MILLISECONDS = 500; int lag = 5000; String loc = TestData.DATA_DIR + File.separator + Time.now(); final Engine engine = new Engine(loc + File.separator + "buffer", loc + File.separator + "db"); engine.bufferTransportThreadSleepInMs = Engine.BUFFER_TRANSPORT_THREAD_HUNG_DETECTION_THRESOLD_IN_MILLISECONDS + lag; engine.start(); Thread thread = new Thread( new Runnable() { @Override public void run() { while (!done.get()) { engine.add(TestData.getString(), TestData.getTObject(), TestData.getLong()); } } }); thread.start(); Threads.sleep( (int) (1.2 * Engine.BUFFER_TRANSPORT_THREAD_HUNG_DETECTION_THRESOLD_IN_MILLISECONDS) + Engine.BUFFER_TRANSPORT_THREAD_HUNG_DETECTION_FREQUENCY_IN_MILLISECONDS); Assert.assertTrue(engine.bufferTransportThreadHasEverAppearedHung.get()); Threads.sleep( (int) (Engine.BUFFER_TRANSPORT_THREAD_HUNG_DETECTION_THRESOLD_IN_MILLISECONDS * 1.2)); Assert.assertTrue(engine.bufferTransportThreadHasEverBeenRestarted.get()); engine.stop(); FileSystem.deleteDirectory(loc); } finally { done.set(true); Engine.BUFFER_TRANSPORT_THREAD_HUNG_DETECTION_FREQUENCY_IN_MILLISECONDS = frequency; Engine.BUFFER_TRANSPORT_THREAD_HUNG_DETECTION_THRESOLD_IN_MILLISECONDS = threshold; } }
@Test public void testBufferTransportBlockingIfWritesAreNotWithinThreshold() { String loc = TestData.DATA_DIR + File.separator + Time.now(); final Engine engine = new Engine(loc + File.separator + "buffer", loc + File.separator + "db"); engine.start(); engine.add(TestData.getString(), TestData.getTObject(), TestData.getLong()); Threads.sleep( Engine.BUFFER_TRANSPORT_THREAD_ALLOWABLE_INACTIVITY_THRESHOLD_IN_MILLISECONDS + 10); engine.add(TestData.getString(), TestData.getTObject(), TestData.getLong()); Assert.assertTrue(engine.bufferTransportThreadHasEverPaused.get()); engine.stop(); FileSystem.deleteDirectory(loc); }