@Test public void testNoBufferTransportBlockingIfWritesAreWithinThreshold() { String loc = TestData.DATA_DIR + File.separator + Time.now(); final Engine engine = new Engine(loc + File.separator + "buffer", loc + File.separator + "db"); Variables.register("now", Time.now()); engine.start(); engine.add(TestData.getString(), TestData.getTObject(), TestData.getLong()); engine.add(TestData.getString(), TestData.getTObject(), TestData.getLong()); engine.stop(); Assert.assertFalse(engine.bufferTransportThreadHasEverPaused.get()); FileSystem.deleteDirectory(loc); }
@Test(timeout = 30000) public void testNoDeadlockIfTransportExceptionOccurs() throws InterruptedException { // NOTE: This test is EXPECTED to print a NoSuchFileException // stacktrace. It can be ignored. String loc = TestData.DATA_DIR + File.separator + Time.now(); final Engine engine = new Engine(loc + File.separator + "buffer", loc + File.separator + "db"); engine.start(); for (int i = 0; i < 1000; i++) { engine.accept(Write.add("foo", Convert.javaToThrift("bar"), i)); } FileSystem.deleteDirectory(loc); Thread a = new Thread( new Runnable() { @Override public void run() { engine.find("foo", Operator.EQUALS, Convert.javaToThrift("bar")); } }); Thread.sleep(2000); // this is an arbitrary amount. In 2 seconds, at // least one page should have transported... a.start(); a.join(); engine.stop(); Assert.assertTrue(true); // if we reach here, this means that the Engine // was able to break out of the transport // exception System.out.println("[INFO] You can ignore the NoSuchFileException stack trace above"); }
@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); }
@Test public void reproCON_239AuditRecord() throws InterruptedException { final Engine engine = (Engine) store; int count = TestData.getScaleCount(); for (int i = 0; i < count; i++) { engine.add(Long.toString(Time.now()), Convert.javaToThrift(i), 1); } engine.add("foo", Convert.javaToThrift("a"), 1); final AtomicBoolean done = new AtomicBoolean(false); final AtomicBoolean go = new AtomicBoolean(false); Thread write = new Thread( new Runnable() { @Override public void run() { while (!go.get()) { continue; // ensure read goes first } while (!done.get()) { if (!done.get()) { engine.add("foo", Convert.javaToThrift(Long.toString(Time.now())), 1); } } } }); final AtomicBoolean succeeded = new AtomicBoolean(true); Thread read = new Thread( new Runnable() { @Override public void run() { go.set(true); Map<Long, String> data = engine.audit(1); done.set(true); Map<Long, String> data1 = engine.audit(1); Variables.register("data_size", data.size()); Variables.register("data1_size", data1.size()); succeeded.set(data.size() == data1.size() || data.size() == data1.size() - 1); } }); read.start(); write.start(); read.join(); write.join(); Assert.assertTrue(succeeded.get()); }
@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; } }
/** * Construct a new instance. * * @param destination */ private Transaction(Engine destination) { super(new TransactionQueue(INITIAL_CAPACITY), destination); this.id = Long.toString(Time.now()); }
@Override protected Store getStore() { directory = TestData.DATA_DIR + File.separator + Time.now(); return new Engine( directory + File.separator + "buffer", directory + File.separator + "database"); }