how do I test socket server application?

View: New views
5 Messages — Rating Filter:   Alert me  

how do I test socket server application?

by youhaodeyi :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


I have a server/client application and want to use junit to test it. How do I setup a server side junit test case?

thanks.


Re: how do I test socket server application?

by Mike Forsberg :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

JUnit is agnostic, or doesn't know, where it runs.

You would write the test cases just as any other test.

If I had some more information I could help a little better.

Big Mike

On Sat, Oct 10, 2009 at 1:41 AM, youhaodeyi <youhaodeyi@...> wrote:

>
> I have a server/client application and want to use junit to test it. How do I setup a server side junit test case?
>
> thanks.
>
>
>
> ------------------------------------
>
> Yahoo! Groups Links
>
>
>
>

Re: how do I test socket server application?

by youhaodeyi :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

What my problem is that I create a thread to listen on a port but the program will exist not block. Below is my code

@Before
public void setUp() throws Exception{
try {
                        (new Thread(new Runnable(){
                                public void run() {
                                        try{
                                        server=new ServerSocket(3000);
                                        server.accept();
                                        }catch(Exception ex){
                                                ex.printStackTrace();
                                        }
                                };
                        })).start();
                } catch (Exception e) {
                        e.printStackTrace();
                }
               
        }
}
@Test
public void test1(){
         ...
}
Mike Forsberg wrote:
JUnit is agnostic, or doesn't know, where it runs.

You would write the test cases just as any other test.

If I had some more information I could help a little better.

Big Mike

On Sat, Oct 10, 2009 at 1:41 AM, youhaodeyi <youhaodeyi@gmail.com> wrote:
>
> I have a server/client application and want to use junit to test it. How do I setup a server side junit test case?
>
> thanks.
>
>
>
> ------------------------------------
>
> Yahoo! Groups Links
>
>
>
>

Re: how do I test socket server application?

by michaellufhl :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Your ServerSocket is accepting in a new thread which is blocked, while main thread is keeping running...

Michael




________________________________
From: youhaodeyi <youhaodeyi@...>
To: junit@...
Sent: Mon, November 16, 2009 2:38:48 PM
Subject: Re: [junit] how do I test socket server application?

 

What my problem is that I create a thread to listen on a port but the program
will exist not block. Below is my code

@Before
public void setUp() throws Exception{
try {
(new Thread(new Runnable(){
public void run() {
try{
server=new ServerSocket( 3000);
server.accept( );
}catch(Exception ex){
ex.printStackTrace( );
}
};
})).start();
} catch (Exception e) {
e.printStackTrace( );
}

}
}
@Test
public void test1(){
...
}

Mike Forsberg wrote:

>
> JUnit is agnostic, or doesn't know, where it runs.
>
> You would write the test cases just as any other test.
>
> If I had some more information I could help a little better.
>
> Big Mike
>
> On Sat, Oct 10, 2009 at 1:41 AM, youhaodeyi <youhaodeyi@gmail. com> wrote:
>>
>> I have a server/client application and want to use junit to test it. How
>> do I setup a server side junit test case?
>>
>> thanks.
>>
>>
>>
>> ------------ --------- --------- ------
>>
>> Yahoo! Groups Links
>>
>>
>>
>>
>
>

--
View this message in context: http://old.nabble. com/how-do- I-test-socket- server-applicati on--tp25833711p2 6367358.html
Sent from the JUnit - User mailing list archive at Nabble.com.


 


     

[Non-text portions of this message have been removed]


Re: how do I test socket server application?

by Mike Forsberg :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

The following works for me...

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import org.junit.Before;
import org.junit.Test;

public class TestSomethingVersion1 {

        @Before
        public void before() {
                System.out.println("@Before");
                Thread myThread = new Thread() {
                        public void run() {
                                System.out.println("@Before myThread run()");
                                Socket myServerSocket = null;
                                try {
                                        ServerSocket myServer = new ServerSocket(3000);
                                        System.out
                                                        .println("@Before myThread run() - server socket created.");
                                        myServerSocket = myServer.accept();
                                        System.out
                                                        .println("@Before myThread run() - accepted connection");
                                } catch (IOException e) {
                                        e.printStackTrace();
                                }
                        }
                };
                myThread.start();
        }

        @Test
        public void test1() throws Exception {
                System.out.println("test1");
                synchronized (this) {
                        try {
                                wait(1000);
                        } catch (InterruptedException e) {
                                // do nothing.
                        }
                }
                System.out.println("test1 - after wait");
                new Socket("localhost", 3000);
                synchronized (this) {
                        try {
                                wait(1000);
                        } catch (InterruptedException e) {
                                // do nothing.
                        }
                }
                System.out.println("test1 - after second wait");
        }
}

Some of the things I ran into while writing up this example...
- Threading issues: The thread created in the before did not get any
processor until it was too late.
- Port contentions: I didn't use an unused port.


Some things you might run into later on down the line....
- Use of the Before annotation will lead to a new ServerSocket thread
created for each test method.  Which will lead to inability to use the
same address.  The old thread will still be alive.
- There will be a ton of test information in the ServerSocket thread
required for each test.

_I think you are testing your client incorrectly_

Have you ever thought about testing only one side of the application
at a time?  Are you testing the client or the server?  Have you
thought about separating the connection portion of the application
from the reading and writing of data.  For instance the Socket reads
and writes over streams.  What if these were the arguments to the main
portion of your application?  You could then write and read from these
streams without the socket overhead.

Hope I helped,

Big Mike


On Mon, Nov 16, 2009 at 12:38 AM, youhaodeyi <youhaodeyi@...> wrote:

>
> What my problem is that I create a thread to listen on a port but the program
> will exist not block. Below is my code
>
> @Before
> public void setUp() throws Exception{
> try {
>                        (new Thread(new Runnable(){
>                                public void run() {
>                                        try{
>                                        server=new ServerSocket(3000);
>                                        server.accept();
>                                        }catch(Exception ex){
>                                                ex.printStackTrace();
>                                        }
>                                };
>                        })).start();
>                } catch (Exception e) {
>                        e.printStackTrace();
>                }
>
>        }
> }
> @Test
> public void test1(){
>         ...
> }
>
> Mike Forsberg wrote:
>>
>> JUnit is agnostic, or doesn't know, where it runs.
>>
>> You would write the test cases just as any other test.
>>
>> If I had some more information I could help a little better.
>>
>> Big Mike
>>
>> On Sat, Oct 10, 2009 at 1:41 AM, youhaodeyi <youhaodeyi@...> wrote:
>>>
>>> I have a server/client application and want to use junit to test it. How
>>> do I setup a server side junit test case?
>>>
>>> thanks.
>>>
>>>
>>>
>>> ------------------------------------
>>>
>>> Yahoo! Groups Links
>>>
>>>
>>>
>>>
>>
>>
>
> --
> View this message in context: http://old.nabble.com/how-do-I-test-socket-server-application--tp25833711p26367358.html
> Sent from the JUnit - User mailing list archive at Nabble.com.
>
>
>
> ------------------------------------
>
> Yahoo! Groups Links
>
>
>
>