{
try
{
if (dataInfo.isClose())
{
stream.data(new StringDataInfo("close stream", true));
closeBarrier.await(5, TimeUnit.SECONDS);
}
streamDataSent.countDown();
if (pushStreamDataReceived.getCount() == 2)
{
Stream pushStream = stream.push(new PushInfo(new Fields(), false));
streamExchanger.exchange(pushStream, 5, TimeUnit.SECONDS);
}
}
catch (Exception e)
{
exceptionCountDownLatch.countDown();
}
}
};
}
catch (Exception e)
{
exceptionCountDownLatch.countDown();
throw new IllegalStateException(e);
}
}
}), null);
Stream stream = clientSession.syn(new SynInfo(new Fields(), false), new StreamFrameListener.Adapter()
{
@Override
public StreamFrameListener onPush(Stream stream, PushInfo pushInfo)
{
pushStreamSynLatch.countDown();
return new StreamFrameListener.Adapter()
{
@Override
public void onData(Stream stream, DataInfo dataInfo)
{
pushStreamDataReceived.countDown();
super.onData(stream, dataInfo);
}
};
}
@Override
public void onReply(Stream stream, ReplyInfo replyInfo)
{
try
{
replyBarrier.await(5, TimeUnit.SECONDS);
}
catch (Exception e)
{
exceptionCountDownLatch.countDown();
}
}
@Override
public void onData(Stream stream, DataInfo dataInfo)
{
try
{
closeBarrier.await(5, TimeUnit.SECONDS);
}
catch (Exception e)
{
exceptionCountDownLatch.countDown();
}
}
});
replyBarrier.await(5, TimeUnit.SECONDS);
stream.data(new StringDataInfo("client data", false));
Stream pushStream = streamExchanger.exchange(null, 5, TimeUnit.SECONDS);
pushStream.data(new StringDataInfo("first push data frame", false));
// nasty, but less complex than using another cyclicBarrier for example
while (pushStreamDataReceived.getCount() != 1)
Thread.sleep(1);
stream.data(new StringDataInfo("client close", true));
closeBarrier.await(5, TimeUnit.SECONDS);
assertThat("stream is closed", stream.isClosed(), is(true));
pushStream.data(new StringDataInfo("second push data frame while associated stream has been closed already", false));
assertThat("2 pushStream data frames have been received.", pushStreamDataReceived.await(5, TimeUnit.SECONDS), is(true));
assertThat("2 data frames have been sent", streamDataSent.await(5, TimeUnit.SECONDS), is(true));
assertThatNoExceptionOccurred(exceptionCountDownLatch);
}