use of ch.cyberduck.core.pool.PooledSessionFactory in project cyberduck by iterate-ch.
the class FTPConcurrentTransferWorkerTest method testTransferredSizeRepeat.
@Test
public void testTransferredSizeRepeat() throws Exception {
final Local local = new Local(System.getProperty("java.io.tmpdir"), new AlphanumericRandomStringService().random());
final byte[] content = new byte[98305];
new Random().nextBytes(content);
final OutputStream out = local.getOutputStream(false);
IOUtils.write(content, out);
out.close();
final AtomicBoolean failed = new AtomicBoolean();
final Path test = new Path(new DefaultHomeFinderService(session).find(), new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.file));
final Transfer t = new UploadTransfer(session.getHost(), test, local);
final BytecountStreamListener counter = new BytecountStreamListener();
final LoginConnectionService connect = new LoginConnectionService(new DisabledLoginCallback() {
@Override
public void warn(final Host bookmark, final String title, final String message, final String continueButton, final String disconnectButton, final String preference) {
//
}
@Override
public Credentials prompt(final Host bookmark, final String username, final String title, final String reason, final LoginOptions options) {
return new Credentials("test", "test");
}
}, new DisabledHostKeyCallback(), new DisabledPasswordStore(), new DisabledProgressListener());
final DefaultSessionPool pool = new DefaultSessionPool(connect, new DefaultVaultRegistry(new DisabledPasswordCallback()), new DisabledTranscriptListener(), session.getHost(), new GenericObjectPool<>(new PooledSessionFactory(connect, new DisabledX509TrustManager(), new DefaultX509KeyManager(), session.getHost(), new DefaultVaultRegistry(new DisabledPasswordCallback())) {
@Override
public Session create() {
return new FTPSession(session.getHost()) {
final FTPWriteFeature write = new FTPWriteFeature(this) {
@Override
public StatusOutputStream<Void> write(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException {
final StatusOutputStream<Void> proxy = super.write(file, status, callback);
if (failed.get()) {
// Second attempt successful
return proxy;
}
return new VoidStatusOutputStream(new CountingOutputStream(proxy) {
@Override
protected void afterWrite(final int n) throws IOException {
super.afterWrite(n);
if (this.getByteCount() >= 42768L) {
// Buffer size
assertEquals(32768L, counter.getSent());
failed.set(true);
throw new SocketTimeoutException();
}
}
}) {
@Override
public Void getStatus() throws BackgroundException {
return proxy.getStatus();
}
};
}
};
@Override
@SuppressWarnings("unchecked")
public <T> T _getFeature(final Class<T> type) {
if (type == Write.class) {
return (T) write;
}
return super._getFeature(type);
}
};
}
}));
final ConcurrentTransferWorker worker = new ConcurrentTransferWorker(pool, SessionPool.DISCONNECTED, t, new TransferOptions(), new TransferSpeedometer(t), new DisabledTransferPrompt() {
@Override
public TransferAction prompt(final TransferItem file) {
return TransferAction.overwrite;
}
}, new DisabledTransferErrorCallback(), new DisabledConnectionCallback(), new DisabledProgressListener(), counter, new DisabledNotificationService());
assertTrue(worker.run(session));
local.delete();
assertTrue(t.isComplete());
assertEquals(content.length, new DefaultAttributesFinderFeature(session).find(test).getSize());
assertEquals(content.length, counter.getRecv(), 0L);
assertEquals(content.length, counter.getSent(), 0L);
assertTrue(failed.get());
new FTPDeleteFeature(session).delete(Collections.singletonList(test), new DisabledLoginCallback(), new Delete.DisabledCallback());
}
Aggregations