Add tests for the decorators/helpers
This commit is contained in:
63
fasteners/tests/test_decorators.py
Normal file
63
fasteners/tests/test_decorators.py
Normal file
@@ -0,0 +1,63 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright (C) 2015 Yahoo! 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.
|
||||
|
||||
import threading
|
||||
|
||||
import fasteners
|
||||
from fasteners import test
|
||||
|
||||
|
||||
class Locked(object):
|
||||
def __init__(self):
|
||||
self._lock = threading.Lock()
|
||||
|
||||
@fasteners.locked
|
||||
def i_am_locked(self, cb):
|
||||
cb(self._lock.locked())
|
||||
|
||||
def i_am_not_locked(self, cb):
|
||||
cb(self._lock.locked())
|
||||
|
||||
|
||||
class RWLocked(object):
|
||||
def __init__(self):
|
||||
self._lock = fasteners.ReaderWriterLock()
|
||||
|
||||
@fasteners.read_locked
|
||||
def i_am_read_locked(self, cb):
|
||||
cb(self._lock.owner)
|
||||
|
||||
@fasteners.write_locked
|
||||
def i_am_write_locked(self, cb):
|
||||
cb(self._lock.owner)
|
||||
|
||||
def i_am_not_locked(self, cb):
|
||||
cb(self._lock.owner)
|
||||
|
||||
|
||||
class DecoratorsTest(test.TestCase):
|
||||
def test_locked(self):
|
||||
obj = Locked()
|
||||
obj.i_am_locked(lambda is_locked: self.assertTrue(is_locked))
|
||||
obj.i_am_not_locked(lambda is_locked: self.assertFalse(is_locked))
|
||||
|
||||
def test_read_write_locked(self):
|
||||
reader = fasteners.ReaderWriterLock.READER
|
||||
writer = fasteners.ReaderWriterLock.WRITER
|
||||
obj = RWLocked()
|
||||
obj.i_am_write_locked(lambda owner: self.assertEqual(owner, writer))
|
||||
obj.i_am_read_locked(lambda owner: self.assertEqual(owner, reader))
|
||||
obj.i_am_not_locked(lambda owner: self.assertIsNone(owner))
|
||||
29
fasteners/tests/test_helpers.py
Normal file
29
fasteners/tests/test_helpers.py
Normal file
@@ -0,0 +1,29 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright (C) 2015 Yahoo! 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.
|
||||
|
||||
import threading
|
||||
|
||||
import fasteners
|
||||
from fasteners import test
|
||||
|
||||
|
||||
class HelpersTest(test.TestCase):
|
||||
def test_try_lock(self):
|
||||
lock = threading.Lock()
|
||||
with fasteners.try_lock(lock) as locked:
|
||||
self.assertTrue(locked)
|
||||
with fasteners.try_lock(lock) as locked:
|
||||
self.assertFalse(locked)
|
||||
Reference in New Issue
Block a user