1

I am new in Django and trying to write some test cases.

In my code I am doing some transaction. For that purpose I locked my that code using select_for_update in django. Now I want to test it whether lock is working correctly or not. I am running 2 or more processes simultaneously so that it allows only first process and wait here for finish first process and then other process proceeds.

#here XYZ and ABC are models.

@transaction.commit_on_success
def transaction_func():
    exp1 = ABC(a = 5)
    exp1.save()
    process_list =[]
    for i in xrange(2):
        p = Process(target=row_lock_method, args=('some_string',))
        p.start()
        time.sleep(3)
        process_list.append(p)

    for each in process_list:
        each.join()

    raise

def row_lock_method(code):
    exp2 = XYZ(b = code)
    exp2.save() 
    client = Client()
    client.login(username='gaurav@example.com', password='sample123')
    response = client.post('some_url',{'exp2':exp2},follow=True)

Here the above code is in other view file. So I used here cron job to run this file in django. I am calling row_lock_method twice in transaction_func using process. As test is run successfully but this is in real database so i want to rollback all changes done in this job so i put raise condition after both for loops.So that exception occurs here and it will rollback it through transaction.commit_on_success. but my problem is that the rollback is not working here. Even no error message is coming.

Is i am doing something wrong. Please do reply. Thanks in Advance.

Community
  • 1
  • 1
Gaurav Nagpal
  • 155
  • 2
  • 15
  • 1
    What are you trying to test? Are you concerned that the django/innodb isn't thread safe or your code (or some other reason involving multiprocessing) – Foon May 08 '14 at 12:17
  • 1
    You are going to need to be a bit clearer. How and why are you using multiprocessing here? Maybe you should show some code. – Daniel Roseman May 08 '14 at 12:22
  • @DanielRoseman: See EDITED part – Gaurav Nagpal May 08 '14 at 13:45
  • @DanielRoseman I Updated question and description. Please look into this. – Gaurav Nagpal May 09 '14 at 07:50
  • @GauravNagpal This won't work, because each new process will create a new connection to DB (it seems that Django can't share connections between processes?) and all changes will happen outside of the original transaction (obviously, because they happen over the other connection). Is there a reason why you use multiprocessing instead of threading? – freakish May 09 '14 at 08:11
  • @freakish I used previously threading only. but By using that , where i am using select_for_update , thread hold and lock that row infinite time. and it throwing me error "Internal Server Error: and OperationalError: (1205, 'Lock wait timeout exceeded; try restarting transaction')". After reading some tutorials i used processing rather than threading. What i have to do now? – Gaurav Nagpal May 09 '14 at 10:10

0 Answers0