<commit_before>#
# Copyright 2013, Couchbase, Inc.
# All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License")
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from threading import Thread, Lock
import time


from couchbase.exceptions import CouchbaseError, ObjectThreadError
from couchbase.tests.base import CouchbaseTestCase
from couchbase import LOCKMODE_WAIT, LOCKMODE_EXC, LOCKMODE_NONE

class LockmodeTest(CouchbaseTestCase):
    def test_lockmode_defaults(self):
        # default is LOCKMODE_EXC
        key = self.gen_key("lockmode_defaults")
        cb = self.make_connection()
        self.assertEqual(cb.lockmode, LOCKMODE_EXC)
        cb._thr_lockop(0)
        cb._thr_lockop(1)
        cb.set(key, "value")

        cb = self.make_connection(lockmode=LOCKMODE_NONE)
        self.assertEqual(cb.lockmode, LOCKMODE_NONE)

        self.assertRaises(ObjectThreadError,
                          cb._thr_lockop, 1)
        self.assertRaises(ObjectThreadError,
                          cb._thr_lockop, 0)
        cb.set(key, "value")

        cb = self.make_connection(lockmode=LOCKMODE_WAIT)
        self.assertEqual(cb.lockmode, LOCKMODE_WAIT)
        cb._thr_lockop(0)
        cb._thr_lockop(1)
        cb.set(key, "value")

        cb = self.make_connection(lockmode=LOCKMODE_WAIT, unlock_gil=False)
        self.assertEqual(cb.lockmode, LOCKMODE_NONE)
        cb.set(key, "value")

    def test_lockmode_exc(self):
        key = self.gen_key("lockmode_exc")

        cb = self.make_connection()
        cb._thr_lockop(0)
        self.assertRaises(ObjectThreadError,
                          cb.set,
                          key, "bar")
        cb._thr_lockop(1)

        # Ensure the old value is not buffered
        cb.set(key, "baz")
        self.assertEqual(cb.get(key).value, "baz")

    def test_lockmode_wait(self):
        key = self.gen_key("lockmode_wait")
        cb = self.make_connection(lockmode=LOCKMODE_WAIT, unlock_gil=True)

        d = {
            'ended' : 0
        }

        def runfunc():
            cb.set(key, "value")
            d['ended'] = time.time()

        cb._thr_lockop(0)
        t = Thread(target=runfunc)
        t.start()

        time.sleep(0.5)
        time_unlocked = time.time()
        cb._thr_lockop(1)

        t.join()
        self.assertTrue(d['ended'] > time_unlocked)<commit_msg>lockmode_t: Contention time may be equal<commit_after>#
# Copyright 2013, Couchbase, Inc.
# All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License")
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from threading import Thread, Lock
import time


from couchbase.exceptions import CouchbaseError, ObjectThreadError
from couchbase.tests.base import CouchbaseTestCase
from couchbase import LOCKMODE_WAIT, LOCKMODE_EXC, LOCKMODE_NONE

class LockmodeTest(CouchbaseTestCase):
    def test_lockmode_defaults(self):
        # default is LOCKMODE_EXC
        key = self.gen_key("lockmode_defaults")
        cb = self.make_connection()
        self.assertEqual(cb.lockmode, LOCKMODE_EXC)
        cb._thr_lockop(0)
        cb._thr_lockop(1)
        cb.set(key, "value")

        cb = self.make_connection(lockmode=LOCKMODE_NONE)
        self.assertEqual(cb.lockmode, LOCKMODE_NONE)

        self.assertRaises(ObjectThreadError,
                          cb._thr_lockop, 1)
        self.assertRaises(ObjectThreadError,
                          cb._thr_lockop, 0)
        cb.set(key, "value")

        cb = self.make_connection(lockmode=LOCKMODE_WAIT)
        self.assertEqual(cb.lockmode, LOCKMODE_WAIT)
        cb._thr_lockop(0)
        cb._thr_lockop(1)
        cb.set(key, "value")

        cb = self.make_connection(lockmode=LOCKMODE_WAIT, unlock_gil=False)
        self.assertEqual(cb.lockmode, LOCKMODE_NONE)
        cb.set(key, "value")

    def test_lockmode_exc(self):
        key = self.gen_key("lockmode_exc")

        cb = self.make_connection()
        cb._thr_lockop(0)
        self.assertRaises(ObjectThreadError,
                          cb.set,
                          key, "bar")
        cb._thr_lockop(1)

        # Ensure the old value is not buffered
        cb.set(key, "baz")
        self.assertEqual(cb.get(key).value, "baz")

    def test_lockmode_wait(self):
        key = self.gen_key("lockmode_wait")
        cb = self.make_connection(lockmode=LOCKMODE_WAIT, unlock_gil=True)

        d = {
            'ended' : 0
        }

        def runfunc():
            cb.set(key, "value")
            d['ended'] = time.time()

        cb._thr_lockop(0)
        t = Thread(target=runfunc)
        t.start()

        time.sleep(0.5)
        time_unlocked = time.time()
        cb._thr_lockop(1)

        t.join()
        self.assertTrue(d['ended'] >= time_unlocked)
{'fraction_non_alphanumeric': 0.09599716613531704, 'fraction_numerical': 0.007438894792773645, 'mean_word_length': 2.524968789013733, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3934132', 'n_tokens_mistral': 1779, 'n_tokens_neox': 1727, 'n_words': 408}