Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3614096
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:12:21+00:00 2026-05-18T22:12:21+00:00

Greetings Stackoverflowers, I have an application in which a communication abstraction layer exists. Each

  • 0

Greetings Stackoverflowers,

I have an application in which a communication abstraction layer exists. Each implementation in this layer, called a connector, provides my application with a way of exchanging data with a connected peer (e.g.: through HTTP, TCP sockets, UDP sockets, etc.).

Per example, I have a Connector_Tcp class that implements methods such as read, write, open and close.

I need to write a unit test for that class. I know that unit tests should have as less dependencies as possible. Unfortunately, in that case, the dependency is a system resource: a socket; and I cannot bypass it.

I need some advice on how to go about unit testing this class.

In my opinion, even though this class is using a system resource, it should be tested as all the other connectors to make sure that it respects the standard established by my application.

I am concerned about such things as binding collisions (Address already in use errors) and blocking. I don’t want the unit test to fail because the port is already in use by a system service that has nothing to do with my application.

I have done a lot of unit tests in my days, but none of which relies on such a low-level resource that are sockets.

How would you go about unit testing a socket-dependent class? Open a socket for every unit? Use a single server class, then a manually-defined socket resource to connect to it and test it…?

I guess my problem really is the following:

If the unit test fails… how do I know if:

  • The socket is already in use by another process;
  • The unit test is badly written; or
  • The method behaves improperly (which is what a unit test is worth).

I need the unit test to test only if the method has been behaving properly or not…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T22:12:22+00:00Added an answer on May 18, 2026 at 10:12 pm

    I’ve written a similar abstraction layer, albeit in C/C++, and I have had occasion to unit-test the socket based code. I don’t know if there’s anything inherently specific to PHP here, so I’ll just offer what generic advice I can.

    • Create a server socket in your setup, close it in the tear down. Depending on whether you support many clients connecting to the server socket, you may want to do that part in the test method.
    • Assuming you can specify the port to connect to in your client code, have the server socket bind to port 0 (ephemeral port), and then use this in the client code. This will eliminate the problem of binding to a port already in use.
    • Set timeouts on your sockets. If your test fails for some unexpected reason, you don’t want it hanging waiting for a connection forever.
    • You need to be able to interleave the client/server behaviour. For example, you’ll want to test what happens if the server shuts down its side of the connection half way through a client read/write. Either asynchronous I/O or multi-threading will do the job. I’m not sure what you have at your disposal in PHP.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Greetings, I have an asp.net mvc application. I have some links that corresponds to
Greetings I may have imagined this but does anyone know if Last.fm previously used
Greetings, I have a view based application project where I have created an NSObject
Greetings , I am a new developer in BlackBerry Application. I have been working
Greetings, I have a particular object which can be constructed from a file, as
Greetings I have an ajax call which: Gets an xml file of posts Populates
Greetings! I have some XML like this: <Root> <MainSection> <SomeNode>Some Node Value</SomeNode> <SomeOtherNode>Some Other
Greetings, I have a complicated scenario to handle. I have a wsdl file which
Greetings StackOverflow. I have a scenario, in which I have a UITableView with a
Greetings! I have a Repeater control that's using an XmlDataSource control. <asp:FormView id=myFormView runat=server

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.