Simulating network connection failure via C#

  • Thread starter Thread starter Phil Haack
  • Start date Start date
P

Phil Haack

Hello all,

Does anyone have a creative quick way to simulate the loss of network
connectivity via C#? I am trying to write a Unit Test of a long
running network operation (initiated by a 3rd party component) and
would like to ensure that the component and my containing code handles
the loss of network connectivity appropriately. Thanks!
 
public class WireCutter
{
Wire wire;
public WireCutter(Wire wire)
{
this.wire = wire;
}

public NetworkCrash CutWire(wire)
{
cut wire;
}
}

Dale
 
(e-mail address removed) (Phil Haack) wrote in
Does anyone have a creative quick way to simulate the loss of network
connectivity via C#? I am trying to write a Unit Test of a long
running network operation (initiated by a 3rd party component) and
would like to ensure that the component and my containing code handles
the loss of network connectivity appropriately. Thanks!

Pull the network cable.


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"


ELKNews - Get your free copy at http://www.atozedsoftware.com
 
ha ha! Thanks for the suggestions. Maybe I didn't make myself clear.
I am attempting to write this as an AUTOMATED unit test that can run
nightly on our build server. I'd hate to have to come in every night
to pull the network plug as part of our scheduled process ;).
 
(e-mail address removed) (Phil Haack) wrote in
ha ha! Thanks for the suggestions. Maybe I didn't make myself clear.
I am attempting to write this as an AUTOMATED unit test that can run

You can get Lego mindstorms and hook it to your serial port to disconnect it
each night. ;)
nightly on our build server. I'd hate to have to come in every night
to pull the network plug as part of our scheduled process ;).

You can run through a mapped port, and simply introduce the errors there by
forcibly closing the connection.

Indy has mapped port components you could implement it with if you dont want
to build them from scratch. Its also free.
http://www.indyproject.org/indy.html



--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"


ELKNews - Get your free copy at http://www.atozedsoftware.com
 
you've got some interesting responses there Phil.

I would insert a testing proxy component in the unit test module (Nunit or
CSunit?).

In other words, you have an interface for this long-running network
component. While standing in front of the machine, actually pull the
network cable to see what error is generated, and it what part of the
interface. Then, hide that part of the interface under a proxy method that
simply passes through or does nothing (in production). For the unit test,
pass in an object that will allow that method to run for a predetermined
clock time and then return the error you detected. See if your application
handles it well.

Testing the server is a bit harder, because it won't know that the client is
performing a unit test. All it will know is that your client has stopped
communicating. However, that is typical of network outage errors, so you
should get the behavior you expect. Caveat: unit test scenarios are often
not good for this kind of testing, because the very next test may need to
re-establish the connection, which may affect how the server behaves.

Good Luck,
--- Nick
 
Something that just occurred to me that WMI might be the answer.
Win32_NetworkAdapterConfiguration has the ability to set the static IP
address to something outside your network address range, thereby effectively
breaking the network.

There does not seem to be any classes that would allow you to specifically
disable the network adapter.

Search Win32_NetworkAdapterConfiguration on MSDN and you should be on your
way. You might want to hit the WMI newsgroup as well.

Dale
 
Hi Chad !

I've been using Indy for Delphi for years ! Oh boy - I didn't know that you made an C# version. I'm
really happe for that since I've just started with C# (Visual Studio).
Indy has mapped port components you could implement it with if you dont want
to build them from scratch. Its also free.
http://www.indyproject.org/indy.html

Best wishes
Kai Bohli
(e-mail address removed)
Norway
 
Back
Top