I am trying to write some unit test cases for a client.
Since we do not expect a server to be running during unit testing, we are trying to mock some behaviors.
The existing code has some tight couplings, so we are unable to mock all cases.
I have created a socket for the client and need to connect the socket file descriptor to an address.
Usually this happens when an active server is running and then I call the connect() function.
Since we cannot have a server running, is it possible to connect the socket file descriptor to some predefined address?
As mentioned, this is just for unit testing purpose.
I have little knowledge on mocks and unit tests so kindly excuse.
Thanks in advance.
Related
I have unidirectional (both client- and server-) streaming gRPC methods implemented in my golang server. I came across bufconn which enables me to creates my server via in-memory connections in my tests. However, I have logic in my method where I check the peer's address, it always returns "bufconn". I do rely on the peer to provide the IP address along with its port, but I can't really achieve that using bufconn.
What is a canonical (or preferred) way to test gRPC streaming calls in golang?
Is it considered an anti-pattern to rely on peer information in the context?
Context: In a server-client setup (using ace-tao).
Problem Statement: The server might be down, while the client is up and attempting to make API calls. Now to make the client setup more robust, I want to make the client to be able to know about the server-down-state, and when server is up again, it can attempt the rebind and get the new ORB ready, for any further API calls.
Any suggestions?
There is only one solution in case of TCP/IP. You must implement so-called heatbeat connection : simple echo connection and analyze the return code of read-write calls.
There is no callback in TCP/IP for connection state : alive or dead.
I'm currently considering to use gRPC for basically inter-process communication between Java app (client) and C++ server. The RPC calls will use functionality from very old C++ code base which is definitely not thread-safe.
Normally the Java client will start more gRPC server instances and have just one connection with each server instance.
Is there any way how to ensure this on the gRPC server to accept just one connection and refuse all other attempts for connection. Otherwise I need to introduce some global lock in the RPC functions to have 100% correct server implementation.
There are plans to provide additional server side APIs that will allow the server to decide whether or not to accept an incoming connection, but this is not done yet. For now, a lock is probably a reasonable option.
I am writing a client and server library for local IPC. Client and Server both has classes which make use of named pipes to send data between two processes. I want to write functional test to test client-server libraries.
My idea is to create client in functionaltest, mock a server in a separate executable, launch server using CreateProcess and send data to server. But in such case I won't have any control on mock server and checking data sent by client cannot be validated on server.
Can anyone suggest me how to write client server functional test so I can validate functionality of both modules.
Here are couple of tests I'm thinking of,
1. Client connects to server.
2. Client disconnects gracefully from server.
3. Client sends some data to server.
4. Server disconnects client connection selectively.
5. Server shutdown/client shutdown
6. etc.
Thanks,
Ajay
For testing, run the client and server code in the same process using separate threads.
As the headline says, how would you test a client/server application, that is written in C/C++, that talks through a protocol over a network? Im a bit confused on how to do this. I have thought about making some mocking, but I have never tried mocking, so I dont know if this is the best way.
How should I do this? I have written many unit tests, but never tried to test something that interact over a network.
I use the command pattern in the unit test driver (client) to send test commands to the server. The advantage of this is that the test is coded in one place.
Example for testing a request timeout:
Client sends a sleep command to server and then the request. The request times out and the test case is passed.
Typically you'll want to use mocking to verify that each side reacts as required to messages from the other side (e.g., that when the client receives a response from the server that it processes that response correctly).
To test the network functionality itself, you can test both running on the same machine, and you can run one (or both) inside a virtual machine. If you have two network adapters, you can even dedicate each to a virtual machine so the network traffic actually goes out one, to a switch/router, and comes back in the other (particularly useful when/if you want to capture and verify packets).
I have some client/server code that I unit test through the loopback address. I use some mocks when I have to test error conditions. So, I test with the real code when I can and the mocks when I need to trigger very specific conditions.