Patchwork [6,of,7] test-lock.py: fix testing for forks

login
register
mail settings
Submitter Siddharth Agarwal
Date Sept. 25, 2015, 7 p.m.
Message ID <5049d8ab47a936227186.1443207640@dev6666.prn1.facebook.com>
Download mbox | patch
Permalink /patch/10641/
State Accepted
Headers show

Comments

Siddharth Agarwal - Sept. 25, 2015, 7 p.m.
# HG changeset patch
# User Siddharth Agarwal <sid0@fb.com>
# Date 1443157251 25200
#      Thu Sep 24 22:00:51 2015 -0700
# Node ID 5049d8ab47a936227186493f7de27a2bea35d627
# Parent  bb88105b0fe118eea54e8043b999f9c813895bfa
test-lock.py: fix testing for forks

The earlier test worked only because the held count went up to 2, so the first
release brought it down to 1. Making a copy of the lock fixes that issue.

Patch

diff --git a/tests/test-lock.py b/tests/test-lock.py
--- a/tests/test-lock.py
+++ b/tests/test-lock.py
@@ -1,8 +1,10 @@ 
 from __future__ import absolute_import
 
+import copy
 import os
 import silenttestrunner
 import tempfile
+import types
 import unittest
 
 from mercurial import (
@@ -12,6 +14,12 @@  from mercurial import (
 
 testlockname = 'testlock'
 
+# work around http://bugs.python.org/issue1515
+if types.MethodType not in copy._deepcopy_dispatch:
+    def _deepcopy_method(x, memo):
+        return type(x)(x.im_func, copy.deepcopy(x.im_self, memo), x.im_class)
+    copy._deepcopy_dispatch[types.MethodType] = _deepcopy_method
+
 class lockwrapper(lock.lock):
     def __init__(self, pidoffset, *args, **kwargs):
         # lock.lock.__init__() calls lock(), so the pidoffset assignment needs
@@ -128,16 +136,16 @@  class testlock(unittest.TestCase):
         state = teststate(self, tempfile.mkdtemp(dir=os.getcwd()))
         lock = state.makelock()
         state.assertacquirecalled(True)
-        lock.lock()
+
         # fake a fork
-        lock.pid += 1
-        lock.release()
+        forklock = copy.deepcopy(lock)
+        forklock._pidoffset = 1
+        forklock.release()
         state.assertreleasecalled(False)
         state.assertpostreleasecalled(False)
         state.assertlockexists(True)
 
         # release the actual lock
-        lock.pid -= 1
         lock.release()
         state.assertreleasecalled(True)
         state.assertpostreleasecalled(True)