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

前端 未结 11 777
被撕碎了的回忆
被撕碎了的回忆 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:04

    If you are trying a complete network outage for your application unplugging the network cable will work. Sometimes you might have a data access layer with multiple data sources (on different machines) in which case you can simulate an exception in your tests with a Mock Framework like Rhino Mocks. Here is some pseudo-code that you may have in your test

    void TestUserDBFailure()
    {
        // ***** THIS IS PSEUDO-CODE *******
        //setting up the stage - retrieval of the user info create an exception
        Expect.Call(_userRepository.GetUser(null))
           .IgnoreArguments()
           .Return(new Exception());
    
        // Call that uses the getuser function, see how it reacts
        User selectedUser = _dataLoader.GetUserData("testuser", "password");        
    }
    
    0 讨论(0)
  • 2020-12-29 05:04

    There is a tool you can use for simulating High Latency and Low Bandwidth in Testing of Database Applications as explained in this blog entry.

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

    Just found an alternative that allows to directly close TCP connections:

    http://lamahashim.blogspot.ch/2010/03/disabling-network-using-c.html

    It is based on Windows IP Helper API (uses DllImport): http://msdn.microsoft.com/en-us/library/windows/desktop/aa366073(v=vs.85).aspx

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

    Look for a WAN simulator that will allow you to restrict bandwidth (and cut it off completely) I always find it interesting to see how the user experience changes when my apps are run in a bandwidth restricted environment. Look here for some information.

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

    Write a wrapper to the network class connectivity class you're using (e.g. WebClient) with an on-off switch :)

    Either that, or block your application in the firewall.

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

    Probably not helpful for simulating "real" network issues, but you could just point your DB connection string to a non-existent machine while within the scope of your test.

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