"Fossies" - the Fresh Open Source Software Archive

Member "buildbot-2.5.1/buildbot/test/unit/test_util_notifier.py" (24 Nov 2019, 3576 Bytes) of package /linux/misc/buildbot-2.5.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 # Copyright Buildbot Team Members
    2 #
    3 # Permission is hereby granted, free of charge, to any person obtaining
    4 # a copy of this software and associated documentation files (the
    5 # "Software"), to deal in the Software without restriction, including
    6 # without limitation the rights to use, copy, modify, merge, publish,
    7 # distribute, sublicense, and/or sell copies of the Software, and to
    8 # permit persons to whom the Software is furnished to do so, subject to
    9 # the following conditions:
   10 #
   11 # The above copyright notice and this permission notice shall be
   12 # included in all copies or substantial portions of the Software.
   13 #
   14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
   15 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
   16 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
   17 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
   18 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
   19 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
   20 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
   21 
   22 from twisted.python.failure import Failure
   23 from twisted.trial.unittest import SynchronousTestCase
   24 
   25 from buildbot.util import Notifier
   26 
   27 
   28 class TestException(Exception):
   29 
   30     """
   31     An exception thrown in tests.
   32     """
   33 
   34 
   35 class Tests(SynchronousTestCase):
   36 
   37     def test_wait(self):
   38         """
   39         Calling `Notifier.wait` returns a deferred that hasn't fired.
   40         """
   41         n = Notifier()
   42         self.assertNoResult(n.wait())
   43 
   44     def test_notify_no_waiters(self):
   45         """
   46         Calling `Notifier.notify` when there are no waiters does not
   47         raise.
   48         """
   49         n = Notifier()
   50         n.notify(object())
   51         # Does not raise.
   52 
   53     def test_notify_multiple_waiters(self):
   54         """
   55         If there all multiple waiters, `Notifier.notify` fires all
   56         the deferreds with the same value.
   57         """
   58         value = object()
   59         n = Notifier()
   60         ds = [n.wait(), n.wait()]
   61         n.notify(value)
   62         self.assertEqual(
   63             [self.successResultOf(d) for d in ds],
   64             [value] * 2,
   65         )
   66 
   67     def test_new_waiters_not_notified(self):
   68         """
   69         If a new waiter is added while notifying, it won't be
   70         notified until the next notification.
   71         """
   72         value = object()
   73         n = Notifier()
   74         box = []
   75 
   76         def add_new_waiter(_):
   77             box.append(n.wait())
   78         n.wait().addCallback(add_new_waiter)
   79         n.notify(object())
   80         self.assertNoResult(box[0])
   81         n.notify(value)
   82         self.assertEqual(
   83             self.successResultOf(box[0]),
   84             value,
   85         )
   86 
   87     def test_notify_failure(self):
   88         """
   89         If a failure is passed to `Notifier.notify` then the waiters
   90         are errback'd.
   91         """
   92         n = Notifier()
   93         d = n.wait()
   94         n.notify(Failure(TestException()))
   95         self.failureResultOf(d, TestException)
   96 
   97     def test_nonzero_waiters(self):
   98         """
   99         If there are waiters, ``Notifier`` evaluates as `True`.
  100         """
  101         n = Notifier()
  102         n.wait()
  103         self.assertTrue(n)
  104 
  105     def test_nonzero_no_waiters(self):
  106         """
  107         If there no waiters, ``Notifier`` evaluates as `False`.
  108         """
  109         n = Notifier()
  110         self.assertFalse(n)
  111 
  112     def test_nonzero_cleared_waiters(self):
  113         """
  114         After notifying waiters, ``Notifier`` evaluates as `False`.
  115         """
  116         n = Notifier()
  117         n.wait()
  118         n.notify(object())
  119         self.assertFalse(n)