示例#1
0
    def test_at_close(self):
        """Verify _at_close() works as expected"""
        negotiator = _Negotiator()
        negotiator._finalize_response = lambda m, ln: None
        negotiator._send_response = lambda m: None
        negotiator.register("abc", None)

        for _ in range(3):
            stream = Stream(self)
            stream.peername = ("abc", 3456)

            m = {
                 "stream": stream,
                 "ident": sha1stream(stream),
                 "module": "abc",
                }

            # To test the delay queue too
            for _ in range(2):
                negotiator.negotiate(m)

        negotiator._at_close(stream, None)
        self.assertTrue(stream not in negotiator._queue)
        self.assertTrue(stream not in negotiator._known)
        self.assertTrue(stream not in negotiator._delay)

        self.assertTrue(len(negotiator._delay) == 0)
示例#2
0
    def test_negotiate_add(self):
        """Verify negotiate behavior when we add a new stream"""
        stream = Stream(None)
        stream.peername = ("abc", 3456)

        m = {
             "stream": stream,
             "ident": sha1stream(stream),
             "module": "abc",
            }

        negotiator = _Negotiator()
        negotiator._finalize_response = lambda m, ln: None
        negotiator._send_response = lambda m: None
        negotiator.register("abc", None)

        negotiator.negotiate(m)

        # Do we keep track of the new stream?
        self.assertTrue(stream in negotiator._queue)
        self.assertTrue(stream in negotiator._known)

        # Is the watchdog correctly initialized?
        self.assertTrue(negotiator._at_close in stream.atclosev)
        self.assertEqual(stream.watchdog, 300)
        self.assertTrue(utils.ticks() - stream.created < 1)             #XXX
示例#3
0
    def test_negotiate_except(self):
        """Verify negotiate raises KeyError if the module is unknown"""
        stream = Stream(None)
        stream.peername = ("abc", 3456)

        m = {
             "stream": stream,
             "ident": sha1stream(stream),
             "module": "abc",
            }

        negotiator = _Negotiator()
        self.assertRaises(KeyError, negotiator.negotiate, m)
示例#4
0
    def test_negotiate_delay(self):
        """Verify negotiate behavior when we delay response"""
        stream = Stream(None)
        stream.peername = ("abc", 3456)

        m = {
             "stream": stream,
             "ident": sha1stream(stream),
             "module": "abc",
            }

        negotiator = _Negotiator()
        negotiator._finalize_response = lambda m, ln: None
        negotiator._send_response = lambda m: None
        negotiator.register("abc", None)

        for _ in range(2):
            negotiator.negotiate(m)

        self.assertTrue(stream in negotiator._delay)
示例#5
0
    def runTest(self):
        """Simulate clients behavior to stress the Negotiator"""

        # Log file
        #fp = open("simulation.txt", "w")

        # Fake negotiator
        negotiator = _Negotiator()
        negotiator.register("A", FakeNegotiatorModule())
        negotiator.register("B", FakeNegotiatorModule())
        negotiator.register("C", FakeNegotiatorModule())

        # Simulated streams
        streams = []
        for _ in range(8192):
            stream = Stream(FakePoller())
            stream.parent = Object()
            stream.parent.connection_lost = lambda s: None
            stream.sock = Object()
            stream.sock.soclose = lambda: None
            stream.peername = str((str(hash(stream)), 7))
            stream.logname = str((str(hash(stream)), 7))
            streams.append(stream)

        # Track streams we have notified
        notified = set()
        self.send_response = lambda m: notified.add(m["stream"])

        # Track streams that have negotiated
        negotiated = set()

        while streams:
            #
            # Select stream
            #
            idx = random.randrange(len(streams))
            stream = streams[idx]

            #
            # Select event
            # 60% negotiate 20% collect 20% close
            #
            rnd = random.random()
            if rnd < 0.6:

                # Negotiate
                m = {
                     "stream": stream,
                     "ident": sha1stream(stream),
                     "module": chr(random.randrange(ord("A"), ord("A") +3)),
                     "parent": self,    #XXX
                    }

                notified.clear()

                # EOF if random early discard
                try:
                    negotiator.negotiate(m)
                except NegotiatorEOF:
                    #fp.write("%s random-early-discard\n" % sha1stream(stream))
                    streams.remove(stream)
                    stream.close()
                else:
                    self.assertTrue(stream in negotiator._queue)
                    self.assertTrue(stream in negotiator._known)

                    # Not notified?  So must be in the delayed queue
                    if stream not in notified:
                        #fp.write("%s negotiate-delay\n" % sha1stream(stream))
                        self.assertTrue(stream in negotiator._delay)
                    else:
                        #fp.write("%s negotiate-send\n" % sha1stream(stream))
                        pass

                    negotiated.add(stream)

            elif rnd < 0.8:

                # Collect
                m = {
                     "stream": stream,
                     "request_body": "{}",
                     "module": chr(random.randrange(ord("A"), ord("A") +3)),
                     "response_body": "",
                    }

                # EOF if not authorized
                try:
                    negotiator.collect(m)
                except NegotiatorEOF:
                    #fp.write("%s collect-no-auth\n" % sha1stream(stream))
                    streams.remove(stream)
                    stream.close()
                else:
                    #fp.write("%s collect-ok\n" % sha1stream(stream))
                    pass

            else:
                # XXX Don't waste streams w/o a good reason
                if stream not in negotiated:
                    continue

                #fp.write("%s close\n" % sha1stream(stream))
                streams.remove(stream)

                # Remember streams to be notified
                orig = set(negotiator._delay.keys())
                notified.clear()

                stream.close()

                # Make sure we removed stream
                self.assertTrue(stream not in negotiator._queue)
                self.assertTrue(stream not in negotiator._known)
                self.assertTrue(stream not in negotiator._delay)

                # Make sure we notified all
                self.assertEqual(notified, orig - set([stream]))

        # Make sure negotiator is empty
        self.assertEqual(len(negotiator._queue), 0)
        self.assertEqual(len(negotiator._known), 0)
        self.assertEqual(len(negotiator._delay), 0)

        # Make sure modules are empty
        self.assertEqual(len(negotiator._mods["A"]._allow), 0)
        self.assertEqual(len(negotiator._mods["B"]._allow), 0)
        self.assertEqual(len(negotiator._mods["C"]._allow), 0)