Esempio n. 1
0
    def testMessageSeekTell(self):
        m = envelope.Envelope()
        ct = "application/binary-stuff"
        m.setContentType(ct)

        line1 = "First line\n"
        line2 = "Second line\n"

        m.write(line1)
        self.failUnlessEqual(m.tell(), len(line1))

        m.write(line2)
        self.failUnlessEqual(m.tell(), len(line1) + len(line2))

        m.seek(0)
        self.failUnlessEqual(m.tell(), 0)

        m.seek(0, 2)
        self.failUnlessEqual(m.tell(), len(line1) + len(line2))

        m.seek(0)
        m.write(line2)
        self.failUnlessEqual(m.tell(), len(line2))

        # Truncate up to 15 bytes
        m.truncate(15)
        self.failUnlessEqual(m.tell(), 15)

        # Seek to 10
        m.seek(10)
        m.truncate()
        self.failUnlessEqual(m.tell(), 10)
Esempio n. 2
0
 def sendMessage(self, message):
     headers, payloadStream, payloadSize = message.freeze()
     e = envelope.Envelope()
     e.setHeaders(headers)
     e.setPayloadStream(payloadStream)
     e.setPayloadSize(payloadSize)
     self.messageQueue.append(e)
Esempio n. 3
0
    def testMessageThawBasic(self):
        m = envelope.Envelope()
        s = StringIO.StringIO(testFrozenMessage1)
        xx = m.thawFromStream(s.read, blocking=True)

        ss = m.getPayloadStream()
        self.failUnlessEqual(ss.read(), testPayload1)
Esempio n. 4
0
    def testMessageThawLead(self):
        m = envelope.Envelope()

        s1 = StringIO.StringIO(testFrozenLead1[:10])
        s2 = StringIO.StringIO(testFrozenLead1[10:])

        self.failIf(m.thawLead(s1.read))
        self.failUnless(m.thawLead(s2.read))
Esempio n. 5
0
    def testMessageThawHeader(self):
        m = envelope.Envelope()

        m.setHeaderSize(len(testFrozenHeader1))

        s1 = StringIO.StringIO(testFrozenHeader1[:4])
        s2 = StringIO.StringIO(testFrozenHeader1[4:])

        self.failIf(m.thawHeader(s1.read))
        self.failUnless(m.thawHeader(s2.read))
Esempio n. 6
0
    def testMessageFreezeBasic(self):
        m = envelope.Envelope()
        payloadStream = StringIO.StringIO(testPayload1)

        m.setPayloadStream(payloadStream)
        m.setPayloadSize(len(testPayload1))
        payloadStream.seek(0)
        m.setContentType('application/octect-stream')

        data = m.freeze()
        self.failUnlessEqual(data, testFrozenMessage1)
Esempio n. 7
0
    def testMessageContentType(self):
        m = envelope.Envelope()
        m.setContentType("text/foobared")

        stream = StringIO.StringIO()
        m.freezeToStream(stream.write)

        stream.seek(0)

        m.thawFromStream(stream.read)

        self.failUnlessEqual(m.getContentType(), "text/foobared")
Esempio n. 8
0
    def processData(self, streamReader, maxRead):
        if self.partialReadEnvelope:
            e = self.partialReadEnvelope
        else:
            e = envelope.Envelope()

        if e.thawFromStream(streamReader):
            self.partialReadEnvelope = None
            m = self.extractMessage(e)
            return m
        else:
            self.partialReadEnvelope = e
            return None
Esempio n. 9
0
    def testSlowMessageRead(self):
        m = envelope.Envelope()
        ct = "application/binary-stuff"
        m.setContentType(ct)

        line1 = "First line\n"
        line2 = "Second line\n"
        m.write(line1)
        m.write(line2)
        data = m.freeze()
        # read in bytes one at a time.
        m = envelope.Envelope()
        for i in range(0, len(data)):
            stream = StringIO.StringIO()
            stream.write(data[i])
            stream.seek(0)
            complete = m.thawFromStream(stream.read)
            if complete:
                break
        assert (i == (len(data) - 1))
        assert (complete)
        assert (m.freeze() == data)
Esempio n. 10
0
    def testMessageWrite(self):
        m = envelope.Envelope()
        ct = "application/binary-stuff"
        m.setContentType(ct)

        line1 = "First line\n"
        line2 = "Second line\n"

        m.write(line1)
        m.write(line2)

        data = m.freeze()
        self.failUnlessEqual(data, testFrozenMessage2)
        self.failUnlessEqual(m.getContentType(), ct)
Esempio n. 11
0
    def testWriteMessageToTwoSources(self):
        # We need to be able to write the same message
        # to two sources simultaneously.
        # Having a writer object gives us a separate marker
        # into the payload stream, etc.
        m = envelope.Envelope()
        ct = "application/binary-stuff"
        m.setContentType(ct)

        line1 = "First line\n"
        line2 = "Second line\n"
        m.write(line1)
        m.write(line2)
        writer1 = m.getWriter()
        writer2 = m.getWriter()
        data = m.freeze()

        outStream1 = StringIO.StringIO()
        outStream2 = StringIO.StringIO()

        def writeone(data):
            outStream1.write(data[0])
            return 1

        def writeone2(data):
            outStream2.write(data[0])
            return 1

        writer1Complete = False
        writer2Complete = False
        while not writer1Complete and not writer2Complete:
            writer1Complete = writer1(writeone)
            writer2Complete = writer2(writeone2)
        outStream1.seek(0)
        assert (outStream1.read() == data)
        outStream2.seek(0)
        assert (outStream2.read() == data)
Esempio n. 12
0
    def testSlowMessageWrite(self):
        m = envelope.Envelope()
        ct = "application/binary-stuff"
        m.setContentType(ct)

        line1 = "First line\n"
        line2 = "Second line\n"
        m.write(line1)
        m.write(line2)
        data = m.freeze()

        outStream = StringIO.StringIO()

        def writeone(data):
            outStream.write(data[0])
            return 1

        for i in range(0, len(data)):
            complete = m.freezeToStream(writeone)
            if complete:
                break
        assert (complete)
        outStream.seek(0)
        assert (outStream.read() == data)
Esempio n. 13
0
    def testMessageThawChunked(self):
        # Tests chunked reads from the stream

        class ChunkedReadStream(object):
            def __init__(self, stream):
                self.stream = stream
                self.chunkSize = 3

            def read(self, size=None):
                toread = self.chunkSize
                if size:
                    toread = min(size, toread)
                return self.stream.read(toread)

        m = envelope.Envelope()
        s = StringIO.StringIO(testFrozenMessage1)

        cs = ChunkedReadStream(s)
        m.thawFromStream(cs.read, blocking=True)

        self.failUnlessEqual(m.readPayload(), testPayload1)

        # Same test, with chunked reads for thawing
        s.seek(0)
        while not m.thawFromStream(cs.read):
            pass

        self.failUnlessEqual(m.readPayload(10), testPayload1[:10])

        # Same test, testing hasComplete*
        s.seek(0)
        m.reset()

        leadSize = envelope.PLead.frozenSize

        bytesRead = 0
        while bytesRead < leadSize - cs.chunkSize:
            m.thawFromStream(cs.read)
            self.failIf(m.hasCompleteLead())
            bytesRead += cs.chunkSize

        m.thawFromStream(cs.read)
        self.failUnless(m.hasCompleteLead())

        self.failIf(m.hasCompleteHeader())

        # Header size
        hs = m.getHeaderSize()

        bytesRead = 0
        while bytesRead < hs - cs.chunkSize:
            m.thawFromStream(cs.read)
            self.failIf(m.hasCompleteHeader())
            bytesRead += cs.chunkSize

        m.thawFromStream(cs.read)
        self.failUnless(m.hasCompleteHeader())

        self.failIf(m.hasCompletePayload())

        # Same reason for not starting with 0 as above
        bytesRead = 0
        while bytesRead < len(testPayload1) - cs.chunkSize:
            m.thawFromStream(cs.read)
            self.failIf(m.hasCompletePayload())
            bytesRead += cs.chunkSize

        m.thawFromStream(cs.read)
        self.failUnless(m.hasCompletePayload())