Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 多线程测试_Java_Multithreading_Testing - Fatal编程技术网

Java 多线程测试

Java 多线程测试,java,multithreading,testing,Java,Multithreading,Testing,我的任务是首先编写一个多线程客户机-服务器应用程序,然后使用大量客户机(100个客户机,每个客户机发送1000条消息)对其进行测试。因此,我有正常工作的控制台客户端服务器。客户端有两个线程,一个用于输入,另一个用于输出。现在我开始编写测试。在我看来,它的工作模式应该是这样的:执行应该等待接受新客户机的服务器线程,然后我将执行InputThreads(将其连接到服务器)并在循环中写入测试协议。我说得对吗 所以我写了这样的东西: public class ServerLoadTest { p

我的任务是首先编写一个多线程客户机-服务器应用程序,然后使用大量客户机(100个客户机,每个客户机发送1000条消息)对其进行测试。因此,我有正常工作的控制台客户端服务器。客户端有两个线程,一个用于输入,另一个用于输出。现在我开始编写测试。在我看来,它的工作模式应该是这样的:执行应该等待接受新客户机的服务器线程,然后我将执行InputThreads(将其连接到服务器)并在循环中写入测试协议。我说得对吗

所以我写了这样的东西:

public class ServerLoadTest {
    private static final Logger LOG = Logger.getLogger(ServerLoadTest.class);
    private ExecutorService clientExec = Executors.newFixedThreadPool(100);
    private ExecutorService serverExec = Executors.newFixedThreadPool(100);

    @Test
    public void test() throws IOException, JAXBException, XMLStreamException, ParserConfigurationException, SAXException, InterruptedException {        
        LOG.trace("Start testing");     
        serverExec.execute(new TestServerThread());     

        for (int i = 0; i < 100; i++) { 
            clientExec.execute(new TestClientThread());
        }

        Assert.assertTrue(true);
        LOG.trace("All working fine");
        clientExec.shutdown();
    }

}


class TestClientThread implements Runnable {
    private static final Logger LOG = Logger.getLogger(TestClientThread.class);
    private ExecutorService outputExec = Executors.newFixedThreadPool(2);

    public TestClientThread() {
        new Thread(this);
    }

    @Override
    public void run() {

        try {
            LOG.trace("Starting Socket");
            Socket s = new Socket("localhost", 4444);
            OutputThread spamming = new OutputThread(s, new PrintWriter(s.getOutputStream(), true), new BufferedReader(
                    new InputStreamReader(s.getInputStream())));
            exec.execute(spamming);

            spamming.getOut().println("HO HO Ho HO HO");

            InputThread getSpamAnswer = new InputThread(s, new BufferedReader(new InputStreamReader(s.getInputStream())));
            outputExec.execute(getSpamAnswer);

        } catch (IOException | JAXBException | XMLStreamException | ParserConfigurationException | SAXException e) {
            e.printStackTrace();
        }
    }
}

class TestServerThread implements Runnable {
    private Server king = mock(Server.class);

    public TestServerThread() {
        new Thread(this);
    }

    @SuppressWarnings("static-access")
    @Override
    public void run() {
        try {
            king.main(null);
        } catch (IOException | JAXBException | ParserConfigurationException | SAXException e) {
            Assert.assertFalse(false);
        }
    }
}
公共类ServerLoadTest{
私有静态最终记录器LOG=Logger.getLogger(ServerLoadTest.class);
private ExecutorService clientExec=Executors.newFixedThreadPool(100);
私有ExecutorService serverExec=Executors.newFixedThreadPool(100);
@试验
public void test()抛出IOException、JAXBEException、XMLStreamException、ParserConfiguration异常、SAXException、InterruptedException{
日志跟踪(“开始测试”);
execute(新的TestServerThread());
对于(int i=0;i<100;i++){
execute(newtestclientthread());
}
Assert.assertTrue(true);
日志跟踪(“所有工作罚款”);
clientExec.shutdown();
}
}
类TestClientThread实现Runnable{
私有静态最终记录器LOG=Logger.getLogger(TestClientThread.class);
私有ExecutorService outputExec=Executors.newFixedThreadPool(2);
公共TestClientThread(){
新线程(本);
}
@凌驾
公开募捐{
试一试{
LOG.trace(“起始套接字”);
套接字s=新套接字(“localhost”,4444);
OutputThread spamming=新的OutputThread(s,新的PrintWriter(s.getOutputStream(),true),新的BufferedReader(
新的InputStreamReader(s.getInputStream());
执行(垃圾邮件);
spamming.getOut().println(“hoho”);
InputThread getSpamAnswer=新的InputThread(s,新的BufferedReader(新的InputStreamReader(s.getInputStream()));
outputExec.execute(getSpamAnswer);
}捕获(IOException | JAXBEException | XMLStreamException | ParserConfiguration异常| SAXE异常){
e、 printStackTrace();
}
}
}
类TestServerThread实现可运行{
私有服务器king=mock(Server.class);
公共TestServerThread(){
新线程(本);
}
@抑制警告(“静态访问”)
@凌驾
公开募捐{
试一试{
king.main(空);
}捕获(IOException | JAXBEException | ParserConfiguration异常| SAXE异常){
Assert.assertFalse(false);
}
}
}
首先,服务器上有很多LOG.trace,但我在控制台中没有任何LOG.trace,当我调试时,我收到一个异常,我的客户端无法连接(我认为它没有时间进行此操作)。我应该如何同步这个


p.S.服务器是多线程的,支持多个客户端。现在我只想从源代码对其进行测试。

您的防火墙是否打开,允许在该端口上进行传入连接

此外,您并没有真正复制客户机-服务器体系结构,因为只有一个套接字由所有“客户机”线程共享。这可能是错误的来源(多个线程访问同一个对象,在本例中是一个流)


请参阅Oracle提供的有关如何使用单独的进程和套接字正确设置客户机/服务器演示程序的文档:这将允许您模拟实际发生的情况,并对代码进行了详细解释

是的,我已经看过了。我的服务器不仅支持一个客户端。我认为问题在测试中。服务器未及时启动。java单元测试中是否有测试多线程的工具。在代码开始时,您启动一个服务器线程,然后在同一进程中启动多个客户端线程。这并不反映客户机/服务器体系结构的正常工作方式,也不是进行测试的良好基础。它们应该是独立的程序。此外,如果您担心ServerSocket没有及时准备好,那么我建议您在
run()
main()
的一开始就创建套接字,以便它立即进行侦听。如果这还不够快,您可以在创建客户机之前调用
sleep()
一秒钟。我提供的文档的底部演示了如何让服务器一次接受多个客户机连接—它确实包含您需要的信息。