@Test public void testTerminalType() throws Exception { final Consumer<String> assertTerm = new Consumer<String>() { @Override public void accept(String term) { if (term.equals("xterm") || term.equals("vt100")) { testComplete(); } else { fail("Unexpected term " + term); } } }; server(new Consumer<TtyConnection>() { @Override public void accept(TtyConnection conn) { if (conn.terminalType() != null) { assertTerm.accept(conn.terminalType()); } else { conn.setTerminalTypeHandler(assertTerm); } } }); assertConnect("xterm"); assertWrite("bye"); await(); }
private void testClientDisconnect(boolean clean) throws Exception { final CountDownLatch disconnectLatch = new CountDownLatch(1); final CountDownLatch closedLatch = new CountDownLatch(1); server(new Consumer<TtyConnection>() { @Override public void accept(TtyConnection conn) { disconnectLatch.countDown(); conn.setCloseHandler(new Consumer<Void>() { @Override public void accept(Void v) { closedLatch.countDown(); } }); } }); assertConnect(); assertTrue(disconnectLatch.await(10, TimeUnit.SECONDS)); assertDisconnect(clean); assertTrue(closedLatch.await(10, TimeUnit.SECONDS)); }
@Test public void testRead() throws Exception { final ArrayBlockingQueue<int[]> queue = new ArrayBlockingQueue<int[]>(1); server(new Consumer<TtyConnection>() { @Override public void accept(final TtyConnection conn) { conn.setStdinHandler(new Consumer<int[]>() { @Override public void accept(int[] data) { queue.add(data); conn.stdoutHandler().accept(new int[]{'h', 'e', 'l', 'l', 'o'}); } }); } }); assertConnect(); assertWriteln(""); int[] data = queue.poll(10, TimeUnit.SECONDS); assertTrue(Arrays.equals(new int[]{'\r'}, data)); assertEquals("hello", assertReadString(5)); }
@Test public void testWrite() throws Exception { final AtomicInteger requestCount = new AtomicInteger(); server(new Consumer<TtyConnection>() { @Override public void accept(TtyConnection conn) { requestCount.incrementAndGet(); conn.stdoutHandler().accept(new int[]{'%', ' '}); } }); assertConnect(); assertEquals("% ", assertReadString(2)); assertEquals(1, requestCount.get()); }
@Test public void testResize() throws Exception { server(new Consumer<TtyConnection>() { @Override public void accept(final TtyConnection conn) { conn.setSizeHandler(new Consumer<Vector>() { @Override public void accept(Vector size) { assertEquals(40, conn.size().x()); assertEquals(12, conn.size().y()); testComplete(); } }); } }); assertConnect(); resize(40, 12); await(); }
@Test public void testConnectionClose() throws Exception { AtomicInteger closeCount = new AtomicInteger(); server(conn -> { conn.setCloseHandler(v -> { if (closeCount.incrementAndGet() > 1) { fail("Closed call several times"); } else { testComplete(); } }); conn.setStdinHandler(text -> conn.close()); }); assertConnect(); assertWrite("bye"); await(); }
@Test public void testResize() throws Exception { server(conn -> { conn.setSizeHandler(size -> { assertEquals(40, conn.size().x()); assertEquals(12, conn.size().y()); testComplete(); }); } ); assertConnect(); resize(40, 12); await(); }
@Test public void testLastAccessedTime() throws Exception { CountDownLatch latch = new CountDownLatch(1); AtomicInteger count = new AtomicInteger(); long connTime = System.currentTimeMillis(); server(conn -> { assertTrue(conn.lastAccessedTime() >= connTime); long openTime = System.currentTimeMillis(); conn.setStdinHandler(cp -> { long delta = conn.lastAccessedTime() - openTime; switch (count.getAndIncrement()) { case 0: assertTrue(delta >= 0); latch.countDown(); break; case 1: assertTrue(delta >= 10); testComplete(); break; } }); }); assertConnect(); Thread.sleep(15); assertWrite("hello"); awaitLatch(latch); Thread.sleep(15); assertWrite("byebye"); await(); }
@Test public void testScheduleThread() throws Exception { server(conn -> { Thread connThread = Thread.currentThread(); conn.execute(() -> { Thread schedulerThread = Thread.currentThread(); try { assertThreading(connThread, schedulerThread); } catch (Exception e) { fail(e); } testComplete(); }); }); assertConnect(); await(); }
@Test public void testServerDisconnect() throws Exception { final CountDownLatch closedLatch = new CountDownLatch(1); server(new Consumer<TtyConnection>() { @Override public void accept(final TtyConnection conn) { conn.setStdinHandler(new Consumer<int[]>() { @Override public void accept(int[] bytes) { conn.close(); } }); conn.setCloseHandler(new Consumer<Void>() { @Override public void accept(Void v) { closedLatch.countDown(); } }); } }); assertConnect(); assertWrite("whatever"); assertTrue(closedLatch.await(10, TimeUnit.SECONDS)); }
@Test public void testScheduleThread() throws Exception { server(new Consumer<TtyConnection>() { @Override public void accept(TtyConnection conn) { final Thread connThread = Thread.currentThread(); conn.execute(new Runnable() { @Override public void run() { Thread schedulerThread = Thread.currentThread(); try { assertThreading(connThread, schedulerThread); } catch (Exception e) { fail(e); } testComplete(); } }); } }); assertConnect(); await(); }
protected final void assertWrite(int... codePoints) throws Exception { assertWrite(Helper.fromCodePoints(codePoints)); }
protected void assertConnect() throws Exception { assertConnect(null); }
@Override public void accept(String term) { if (term.equals("xterm") || term.equals("vt100")) { testComplete(); } else { fail("Unexpected term " + term); } } };
@Override public void accept(final TtyConnection conn) { assertTrue(conn.lastAccessedTime() >= connTime); final long openTime = System.currentTimeMillis(); conn.setStdinHandler(new Consumer<int[]>() { @Override public void accept(int[] cp) { long delta = conn.lastAccessedTime() - openTime; switch (count.getAndIncrement()) { case 0: assertTrue(delta >= 0); latch.countDown(); break; case 1: assertTrue(delta >= 10); testComplete(); break; } } }); } });
@Override public void accept(final TtyConnection conn) { if (conn.size() != null) { assertEquals(80, conn.size().x()); assertEquals(24, conn.size().y()); testComplete(); } else { conn.setSizeHandler(new Consumer<Vector>() { @Override public void accept(Vector size) { assertEquals(80, conn.size().x()); assertEquals(24, conn.size().y()); testComplete(); } }); } } });
@Test public void testConnectionClose() throws Exception { AtomicInteger closeCount = new AtomicInteger(); server(conn -> { conn.setCloseHandler(v -> { if (closeCount.incrementAndGet() > 1) { fail("Closed call several times"); } else { testComplete(); } }); conn.setStdinHandler(text -> conn.close()); }); assertConnect(); assertWrite("bye"); await(); }
@Test public void testResize() throws Exception { server(conn -> { conn.setSizeHandler(size -> { assertEquals(40, conn.size().x()); assertEquals(12, conn.size().y()); testComplete(); }); } ); assertConnect(); resize(40, 12); await(); }
@Test public void testDifferentCharset() throws Exception { charset = Charset.forName("ISO-8859-1"); server(new Consumer<TtyConnection>() { @Override public void accept(TtyConnection conn) { // 20AC does not exists in ISO_8859_1 and is replaced by `?` conn.write("\u20AC"); } }); assertConnect(); String s = assertReadString(1); assertEquals("?", s); } }
@Test public void testLastAccessedTime() throws Exception { CountDownLatch latch = new CountDownLatch(1); AtomicInteger count = new AtomicInteger(); long connTime = System.currentTimeMillis(); server(conn -> { assertTrue(conn.lastAccessedTime() >= connTime); long openTime = System.currentTimeMillis(); conn.setStdinHandler(cp -> { long delta = conn.lastAccessedTime() - openTime; switch (count.getAndIncrement()) { case 0: assertTrue(delta >= 0); latch.countDown(); break; case 1: assertTrue(delta >= 10); testComplete(); break; } }); }); assertConnect(); Thread.sleep(15); assertWrite("hello"); awaitLatch(latch); Thread.sleep(15); assertWrite("byebye"); await(); }