forked from twisted/twisted
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge sync-deferred-testing-helper-6105-2
Author: exarkun Reviewer: therve Fixes: twisted#6105 Add `successResultOf`, `failureResultOf`, and `assertNoResult` to `SynchronousTestCase`, to help developers write tests for `Deferred`-using code. git-svn-id: svn://svn.twistedmatrix.com/svn/Twisted/trunk@36506 bbbe8e31-12d6-0310-92fd-ac37d47ddeeb
- Loading branch information
Showing
7 changed files
with
310 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
doc/core/howto/listings/trial/calculus/test/test_client_4.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from calculus.client_3 import RemoteCalculationClient, ClientTimeoutError | ||
|
||
from twisted.internet import task | ||
from twisted.trial import unittest | ||
from twisted.test import proto_helpers | ||
|
||
|
||
|
||
class ClientCalculationTestCase(unittest.TestCase): | ||
def setUp(self): | ||
self.tr = proto_helpers.StringTransportWithDisconnection() | ||
self.clock = task.Clock() | ||
self.proto = RemoteCalculationClient() | ||
self.tr.protocol = self.proto | ||
self.proto.callLater = self.clock.callLater | ||
self.proto.makeConnection(self.tr) | ||
|
||
|
||
def _test(self, operation, a, b, expected): | ||
d = getattr(self.proto, operation)(a, b) | ||
self.assertEqual(self.tr.value(), '%s %d %d\r\n' % (operation, a, b)) | ||
self.tr.clear() | ||
self.proto.dataReceived("%d\r\n" % (expected,)) | ||
self.assertEqual(expected, self.successResultOf(d)) | ||
|
||
|
||
def test_add(self): | ||
self._test('add', 7, 6, 13) | ||
|
||
|
||
def test_subtract(self): | ||
self._test('subtract', 82, 78, 4) | ||
|
||
|
||
def test_multiply(self): | ||
self._test('multiply', 2, 8, 16) | ||
|
||
|
||
def test_divide(self): | ||
self._test('divide', 14, 3, 4) | ||
|
||
|
||
def test_timeout(self): | ||
d = self.proto.add(9, 4) | ||
self.assertEqual(self.tr.value(), 'add 9 4\r\n') | ||
self.clock.advance(self.proto.timeOut) | ||
self.failureResultOf(d).trap(ClientTimeoutError) | ||
|
||
|
||
def test_timeoutConnectionLost(self): | ||
called = [] | ||
def lost(arg): | ||
called.append(True) | ||
self.proto.connectionLost = lost | ||
|
||
d = self.proto.add(9, 4) | ||
self.assertEqual(self.tr.value(), 'add 9 4\r\n') | ||
self.clock.advance(self.proto.timeOut) | ||
|
||
def check(ignore): | ||
self.assertEqual(called, [True]) | ||
self.failureResultOf(d).trap(ClientTimeoutError) | ||
self.assertEqual(called, [True]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Several new methods of twisted.trial.unittest.SynchronousTestCase -- `successResultOf`, `failureResultOf`, and `assertNoResult` - have been added to make testing `Deferred`-using code easier. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.