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)
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
def __init__(self, poller): Stream.__init__(self, poller) self.complete = False self.got_anything = False self.left = 68 self.buff = [] self.count = 0 self.id = None self.piece = None self.smallmessage = SMALLMESSAGE
def __init__(self, poller): ''' Initialize BitTorrent stream ''' Stream.__init__(self, poller) self.complete = False self.got_anything = False # This is the size of the handshake self.left = 68 self.buff = [] self.count = 0 self.id = None self.piece = None
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)
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)
def __init__(self, poller): ''' Initialize the stream ''' Stream.__init__(self, poller) self.incoming = [] self.state = FIRSTLINE self.left = 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)
def __init__(self, poller): Stream.__init__(self, poller) self.incoming = [] self.state = FIRSTLINE self.left = 0