How to simulate network failure for test purposes (in C#)?

前端 未结 11 779
被撕碎了的回忆
被撕碎了的回忆 2020-12-29 04:25

I\'m building what could be called the DAL for a new app. Unfortunately, network connectivity to the database is a real problem.

I\'d like to be able to temporarily

相关标签:
11条回答
  • 2020-12-29 05:24

    For the time being, I'm just "disabling" the network by setting a bogus static IP as follows:

    using System.Management;
    
    class NetworkController
    {
    
        public static void Disable()
        {
            SetIP("192.168.0.4", "255.255.255.0");
        }
    
        public static void Enable()
        {
            SetDHCP();
        }
    
    
        private static void SetIP(string ip_address, string subnet_mask)
        {
            ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection objMOC = objMC.GetInstances();
    
            foreach (ManagementObject objMO in objMOC) {
                if ((bool)objMO("IPEnabled")) {
                    try {
                        ManagementBaseObject setIP = default(ManagementBaseObject);
                        ManagementBaseObject newIP = objMO.GetMethodParameters("EnableStatic");
    
                        newIP("IPAddress") = new string[] { ip_address };
                        newIP("SubnetMask") = new string[] { subnet_mask };
    
                        setIP = objMO.InvokeMethod("EnableStatic", newIP, null);
                    }
                    catch (Exception generatedExceptionName) {
                        throw;
                    }
                }
    
    
            }
        }
    
        private static void SetDHCP()
        {
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = mc.GetInstances();
    
            foreach (ManagementObject mo in moc) {
                // Make sure this is a IP enabled device. Not something like memory card or VM Ware
                if ((bool)mo("IPEnabled")) {
                    ManagementBaseObject newDNS = mo.GetMethodParameters("SetDNSServerSearchOrder");
                    newDNS("DNSServerSearchOrder") = null;
                    ManagementBaseObject enableDHCP = mo.InvokeMethod("EnableDHCP", null, null);
                    ManagementBaseObject setDNS = mo.InvokeMethod("SetDNSServerSearchOrder", newDNS, null);
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-29 05:29

    Depends on what particular network problem you wish to simulate. For most folks, it's as simple as "server unreachable", in which case you'd just try to connect to a non existent server. Be careful, though, because you want something that is routable but does not answer. Trying to connect to dkjdsjk.com will fail immediately (DNS lookup), but trying to connect to www.google.com:1433 will (probably) time out due to a firewall - which is how your app will behave when your DB server is down.

    0 讨论(0)
  • 2020-12-29 05:30

    Try blocking the connection with a firewall midway through the session maybe?

    I like the wrapper idea as well, but thats kind of abstracting the problem and you prolly might not get exact real world behavior. Also, inserting the wrapper layer and then removing it may be more trouble than its worth.

    Edit: Run a script that turns the Network adapter on/off randomly or at set intervals?

    0 讨论(0)
  • 2020-12-29 05:30

    Try Toxiproxi, it can simulate network outage.

    They have REST API and even .NET Client API to change the network simulation programatically (from your test code) https://github.com/shopify/toxiproxy

    0 讨论(0)
  • 2020-12-29 05:30

    Use mock objects to create configurable, destructible versions of the real thing--in this case, the database.

    0 讨论(0)
提交回复
热议问题