"longer Test Data. This is significantly longer Test Data. This is significantly "+
"longer Test Data. This is significantly longer Test Data. This is significantly "+
"longer Test Data. This is significantly longer Test Data. This is significantly "+
"longer Test Data. This is significantly longer Test Data. This is significantly "+
"longer Test Data. ";
Session session = createSession();
final ServerSocket ss = new ServerSocket(0);
int forwardedPort = ss.getLocalPort();
int sinkPort = getFreePort();
session.setPortForwardingR(sinkPort, "localhost", forwardedPort);
final boolean started[] = new boolean[1];
started[0] = false;
final AtomicInteger conCount = new AtomicInteger(0);
new Thread() {
public void run() {
started[0] = true;
try {
for (int i = 0; i < NUM_ITERATIONS; ++i) {
Socket s = ss.accept();
conCount.incrementAndGet();
s.getOutputStream().write(PAYLOAD.getBytes());
s.getOutputStream().flush();
s.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
Thread.sleep(50);
Assert.assertTrue("Server not started", started[0]);
final boolean lenOK[] = new boolean[NUM_ITERATIONS];
final boolean dataOK[] = new boolean[NUM_ITERATIONS];
for ( int i = 0; i < NUM_ITERATIONS; i++) {
final int ii = i;
Socket s = null;
try {
s = new Socket("localhost", sinkPort);
byte b1[] = new byte[PAYLOAD.length() / 2];
byte b2[] = new byte[PAYLOAD.length()];
int read1 = s.getInputStream().read(b1);
Thread.sleep(50);
int read2 = s.getInputStream().read(b2);
lenOK[ii] = PAYLOAD.length() == read1 + read2;
dataOK[ii] = PAYLOAD.equals(new String(b1, 0, read1) + new String(b2, 0, read2));
if (!lenOK[ii] || !dataOK[ii] ) {
throw new Exception("Bad data");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (s != null) {
s.close();
}
}
}
int ok = 0;
for (int i = 0; i < NUM_ITERATIONS; i++) {
ok += lenOK[i] ? 1 : 0;
}
Thread.sleep(50);
for (int i = 0; i < NUM_ITERATIONS; i++) {
Assert.assertTrue(lenOK[i]);
Assert.assertTrue(dataOK[i]);
}
session.delPortForwardingR(forwardedPort);
}