summaryrefslogtreecommitdiffstats
path: root/chrome/common/extensions/docs/server2/future_test.py
blob: 440994bbabecdf2a94e1fc56fb006a16098b3277 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python
# Copyright 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import traceback
import unittest


from future import All, Future, Race
from mock_function import MockFunction


class FutureTest(unittest.TestCase):
  def testNoValueOrDelegate(self):
    self.assertRaises(ValueError, Future)

  def testValue(self):
    future = Future(value=42)
    self.assertEqual(42, future.Get())
    self.assertEqual(42, future.Get())

  def testDelegateValue(self):
    called = [False,]
    def callback():
      self.assertFalse(called[0])
      called[0] = True
      return 42
    future = Future(callback=callback)
    self.assertEqual(42, future.Get())
    self.assertEqual(42, future.Get())

  def testErrorThrowingDelegate(self):
    class FunkyException(Exception):
      pass

    # Set up a chain of functions to test the stack trace.
    def qux():
      raise FunkyException()
    def baz():
      return qux()
    def bar():
      return baz()
    def foo():
      return bar()
    chain = [foo, bar, baz, qux]

    called = [False,]
    def callback():
      self.assertFalse(called[0])
      called[0] = True
      return foo()

    fail = self.fail
    assertTrue = self.assertTrue
    def assert_raises_full_stack(future, err):
      try:
        future.Get()
        fail('Did not raise %s' % err)
      except Exception as e:
        assertTrue(isinstance(e, err))
        stack = traceback.format_exc()
        assertTrue(all(stack.find(fn.__name__) != -1 for fn in chain))

    future = Future(callback=callback)
    assert_raises_full_stack(future, FunkyException)
    assert_raises_full_stack(future, FunkyException)

  def testAll(self):
    def callback_with_value(value):
      return MockFunction(lambda: value)

    # Test a single value.
    callback = callback_with_value(42)
    future = All((Future(callback=callback),))
    self.assertTrue(*callback.CheckAndReset(0))
    self.assertEqual([42], future.Get())
    self.assertTrue(*callback.CheckAndReset(1))

    # Test multiple callbacks.
    callbacks = (callback_with_value(1),
                 callback_with_value(2),
                 callback_with_value(3))
    future = All(Future(callback=callback) for callback in callbacks)
    for callback in callbacks:
      self.assertTrue(*callback.CheckAndReset(0))
    self.assertEqual([1, 2, 3], future.Get())
    for callback in callbacks:
      self.assertTrue(*callback.CheckAndReset(1))

    # Test throwing an error.
    def throws_error():
      raise ValueError()
    callbacks = (callback_with_value(1),
                 callback_with_value(2),
                 MockFunction(throws_error))
    future = All(Future(callback=callback) for callback in callbacks)
    for callback in callbacks:
      self.assertTrue(*callback.CheckAndReset(0))
    # Can't check that the callbacks were actually run because in theory the
    # Futures can be resolved in any order.
    self.assertRaises(ValueError, future.Get)

  def testRaceSuccess(self):
    callback = MockFunction(lambda: 42)

    # Test a single value.
    race = Race((Future(callback=callback),))
    self.assertTrue(*callback.CheckAndReset(0))
    self.assertEqual(42, race.Get())
    self.assertTrue(*callback.CheckAndReset(1))

    # Test multiple success values. Note that we could test different values
    # and check that the first returned, but this is just an implementation
    # detail of Race. When we have parallel Futures this might not always hold.
    race = Race((Future(callback=callback),
                 Future(callback=callback),
                 Future(callback=callback)))
    self.assertTrue(*callback.CheckAndReset(0))
    self.assertEqual(42, race.Get())
    # Can't assert the actual count here for the same reason as above.
    callback.CheckAndReset(99)

    # Test values with except_pass.
    def throws_error():
      raise ValueError()
    race = Race((Future(callback=callback),
                 Future(callback=throws_error)),
                 except_pass=(ValueError,))
    self.assertTrue(*callback.CheckAndReset(0))
    self.assertEqual(42, race.Get())
    self.assertTrue(*callback.CheckAndReset(1))

  def testRaceErrors(self):
    def throws_error():
      raise ValueError()

    # Test a single error.
    race = Race((Future(callback=throws_error),))
    self.assertRaises(ValueError, race.Get)

    # Test multiple errors. Can't use different error types for the same reason
    # as described in testRaceSuccess.
    race = Race((Future(callback=throws_error),
                 Future(callback=throws_error),
                 Future(callback=throws_error)))
    self.assertRaises(ValueError, race.Get)

    # Test values with except_pass.
    def throws_except_error():
      raise NotImplementedError()
    race = Race((Future(callback=throws_error),
                 Future(callback=throws_except_error)),
                 except_pass=(NotImplementedError,))
    self.assertRaises(ValueError, race.Get)

    race = Race((Future(callback=throws_error),
                 Future(callback=throws_error)),
                 except_pass=(ValueError,))
    self.assertRaises(ValueError, race.Get)


if __name__ == '__main__':
  unittest.main()