ObservableRedableByteChannel implements ReadableByteChannel { @Override public void close() throws IOException { try { this.channel.close(); } finally { this.listener.close(); } } ObservableRedableByteChannel(ReadableByteChannel channel, TransferListener listener); @Override boolean isOpen(); @Override int read(ByteBuffer dst); @Override void close();  }
@Test public void testClose() throws IOException { EventLogHttpTransferListener listener = new EventLogHttpTransferListener(); ReadableByteChannel channel = Channels.newChannel(new ByteArrayInputStream(new byte[10])); ReadableByteChannel in = new ObservableRedableByteChannel(channel, listener); in.close(); assertFalse(in.isOpen()); assertEquals(of(Closed.INSTANCE), listener.getEventLog()); }
@Test public void testCloseFailed() throws IOException { EventLogHttpTransferListener listener = new EventLogHttpTransferListener(); ReadableByteChannel in = mock(ReadableByteChannel.class); doThrow(new IOException("test")).when(in).close(); ReadableByteChannel oin = new ObservableRedableByteChannel(in, listener); try { oin.close(); fail("expected exception not thrown"); } catch (IOException e) { assertEquals("test", e.getMessage()); } assertEquals(of(Closed.INSTANCE), listener.getEventLog()); }