Solved

Unit test server/client program

Posted on 2007-03-23
10
353 Views
Last Modified: 2010-04-15
My project is of client/server type. The client side is using Java and the server side is using C. It uses socket to send data via packets to the clients. Clients communicating to server with the same way: using socket.

We are told to do "unit test" on each sides. For my server side, I have to test my C code. The problem is that if I don't modify the code, it will try to connect to the client therefore it is no longer a "unit test" but some kind of intergration test.

1. Hard code the "send" and "receive" packet part of the code, use printf or use debugging mode ( gdb ) to confirm "send" and "receive" data step by step.

This is very time consuming, we have to do the same thing with all the codes, and we have to remove test code after unit test is finished.

I think there must be more effective ways to do "unit test" with my case, and "unit test" in general client/server model.

Please share your opinions how to test such kind of code ( unit test ).

TIA
I am thinking of a new way to test my C code, without connecting to the client side ( so it will be independent to the Java programs ). So far, only one idea pops up in my mind:
0
Comment
Question by:ksv9x
  • 3
  • 2
  • 2
  • +1
10 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 18778873
Properly testing always takes time - there's no quick-and-easy solution.

To do unit testing, you indeed have to test both applications separately. Take the server side, and send it a known good message, and see what happens (the "see" part can be achieved by adding printf() statements in the code, by using a debugger, etc.).
Once the server is tested, and works correctly, you can do the same thing with the client.

I'm not sure I understand what the problem is you have with this. Can you tell me what exactly you don't understand and/or don't like ?
0
 
LVL 45

Expert Comment

by:Kdo
ID: 18779273

Hi ksv9x,

Whenever possible, I always test client/server applications by running my own version of the server on my desktop machine.  If the server is host specific (like AIX, HP-UX, TRU64, etc.) there are other, often insurmountable, challenges.  But even then, it's often possible to "borrow" a test system to run the server.

If it runs on a linux server, Windows Server, etc.  Then it's usually pretty easy to run a copy on my local machine.  The client program will be configured to connect to the desired port on the LOCALHOST (127.0.0.1).


Can you run the server program on your desktop?  What about an R&D or Q/A machine?
Kent
0
 
LVL 15

Expert Comment

by:efn
ID: 18787462
The answer depends on what you (or whoever is requiring unit testing) consider the unit(s).

If the unit is the whole server program and you want to test the whole program without the real client program, then my colleagues' advice applies.  You can write a test client program and peek into the server state to see if it is doing the right thing.  Better yet, have your test program collect the server's outputs and check whether they are correct.

If the unit is something smaller, you would approach it differently.  For example, you might have some application module that calls other functions to do input and output.  In production, it calls functions that read from and send to a client over a network, but to test it, you could link it with test modules that supply the input from hard-coded sources rather than a network, and check what is passed to the output functions to see if it is correct.  You should never need to add or remove code.  You should be able to do this just by linking with different selections of modules.  In order for this to work, though, your program has to be designed with multiple separate modules with different functions, not just one big module that does everything.
0
 

Author Comment

by:ksv9x
ID: 18813640
Infinity08,

Currently I have to do the Unit Test for the server side. So far, when I am still not familiar with the system, what I did is not muture, just use printf() and observe the result printed at the console. The "messages" are string, sent by sockets.

My question is like: Can I test the server side without client one? I think I can, at some level, but having a trivial client that works and sending correct requests to server is one point, so we can use it to test the my server program.

------------------
quote from Infinity08,

Properly testing always takes time - there's no quick-and-easy solution.

To do unit testing, you indeed have to test both applications separately. Take the server side, and send it a known good message, and see what happens (the "see" part can be achieved by adding printf() statements in the code, by using a debugger, etc.).
Once the server is tested, and works correctly, you can do the same thing with the client.

I'm not sure I understand what the problem is you have with this. Can you tell me what exactly you don't understand and/or don't like ?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:ksv9x
ID: 18813657
Kdo,

The ( OS ) server is running Linux, I mean the host OS is Linux. The "server" I am using here is a special DB server ( Yes, special, developed internal used internal ). What I am am making and writing are the "modules" for the DB server. The server can't run on Windows.

So If the client - "client00" sends a "message" to the DB server in the form of a HTTP request, the DB server will call the module, says "module00", excute it and return the result to client00.

I don't think I can isolate module00 and test it without using the "client00". But one thing is that: If we use client00 ( even a trivial verison of it ) to test module00, then the test case is no longer UT, but an intergration test.

Is that right?

We had some bad exprience with the testing module: When module00 failed, we spent hours on module00 but actually client00 had bug.

I  hope you can understand my problem ( sorry for my bad English and poor understanding of my system :D )
 
-----------------
quote from dko:
Hi ksv9x,

Whenever possible, I always test client/server applications by running my own version of the server on my desktop machine.  If the server is host specific (like AIX, HP-UX, TRU64, etc.) there are other, often insurmountable, challenges.  But even then, it's often possible to "borrow" a test system to run the server.

If it runs on a linux server, Windows Server, etc.  Then it's usually pretty easy to run a copy on my local machine.  The client program will be configured to connect to the desired port on the LOCALHOST (127.0.0.1).


Can you run the server program on your desktop?  What about an R&D or Q/A machine?
Kent
0
 

Author Comment

by:ksv9x
ID: 18813723
efn,

The unit is a module of the DB system ( see my previous posts for explanation ), not the whole DB server.

You have recommended a test program, hard-code it and let it send test data to the DB server. The pro of the method is that: We don't have to modify the code of the module in order to test it. But the problem is that: The test progam may have bugs! Actually, it did and we had quite amount of time with it to judge which on ( the server side and the client side ) has bugs.

Your answer is very close to me, I accept it but please reply if you have time...

Thank you



----------------------

quote from efn:

The answer depends on what you (or whoever is requiring unit testing) consider the unit(s).

If the unit is the whole server program and you want to test the whole program without the real client program, then my colleagues' advice applies.  You can write a test client program and peek into the server state to see if it is doing the right thing.  Better yet, have your test program collect the server's outputs and check whether they are correct.

If the unit is something smaller, you would approach it differently.  For example, you might have some application module that calls other functions to do input and output.  In production, it calls functions that read from and send to a client over a network, but to test it, you could link it with test modules that supply the input from hard-coded sources rather than a network, and check what is passed to the output functions to see if it is correct.  You should never need to add or remove code.  You should be able to do this just by linking with different selections of modules.  In order for this to work, though, your program has to be designed with multiple separate modules with different functions, not just one big module that does everything.
0
 
LVL 15

Expert Comment

by:efn
ID: 18813866
No matter what, your test software can be defective, and debugging the larger system of test software combined with software under test is work.  It can help if the test software can show you what it is sending and receiving, so you can check its reports about whether the software under test is defective.

What about my second suggestion?  Can you detach the module you want to test from the rest of the DB system and run it in isolation somehow?
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 50 total points
ID: 18814346
Since the interface between the server and the client is HTTP, you don't have to write a complicated test tool. You can just use telnet for example to send a known good request, and receive the response.
If you want some more diagnostics, you can write a simple test client that opens a connection, sends the request, receives the result, and parses it, looking for problems.

Sure, you can introduce bugs in the test client - that's why you should try to keep it as simple as possible - to minimize the chance for bugs.

HTTP is a very simple and well-known protocol, and there's several tools around that send HTTP requests.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now