From 68b14b13446265e0106f77fcb1cbc4f3e531e189 Mon Sep 17 00:00:00 2001 From: Joshua Harlow Date: Wed, 3 Jun 2015 14:34:53 -0700 Subject: [PATCH] Add tests for the decorators/helpers --- fasteners/tests/test_decorators.py | 63 ++++++++++++++++++++++++++++++ fasteners/tests/test_helpers.py | 29 ++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 fasteners/tests/test_decorators.py create mode 100644 fasteners/tests/test_helpers.py diff --git a/fasteners/tests/test_decorators.py b/fasteners/tests/test_decorators.py new file mode 100644 index 0000000..d184516 --- /dev/null +++ b/fasteners/tests/test_decorators.py @@ -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)) diff --git a/fasteners/tests/test_helpers.py b/fasteners/tests/test_helpers.py new file mode 100644 index 0000000..2f48074 --- /dev/null +++ b/fasteners/tests/test_helpers.py @@ -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)