Ejemplo n.º 1
0
 def test_pullFailure(self):
     l = []
     s = TestStreamer(['abcd', defer.fail(RuntimeError()), 'ijkl'])
     def test(result):
         result.trap(RuntimeError)
         self.assertEquals(l, ["abcd"])
     return stream.readStream(s, l.append).addErrback(test)
Ejemplo n.º 2
0
 def test_errouput(self):
     p = self.runCode("import sys\nfor i in range(100): sys.stderr.write('x' * 1000)")
     l = []
     d = stream.readStream(p.errStream, l.append)
     def verify(_):
         self.assertEquals("".join(l), ("x" * 1000) * 100)
     p.run()
     return d.addCallback(verify)
Ejemplo n.º 3
0
 def test_output(self):
     p = self.runCode("import sys\nfor i in range(100): sys.stdout.write('x' * 1000)")
     l = []
     d = stream.readStream(p.outStream, l.append)
     def verify(_):
         self.assertEquals("".join(l), ("x" * 1000) * 100)
     d2 = p.run()
     return d.addCallback(verify).addCallback(lambda _: d2)
Ejemplo n.º 4
0
 def assertListing(response):
     data = []
     d = readStream(response.stream, lambda s: data.append(str(s)))
     d.addCallback(lambda _: self.failIf(
         'dir2/' not in "".join(data),
         "'dir2' expected in listing: %r" % (data,)
     ))
     return d
Ejemplo n.º 5
0
 def test_processclosedinput(self):
     p = self.runCode("import sys; sys.stdout.write(sys.stdin.read(3));" +
                      "sys.stdin.close(); sys.stdout.write('def')",
                      "abc123")
     l = []
     d = stream.readStream(p.outStream, l.append)
     def verify(_):
         self.assertEquals("".join(l), "abcdef")
     d2 = p.run()
     return d.addCallback(verify).addCallback(lambda _: d2)
Ejemplo n.º 6
0
 def test_input(self):
     p = self.runCode("import sys\nsys.stdout.write(sys.stdin.read())",
                      "hello world")
     l = []
     d = stream.readStream(p.outStream, l.append)
     d2 = p.run()
     def verify(_):
         self.assertEquals("".join(l), "hello world")
         return d2
     return d.addCallback(verify)
Ejemplo n.º 7
0
def allDataFromStream(stream, filter=None):
    data = []
    def gotAllData(_):
        if not data: return None
        result = "".join([str(x) for x in data])
        if filter is None:
            return result
        else:
            return filter(result)
    return readStream(stream, data.append).addCallback(gotAllData)
Ejemplo n.º 8
0
 def test_inputerror(self):
     p = self.runCode("import sys\nsys.stdout.write(sys.stdin.read())",
                      TestStreamer(["hello", defer.fail(ZeroDivisionError())]))
     l = []
     d = stream.readStream(p.outStream, l.append)
     d2 = p.run()
     def verify(_):
         self.assertEquals("".join(l), "hello")
         return d2
     def cbVerified(ignored):
         excs = self.flushLoggedErrors(ZeroDivisionError)
         self.assertEqual(len(excs), 1)
     return d.addCallback(verify).addCallback(cbVerified)
Ejemplo n.º 9
0
 def test_render(self):
     """
     Test that the result from Data.render is acceptable, including the
     response code, the content-type header, and the actual response body
     itself.
     """
     response = iweb.IResponse(self.data.render(None))
     self.assertEqual(response.code, 200)
     self.assert_(response.headers.hasHeader("content-type"))
     self.assertEqual(response.headers.getHeader("content-type"),
                      http_headers.MimeType("text", "plain"))
     def checkStream(data):
         self.assertEquals(str(data), self.text)
     return stream.readStream(iweb.IResponse(self.data.render(None)).stream,
                              checkStream)
Ejemplo n.º 10
0
def readIntoFile(stream, outFile, maxlen):
    """Read the stream into a file, but not if it's longer than maxlen.
    Returns Deferred which will be triggered on finish.
    """
    curlen = [0]
    def done(_):
        return _
    def write(data):
        curlen[0] += len(data)
        if curlen[0] > maxlen:
            raise MimeFormatError("Maximum length of %d bytes exceeded." %
                                  maxlen)
        
        outFile.write(data)
    return readStream(stream, write).addBoth(done)
Ejemplo n.º 11
0
 def test_processingException(self):
     s = TestStreamer(['abcd', defer.succeed('efgh'), 'ijkl'])
     return stream.readStream(s, lambda x: 1/0).addErrback(
         lambda _: _.trap(ZeroDivisionError))
Ejemplo n.º 12
0
 def test_pullException(self):
     class Failer:
         def read(self): raise RuntimeError
     return stream.readStream(Failer(), lambda _: None).addErrback(
         lambda _: _.trap(RuntimeError))
Ejemplo n.º 13
0
 def test_pull(self):
     l = []
     s = TestStreamer(['abcd', defer.succeed('efgh'), 'ijkl'])
     return stream.readStream(s, l.append).addCallback(
         lambda _: self.assertEquals(l, ["abcd", "efgh", "ijkl"]))
Ejemplo n.º 14
0
def noDataFromStream(stream):
    def gotData(data):
        if data: raise ValueError("Stream contains unexpected data.")
    return readStream(stream, gotData)