@Override public void go() throws Exception { try (FSDataOutputStream stream = fs.create(path, WriteMode.OVERWRITE)) { assertTrue(fs.getNumberOfOpenOutputStreams() <= maxConcurrentOutputStreams); assertTrue(fs.getTotalNumberOfOpenStreams() <= maxConcurrentStreamsTotal); final Random rnd = new Random(); final byte[] data = new byte[rnd.nextInt(10000) + 1]; rnd.nextBytes(data); stream.write(data); } } }
@Override public void go() throws Exception { try (FSDataOutputStream stream = fs.create(path, WriteMode.OVERWRITE)) { assertTrue(fs.getNumberOfOpenOutputStreams() <= maxConcurrentOutputStreams); assertTrue(fs.getTotalNumberOfOpenStreams() <= maxConcurrentStreamsTotal); final Random rnd = new Random(); final byte[] data = new byte[rnd.nextInt(10000) + 1]; rnd.nextBytes(data); stream.write(data); waitTillWokenUp(); // try to write one more thing, which might/should fail with an I/O exception stream.write(rnd.nextInt()); } } }
@Test public void testDelegateOutStreamMethods() throws IOException { // mock the output stream final FSDataOutputStream mockOut = mock(FSDataOutputStream.class); final long outPos = 46651L; when(mockOut.getPos()).thenReturn(outPos); final FileSystem fs = mock(FileSystem.class); when(fs.create(any(Path.class), any(WriteMode.class))).thenReturn(mockOut); final LimitedConnectionsFileSystem lfs = new LimitedConnectionsFileSystem(fs, 100); final FSDataOutputStream out = lfs.create(mock(Path.class), WriteMode.OVERWRITE); // validate the output stream out.write(77); verify(mockOut).write(77); { byte[] bytes = new byte[1786]; out.write(bytes, 100, 111); verify(mockOut).write(bytes, 100, 111); } assertEquals(outPos, out.getPos()); out.flush(); verify(mockOut).flush(); out.sync(); verify(mockOut).sync(); out.close(); verify(mockOut).close(); }
try (FSDataOutputStream out = fs.create(new Path(tempFolder.newFile().toURI()), WriteMode.OVERWRITE)) {
limitedFs.create(new Path(tempFolder.newFile().toURI()), WriteMode.OVERWRITE); fail("this should have timed out");
@Test public void testFailingStreamsUnregister() throws Exception { final LimitedConnectionsFileSystem fs = new LimitedConnectionsFileSystem(new FailFs(), 1); assertEquals(0, fs.getNumberOfOpenInputStreams()); assertEquals(0, fs.getNumberOfOpenOutputStreams()); assertEquals(0, fs.getTotalNumberOfOpenStreams()); try { fs.open(new Path(tempFolder.newFile().toURI())); fail("this is expected to fail with an exception"); } catch (IOException e) { // expected } try { fs.create(new Path(tempFolder.newFile().toURI()), WriteMode.NO_OVERWRITE); fail("this is expected to fail with an exception"); } catch (IOException e) { // expected } assertEquals(0, fs.getNumberOfOpenInputStreams()); assertEquals(0, fs.getNumberOfOpenOutputStreams()); assertEquals(0, fs.getTotalNumberOfOpenStreams()); }