Can I create a Deadlock Database test in Nunit?

In this asp.net, I clear it so that deadlocks occur. I want to make sure the code works properly with them, so I'm trying to write NUnit tests that cause a dead end .....

DAO is shared by entity. Each object has a set of tests, which are surrounded by the Startup () and Teardown () methods, which create a transaction and then roll it back after the tests are completed. This works great for everything else, but is completely useless for dead ends.

How to configure and run a dead end test using TransactionScope and SQL2000 (for example, MSDTC) that can be reliably reproduced? In more detail: I know that there is a situation where two users call two functions with different specific data values, then a deadlock may occur. How can I mimic this in NUNIT - and make deadlock always a case?

And yes, I started with the action plan "Why don’t you stop the deadlocks that occur in the first place," but I do not control the code where deadlocks can occur - I just call the functions and they can slow down.

+4
source share
6 answers

If your deadlock causes an exception to be thrown, you want to use the Mock Object to emulate the thrown exception.

The basic idea is that you talk about your Mock Object structure (I like TypeMock ) to throw an exception, something like this

MockObject mo = MockManager.MockObject(typeof(MyDeadlockException)); mock.ExpectAndThrow("MyMethod", (MyDeadlockException)mo.Object); 

The idea is basically the same for other mocking frameworks.

+2
source

Most of these solutions are multi-thread related. Here is one that does not.

Close These Loopholes - Reproduce Database Errors

The author - Alexey Kuznetsov.

+1
source

What if one of your tests in the middle of your transaction just β€œwaits” for about 5 minutes? Or you just write a test that starts a transaction, creates a new record and then updates that record without committing. Then start a new transaction and try to read this record, which has been created and is currently being updated. There you will find a dead end.

0
source

What to do if you manually locked the table and ALWAYS left it locked? Then any action taken against this table would create a deadlock?

0
source

On this blind one, but is it possible in your TestSetup method to actually create an SQLConnection for your database? Then, using this, can you simply specify either a command to lock the table, or take some action to lock the record or page? So, will it be outside of any other transactions that you have? It looks like it will be an option that you have already considered. What am I missing in your situation?

0
source

For unit testing, you probably want to avoid actually using the database. How do you know that you have a dead end. You should check for a condition that tells you that there is a dead end and the creation of this in your test.

A layout is an ideal that mimics if you call a service and it returns an error. Just try to return the error you expect. If you expect a timeout or something, then the same applies.

In the general case, unit test should work only on the code being tested and not rely on any other code or components. However, databases are essentially another component, and you are probably doing some kind of functional tests using nunit to manage them.

In this case, you really need to create a deadlock, but lock the record or table, and then call the component that is trying to use the same record and process the response.

0
source

Source: https://habr.com/ru/post/1277532/


All Articles