Unit tests keep failing (C#, Sockets)

kittyfisto87's Avatar


30 Nov, 2015 09:38 PM

I have the problem that several of my unit tests keep failing on AppVeyor that run perfectly on my local machine and I seriously don't have a clue what could be the problem.

I'm writing an RPC middleware and some of my unit tests keep failing on AppVeyor only. These tests all fail the handshake that's happening after the tcp-connection has been established, but this handshake is working flawlessly on dozens of computers that I've run them on.

The following bare-bone example that runs on my computer, but fails a 100% of the time on my AppVeyor build is the following:

for (int i = 0; i < 1000; ++i)
                const AddressFamily family = AddressFamily.InterNetwork;
                const SocketType socket = SocketType.Stream;
                const ProtocolType protocol = ProtocolType.Tcp;
                using (var client = new Socket(family, socket, protocol))
                using (var server = new Socket(family, socket, protocol))
                    client.ReceiveTimeout = 10000;

                    server.ExclusiveAddressUse = true;
                    server.Bind(new IPEndPoint(IPAddress.Loopback, 60310));

                    bool isConnected = false;
                    server.BeginAccept(ar =>
                            Console.WriteLine("BeginAccept handler");
                            var serverCon = server.EndAccept(ar);
                            Console.WriteLine("EndAccept called");

                            isConnected = true;
                            serverCon.Send(new byte[256]);
                        }, null);

                        Console.WriteLine("socket.Connected: {0} to {1}", client.Connected, client.RemoteEndPoint);
                    catch (Exception e)
                        throw new Exception(string.Format("Connect failed: {0}", e.Message), e);

                    client.Send(new byte[256]);

                        int length = client.Receive(new byte[256]);
                    catch (Exception e)
                        throw new Exception(string.Format("Receive #{0} failed (Connected: {1}): {2}",

From the console output I can gahter that client.Connect() doesn't throw, but the BeginAccept() callback isn't invoked either (within 10 seconds).

The really puzzling thing is that NOT all unit tests involving communication over a socket fail, but only some - however those that do fail every time.

I'm unable to recognize the issue here and I hope you gals/guys can spot the problem in my source code, as I'm too blind to find it.

  1. Support Staff 1 Posted by Feodor Fitsner on 30 Nov, 2015 09:49 PM

    Feodor Fitsner's Avatar

    Is it a public or private project?

  2. 2 Posted by kittyfisto87 on 30 Nov, 2015 09:59 PM

    kittyfisto87's Avatar

    It's a public project, you can find one failing build (TestConnect28 is the test in question) here - I've tagged and ignored all tests that won't run on AppVeyor; but I'd like to reactive them asap.

    The source in my original post should be a verbatim copy of the one being on AppVeyor and can be found on github (ConnectTest.TestConnect28).

  3. Support Staff 3 Posted by Feodor Fitsner on 30 Nov, 2015 10:07 PM

    Feodor Fitsner's Avatar

    OK, I've just made a console app from the code above and ran it on OSS build environment - looks like it worked: https://ci.appveyor.com/project/FeodorFitsner/simple-console/build/...

  4. Support Staff 4 Posted by Feodor Fitsner on 30 Nov, 2015 10:18 PM

    Feodor Fitsner's Avatar
  5. 5 Posted by kittyfisto87 on 30 Nov, 2015 10:27 PM

    kittyfisto87's Avatar

    I guess it's a good sign that the code in question isn't completely wrong :)

    I have to wonder though why I'm doing to the environment that causes this test (and similar) to fail in my builds. It seems the problem's not that straight forward as I expected.


    I completely forgot to thank you for your effort, so... thanks ;)

  6. Support Staff 6 Posted by Feodor Fitsner on 30 Nov, 2015 10:44 PM

    Feodor Fitsner's Avatar

    Try isolating it as much as possible. I'd leave only that single test that fails and made sure it runs first. Then enabled more and more tests to see which one interfere.

  7. 7 Posted by kittyfisto87 on 30 Nov, 2015 11:01 PM

    kittyfisto87's Avatar

    I'll try that and post the results tomorrow.

  8. Ilya Finkelshteyn closed this discussion on 25 Aug, 2018 02:02 AM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts


? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac