Ticket #2443: trial-tut.patch
| File trial-tut.patch, 4.8 KB (added by binjured, 2 years ago) |
|---|
-
doc/core/howto/listings/trial/calculus/test/test_client_2.py
8 8 9 9 class ClientCalculationTestCase(unittest.TestCase): 10 10 def setUp(self): 11 self.tr = proto_helpers.StringTransport WithDisconnection()11 self.tr = proto_helpers.StringTransport() 12 12 self.clock = task.Clock() 13 13 self.proto = RemoteCalculationClient() 14 14 self.tr.protocol = self.proto -
doc/core/howto/listings/trial/calculus/test/test_client_3.py
8 8 9 9 class ClientCalculationTestCase(unittest.TestCase): 10 10 def setUp(self): 11 self.tr = proto_helpers.StringTransport WithDisconnection()11 self.tr = proto_helpers.StringTransport() 12 12 self.clock = task.Clock() 13 13 self.proto = RemoteCalculationClient() 14 14 self.tr.protocol = self.proto … … 49 49 50 50 51 51 def test_timeoutConnectionLost(self): 52 called = []53 def lost(arg):54 called.append(True)55 self.proto.connectionLost = lost56 57 52 d = self.proto.add(9, 4) 58 53 self.assertEquals(self.tr.value(), 'add 9 4\r\n') 59 54 self.clock.advance(self.proto.timeOut) 60 55 61 56 def check(ignore): 62 self.assert Equals(called, [True])57 self.assertTrue(self.tr.disconnecting) 63 58 return self.assertFailure(d, ClientTimeoutError).addCallback(check) -
doc/core/howto/listings/trial/calculus/remote_2.py
26 26 try: 27 27 result = op(a, b) 28 28 except TypeError: 29 log.err( )29 log.err("error") 30 30 self.sendLine("error") 31 31 else: 32 32 self.sendLine(str(result)) -
doc/core/howto/trial.xhtml
447 447 448 448 <a href="listings/trial/calculus/test/test_client_1.py" class="py-listing">test_client_1.py</a> 449 449 450 <div class="note"> 451 <p> 452 The above test makes use of the <code class="python">proto_helpers</code> 453 module, located in <code class="python">trial.test</code>. The 454 <code class="python">proto_helpers</code> module is the <strong>only</strong> 455 public module in <code class="python">trial.test</code> and for this reason 456 may be moved in a later version. 457 </p> 458 </div> 450 459 <p>It's really symmetric to the server test cases. The only tricky part is 451 460 that we don't use a client factory. We're lazy, and it's not very useful in 452 461 the client part, so we instantiate the protocol directly.</p> … … 468 477 469 478 <h3>Testing scheduling</h3> 470 479 471 <p>When testing code that involves the passage of time, waiting e.g. for a two hour timeout to occur in a test is not very realistic. Twisted provides a solution to this, the <code class="API" base="twisted.internet.task">Clock</code> class that allows one to simulate the passage of time.</p> 480 <p>When testing code that involves the passage of time, waiting e.g. for a two 481 hour timeout to occur in a test is not very realistic. Twisted provides a 482 solution to this, the <code class="API" 483 base="twisted.internet.task">Clock</code> class that allows one to simulate the 484 passage of time.</p> 472 485 473 486 <p>As an example we'll test the code for client request timeout: since our client 474 487 uses TCP it can hang for a long time (firewall, connectivity problems, etc...). … … 536 549 return creator.connectTCP('127.0.0.1', self.port.getHost().port).addCallback(cb) 537 550 </pre> 538 551 539 <p>This remove the need ofa tearDown method, and you don't have to check for552 <p>This removes the need for a tearDown method, and you don't have to check for 540 553 the value of self.client: you only call addCleanup when the client is 541 554 created.</p> 542 555 … … 549 562 So we'll want a test like this:</p> 550 563 551 564 <pre class="python"> 552 def test_invalidParameters(self):553 self.proto.dataReceived('add foo bar\r\n')554 self.assertEquals(self.tr.value(), "error\r\n")565 def test_invalidParameters(self): 566 self.proto.dataReceived('add foo bar\r\n') 567 self.assertEquals(self.tr.value(), "error\r\n") 555 568 </pre> 556 569 557 570 <a href="listings/trial/calculus/remote_2.py" class="py-listing">remote_2.py</a>
