Ticket #2443: trial-tut.patch

File trial-tut.patch, 4.8 KB (added by binjured, 5 years ago)
  • doc/core/howto/listings/trial/calculus/test/test_client_2.py

     
    88
    99class ClientCalculationTestCase(unittest.TestCase):
    1010    def setUp(self):
    11         self.tr = proto_helpers.StringTransportWithDisconnection()
     11        self.tr = proto_helpers.StringTransport()
    1212        self.clock = task.Clock()
    1313        self.proto = RemoteCalculationClient()
    1414        self.tr.protocol = self.proto
  • doc/core/howto/listings/trial/calculus/test/test_client_3.py

     
    88
    99class ClientCalculationTestCase(unittest.TestCase):
    1010    def setUp(self):
    11         self.tr = proto_helpers.StringTransportWithDisconnection()
     11        self.tr = proto_helpers.StringTransport()
    1212        self.clock = task.Clock()
    1313        self.proto = RemoteCalculationClient()
    1414        self.tr.protocol = self.proto
     
    4949
    5050
    5151    def test_timeoutConnectionLost(self):
    52         called = []
    53         def lost(arg):
    54             called.append(True)
    55         self.proto.connectionLost = lost
    56        
    5752        d = self.proto.add(9, 4)
    5853        self.assertEquals(self.tr.value(), 'add 9 4\r\n')
    5954        self.clock.advance(self.proto.timeOut)
    6055
    6156        def check(ignore):
    62             self.assertEquals(called, [True])
     57            self.assertTrue(self.tr.disconnecting)
    6358        return self.assertFailure(d, ClientTimeoutError).addCallback(check)
  • doc/core/howto/listings/trial/calculus/remote_2.py

     
    2626        try:
    2727            result = op(a, b)
    2828        except TypeError:
    29             log.err()
     29            log.err("error")
    3030            self.sendLine("error")
    3131        else:
    3232            self.sendLine(str(result))
  • doc/core/howto/trial.xhtml

     
    447447
    448448<a href="listings/trial/calculus/test/test_client_1.py" class="py-listing">test_client_1.py</a>
    449449
     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>
    450459<p>It's really symmetric to the server test cases. The only tricky part is
    451460that we don't use a client factory. We're lazy, and it's not very useful in
    452461the client part, so we instantiate the protocol directly.</p>
     
    468477
    469478<h3>Testing scheduling</h3>
    470479
    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
     481hour timeout to occur in a test is not very realistic. Twisted provides a
     482solution to this, the <code class="API"
     483base="twisted.internet.task">Clock</code> class that allows one to simulate the
     484passage of time.</p>
    472485
    473486<p>As an example we'll test the code for client request timeout: since our client
    474487uses TCP it can hang for a long time (firewall, connectivity problems, etc...).
     
    536549    return creator.connectTCP('127.0.0.1', self.port.getHost().port).addCallback(cb)
    537550</pre>
    538551
    539 <p>This remove the need of a tearDown method, and you don't have to check for
     552<p>This removes the need for a tearDown method, and you don't have to check for
    540553the value of self.client: you only call addCleanup when the client is
    541554created.</p>
    542555
     
    549562So we'll want a test like this:</p>
    550563
    551564<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")
     565def test_invalidParameters(self):
     566    self.proto.dataReceived('add foo bar\r\n')
     567    self.assertEquals(self.tr.value(), "error\r\n")
    555568</pre>
    556569
    557570<a href="listings/trial/calculus/remote_2.py" class="py-listing">remote_2.py</a>