我有大约 100 个 JUnit 测试来模拟客户端与服务器的套接字连接。它们看起来像这样:
@Test
public void testProtocolInACertainWay() throws Exception {
Socket socket = _socketFactory.createSocket(_host, _port); // SSLSocketFactory
// Send payload
DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
outputStream.write(/* test-specific payload */);
outputStream.flush();
// Receive response
DataInputStream inputStream = new DataInputStream(socket.getInputStream());
socket.setSoTimeout(5000);
byte[] buffer = new byte[512];
int numBytesRead = inputStream.read(buffer);
buffer = ArrayUtils.subarray(buffer, 0, numBytesRead);
// Assert test-specific stuff on response
Assert.assertTrue(buffer[0] == (byte)1); // for example
/* At this point, depending on the test, we either repeat similar steps with different payloads or end the test */
}
现在,我希望能够从服务器上运行这些测试(或子集),一次 150 万。这意味着我想同时发送 150 万个套接字写入,全部读取它们,并断言它们的响应。
有没有一种方法可以做到这一点而不必重写所有 100 个 JUnit 测试?(请说是的,所以:))
谢谢!