Esempio n. 1
0
    def testStopTrying(self):
        f = Factory()
        f.protocol = In
        f.connections = 0
        f.allMessages = []

        c = ReconnectingClientFactory()
        c.initialDelay = c.delay = 0.2
        c.protocol = Out
        c.howManyTimes = 2

        port = reactor.listenTCP(0, f)
        PORT = port.getHost().port
        reactor.connectTCP('127.0.0.1', PORT, c)

        now = time.time()
        while len(f.allMessages) != 2 and (time.time() < now + 10):
            reactor.iterate(0.1)
        util.wait(defer.maybeDeferred(port.stopListening))
        
        self.assertEquals(len(f.allMessages), 2,
                          "not enough messages -- %s" % f.allMessages)
        self.assertEquals(f.connections, 2,
                          "Number of successful connections incorrect %d" %
                          f.connections)
        self.assertEquals(f.allMessages, [Out.msgs] * 2)
        self.failIf(c.continueTrying, "stopTrying never called or ineffective")
Esempio n. 2
0
def loopback(server, client, logFile=None):
    """Run session between server and client.
    DEPRECATED in Twisted 2.5. Use loopbackAsync instead.
    """
    import warnings
    warnings.warn('loopback() is deprecated (since Twisted 2.5). '
                  'Use loopbackAsync() instead.',
                  stacklevel=2, category=DeprecationWarning)
    from twisted.internet import reactor
    serverToClient = LoopbackRelay(client, logFile)
    clientToServer = LoopbackRelay(server, logFile)
    server.makeConnection(serverToClient)
    client.makeConnection(clientToServer)
    while 1:
        reactor.iterate(0.01) # this is to clear any deferreds
        serverToClient.clearBuffer()
        clientToServer.clearBuffer()
        if serverToClient.shouldLose:
            serverToClient.clearBuffer()
            server.connectionLost(failure.Failure(main.CONNECTION_DONE))
            break
        elif clientToServer.shouldLose:
            client.connectionLost(failure.Failure(main.CONNECTION_DONE))
            break
    reactor.iterate() # last gasp before I go away
Esempio n. 3
0
    def test_bind_noMatchingServicesFound_fallback_success(self):
        server = self.createServer(
            services=['svc1',
                      'svc2',
                      'svc3',
                      ],
            fallback=True,
            responses=[
            [ pureldap.LDAPSearchResultDone(ldaperrors.Success.resultCode) ],
            [ pureldap.LDAPSearchResultDone(ldaperrors.Success.resultCode) ],
            [ pureldap.LDAPSearchResultDone(ldaperrors.Success.resultCode) ],
            [ pureldap.LDAPBindResponse(resultCode=ldaperrors.Success.resultCode) ],
            ])
        server.dataReceived(str(pureldap.LDAPMessage(pureldap.LDAPBindRequest(dn='cn=jack,dc=example,dc=com', auth='s3krit'), id=4)))
        reactor.iterate() #TODO
        client = server.client

        client.assertSent(
            pureldap.LDAPSearchRequest(baseObject='dc=example,dc=com',
                                       derefAliases=0,
                                       sizeLimit=0,
                                       timeLimit=0,
                                       typesOnly=0,
                                       filter=ldapfilter.parseFilter('(&'
                                                                     +'(objectClass=serviceSecurityObject)'
                                                                     +'(owner=cn=jack,dc=example,dc=com)'
                                                                     +'(cn=svc1)'
                                                                     +('(|(!(validFrom=*))(validFrom<=%s))' % server.now)
                                                                     +('(|(!(validUntil=*))(validUntil>=%s))' % server.now)
                                                                     +')'),
                                       attributes=('1.1',)),
            pureldap.LDAPSearchRequest(baseObject='dc=example,dc=com',
                                       derefAliases=0,
                                       sizeLimit=0,
                                       timeLimit=0,
                                       typesOnly=0,
                                       filter=ldapfilter.parseFilter('(&'
                                                                     +'(objectClass=serviceSecurityObject)'
                                                                     +'(owner=cn=jack,dc=example,dc=com)'
                                                                     +'(cn=svc2)'
                                                                     +('(|(!(validFrom=*))(validFrom<=%s))' % server.now)
                                                                     +('(|(!(validUntil=*))(validUntil>=%s))' % server.now)
                                                                     +')'),
                                       attributes=('1.1',)),
            pureldap.LDAPSearchRequest(baseObject='dc=example,dc=com',
                                       derefAliases=0,
                                       sizeLimit=0,
                                       timeLimit=0,
                                       typesOnly=0,
                                       filter=ldapfilter.parseFilter('(&'
                                                                     +'(objectClass=serviceSecurityObject)'
                                                                     +'(owner=cn=jack,dc=example,dc=com)'
                                                                     +'(cn=svc3)'
                                                                     +('(|(!(validFrom=*))(validFrom<=%s))' % server.now)
                                                                     +('(|(!(validUntil=*))(validUntil>=%s))' % server.now)
                                                                     +')'),
                                       attributes=('1.1',)),
            pureldap.LDAPBindRequest(dn='cn=jack,dc=example,dc=com', auth='s3krit'))
        self.assertEquals(server.transport.value(),
                          str(pureldap.LDAPMessage(pureldap.LDAPBindResponse(resultCode=ldaperrors.Success.resultCode), id=4)))
Esempio n. 4
0
def __try_import(packageName):
	#log.msg("try left "+str(retry)+" for package "+str(packageName))
	imp = None
	async_call = 0
	
	while imp == None:
		packageFile = packagename_to_packagefile(packageName)
		
		#log.msg(str(packageFile))
		#log.msg("Loop")
		#if source file does not exist
		#log.msg("package file "+packageFile)
		if not (os.path.isfile('upload/' + packageFile) or os.path.isfile(packageFile)):
			#require it at first call
			if (async_call%20)==0:
				threads.deferToThread(__async_import, packageName)
			async_call+=1	
			reactor.iterate(4)
		else:
			try:
				#log.msg("try importing")
				imp = __import__(packageName)	
			except (Exception):
				import traceback
				traceback.print_exc(file=sys.stdout)
				log.msg("Error during import")
				return None
			#log.msg(imp)
		
		

		#try to reimport it with a retry less
	#log.msg("Return __try_import "+str(imp))
	return imp
Esempio n. 5
0
 def testPortforward(self):
     serverProtocol = wire.Echo()
     realServerFactory = protocol.ServerFactory()
     realServerFactory.protocol = lambda: serverProtocol
     realServerPort = reactor.listenTCP(0, realServerFactory,
                                        interface='127.0.0.1')
     proxyServerFactory = portforward.ProxyFactory('127.0.0.1',
                                                   realServerPort.getHost().port)
     proxyServerPort = reactor.listenTCP(0, proxyServerFactory,
                                         interface='127.0.0.1')
     nBytes = 1000
     received = []
     clientProtocol = protocol.Protocol()
     clientProtocol.dataReceived = received.extend
     clientProtocol.connectionMade = lambda: clientProtocol.transport.write('x' * nBytes)
     clientFactory = protocol.ClientFactory()
     clientFactory.protocol = lambda: clientProtocol
     reactor.connectTCP('127.0.0.1', proxyServerPort.getHost().port,
                        clientFactory)
     c = 0
     while len(received) < nBytes and c < 100:
         reactor.iterate(0.01)
         c += 1
     self.assertEquals(''.join(received), 'x' * nBytes)
     clientProtocol.transport.loseConnection()
     serverProtocol.transport.loseConnection()
     return defer.gatherResults([
         defer.maybeDeferred(realServerPort.stopListening),
         defer.maybeDeferred(proxyServerPort.stopListening)])
Esempio n. 6
0
 def test_bind(self):
     server = self.createServer([ pureldap.LDAPBindResponse(resultCode=0),
                                  ])
     server.dataReceived(str(pureldap.LDAPMessage(pureldap.LDAPBindRequest(), id=4)))
     reactor.iterate() #TODO
     self.assertEquals(server.transport.value(),
                       str(pureldap.LDAPMessage(pureldap.LDAPBindResponse(resultCode=0), id=4)))
Esempio n. 7
0
    def testHeadRequest(self):
        self.testArticleRequest()
        index = self.result[0]

        try: del self.result
        except: pass

        try: del self.error
        except: pass

        d = self.backend.headRequest("alt.test.nntp", index)
        d.addCallbacks(self.callback, self.errback)

        id = reactor.callLater(5, self.timeout)
        while not hasattr(self, 'result') and not hasattr(self, 'error'):
            reactor.iterate()
        try:
            id.cancel()
        except ValueError: pass

        error = getattr(self, 'error', None)
        if error:
            raise error[0], error[1], error[2]

        self.failUnless(self.result[1] == MESSAGE_ID,
                        "callback result Message-Id doesn't match: %s vs %s" %
                        (MESSAGE_ID, self.result[1]))

        self.failUnless(self.result[2][-2:] == '\r\n',
                        "headers must be \\r\\n terminated.")
Esempio n. 8
0
 def testDeferredFailure(self):
     def raiseError(): raise TypeError
     d = threads.deferToThread(raiseError)
     d.addErrback(self._resultErrback)
     while not self.gotResult:
         reactor.iterate()
     self.gotResult = 0
Esempio n. 9
0
 def buildProtocol(self, addr):
     p = TCPListener()
     ah = listenAirhookStream(randrange(10000,12000), UDPOutFactory(p))
     reactor.iterate()
     c = ah.connectionForAddr(self.oaddr)
     #c.noisy= 1 
     return p
Esempio n. 10
0
    def runReactor(self, timesOrSeconds, seconds=False):
        """DEPRECATED: just return a deferred from your test method and
        trial with do the Right Thing. Alternatively, call
        twisted.trial.util.wait to block until the deferred fires.
        
        I'll iterate the reactor for a while.
        
        You probably want to use expectedAssertions with this.
        
        @type timesOrSeconds: int
        @param timesOrSeconds: Either the number of iterations to run,
               or, if `seconds' is True, the number of seconds to run for.

        @type seconds: bool
        @param seconds: If this is True, `timesOrSeconds' will be
               interpreted as seconds, rather than iterations.
        """
        warnings.warn("runReactor is deprecated. return a deferred from "
                      "your test method, and trial will wait for results."
                      "Alternatively, call twisted.trial.util.wait to"
                      "block until the deferred fires.",
                      DeprecationWarning, stacklevel=2)
        from twisted.internet import reactor

        if seconds:
            reactor.callLater(timesOrSeconds, reactor.crash)
            reactor.run()
            return

        for i in xrange(timesOrSeconds):
            reactor.iterate()
Esempio n. 11
0
 def testOldAddress(self):
     server = Server()
     p = reactor.listenUDP(0, server, interface="127.0.0.1")
     reactor.iterate()
     addr = p.getHost()
     self.assertEquals(addr, ('INET_UDP', addr.host, addr.port))
     p.stopListening()
Esempio n. 12
0
    def testRetr(self):
        # Connect
        client = ftp.FTPClient(passive=self.passive)
        factory = ClientFactory()
        factory.noisy = 0
        factory.buildProtocol = lambda s, c=client: c
        reactor.connectTCP('localhost', self.ftp_port, factory)

        # download ftp_crap
        

        proto = BufferingProtocol()
        d = client.retr(os.path.basename('ftp_crap'), proto)
        d.addCallbacks(self.callback, self.errback)

        # Wait for a result
        id = reactor.callLater(5, self.errback, "timed out") # timeout so we don't freeze
        while not hasattr(self, 'result') and not hasattr(self, 'error'):
            reactor.iterate()
        try:
            id.cancel()
        except ValueError: pass

        error = getattr(self, 'error', None)
        if error:
            raise error[0], error[1], error[2]

        # Check that the file is the same as read directly off the disk
        self.failUnless(type(self.result) == types.ListType,
                        'callback result is wrong type: ' + str(self.result))
        data = proto.buf.getvalue()
        self.failUnless(data == open('ftp_crap', "rb").read(),
                        'RETRieved file does not match original')
Esempio n. 13
0
    def testStor(self):
        # Connect
        client = ftp.FTPClient(passive=self.passive)
        client.debug = 1
        factory = ClientFactory()
        factory.noisy = 0
        factory.buildProtocol = lambda s, c=client: c
        reactor.connectTCP('localhost', self.ftp_port, factory)

        expectedContent = "Hello\n"*4
        
        def gotResult(c):
            c.write(expectedContent)
            c.finish()

        def gotErr(f):
            self.errback(f)

        t = client.storeFile("HelloThere")
        t[0].addCallbacks(gotResult, gotErr)
        t[1].addCallbacks(self.callback, self.errback)

        # Wait for a result
        id = reactor.callLater(5, self.errback, "timed out") # timeout so we don't freeze
        while not hasattr(self, 'result') and not hasattr(self, 'error'):
            reactor.iterate()
        try:
            id.cancel()
        except ValueError: pass

        error = getattr(self, 'error', None)
        if error:
            raise error[0], error[1], error[2]

        self.assertEquals(open('HelloThere').read(), expectedContent)
Esempio n. 14
0
    def testCallLaterDelayAndReset(self):
        self._resetcallbackTime = None
        self._delaycallbackTime = None
        ireset = reactor.callLater(0.4, self._resetcallback)
        idelay = reactor.callLater(0.5, self._delaycallback)
        start = time.time()
        # chug a little before delaying
        while time.time() - start < 0.2:
            reactor.iterate(0.01)
        ireset.reset(0.4) # move expiration from 0.4 to (now)0.2+0.4=0.6
        idelay.delay(0.3) # move expiration from 0.5 to (orig)0.5+0.3=0.8
        # both should be called sometime during this
        while time.time() - start < 2:
            reactor.iterate(0.01)
        ireset_elapsed = self._resetcallbackTime - start
        idelay_elapsed = self._delaycallbackTime - start
        #self.assertApproximates(ireset_elapsed, 0.6, 0.4,
        #                        "ireset fired at %f (wanted 0.6)" % \
        #                        ireset_elapsed)
        #self.assertApproximates(idelay_elapsed, 0.8, 0.4,
        #                        "idelay fired at %f (wanted 0.8)" % \
        #                        idelay_elapsed)
        self.failUnless(idelay_elapsed >= ireset_elapsed,
                        "got %f, %f" % (idelay_elapsed, ireset_elapsed))
        self.failUnless(ireset_elapsed >= (0.6-0.05),
                        "got %f (wanted 0.6)" % ireset_elapsed)
        self.failUnless(idelay_elapsed >= (0.8-0.05),
                        "got %f (wanted 0.8)" % idelay_elapsed)

        del self._resetcallbackTime
        del self._delaycallbackTime
Esempio n. 15
0
    def testShortFileListings(self):

        # Connect
        client = ftp.FTPClient(passive=self.passive)
        factory = ClientFactory()
        factory.noisy = 0
        factory.buildProtocol = lambda s, c=client: c
        reactor.connectTCP('localhost', self.ftp_port, factory)

        # Issue the command and set the callbacks
        p = BufferingProtocol()
        d = client.nlst('.', p)
        d.addCallbacks(self.callback, self.errback)

        # Wait for the result
        id = reactor.callLater(5, self.errback, "timed out") # timeout so we don't freeze
        while not hasattr(self, 'result') and not hasattr(self, 'error'):
            reactor.iterate()
        try:
            id.cancel()
        except ValueError: pass

        error = getattr(self, 'error', None)
        if error:
            raise error[0], error[1], error[2]

        # Check that the listing contains this file (ftp_crap)
        filenames = p.buf.getvalue().split('\r\n')
        self.failUnless('ftp_crap' in filenames,
                        'ftp_crap not in file listing')
Esempio n. 16
0
 def test_script_kill(self):
     self._skipCheck()
     try:
         result = yield self.db.script_kill()
     except redis.NoScriptRunning:
         pass
     except:
         raise self.failureException('%s raised instead of %s:\n %s'
                                     % (sys.exc_info()[0],
                                        'txredisapi.NoScriptRunning',
                                        failure.Failure().getTraceback()))
     else:
         raise self.failureException('%s not raised (%r returned)'
                                     % ('txredisapi.ResponseError', result))
     # Run an infinite loop script from one connection
     # and kill it from another.
     inf_loop = "while 1 do end"
     self.db1 = yield redis.Connection(REDIS_HOST, REDIS_PORT,
                                       reconnect=False)
     eval_deferred = self.db1.eval(inf_loop)
     reactor.iterate()
     r = yield self.db.script_kill()
     self.assertEqual(r, 'OK')
     try:
         result = yield eval_deferred
     except redis.ResponseError:
         pass
     except:
         raise self.failureException('%s raised instead of %s:\n %s'
                                     % (sys.exc_info()[0],
                                        'txredisapi.ResponseError',
                                        failure.Failure().getTraceback()))
     else:
         raise self.failureException('%s not raised (%r returned)'
                                     % ('txredisapi.ResponseError', result))
Esempio n. 17
0
    def testArticleRequest(self):
        d = self.backend.articleRequest(None, None, MESSAGE_ID)
        d.addCallbacks(self.callback, self.errback)

        id = reactor.callLater(5, self.timeout)
        while not hasattr(self, 'result') and not hasattr(self, 'error'):
            reactor.iterate()
        try:
            id.cancel()
        except ValueError: pass

        error = getattr(self, 'error', None)
        if error:
            raise error[0], error[1], error[2]

        self.failUnless(type(self.result) == types.TupleType,
                        'callback result is wrong type: ' + str(self.result))
        self.failUnless(len(self.result) == 3,
                        'callback result list should have three entries: ' +
                        str(self.result))
        self.failUnless(self.result[1] == MESSAGE_ID,
                        "callback result Message-Id doesn't match: %s vs %s" %
                        (MESSAGE_ID, self.result[1]))
        self.failUnless(self.result[2].find('\r\n\r\n'),
                        "Can't find \\r\\n\\r\\n between header and body")
Esempio n. 18
0
    def testBodyRequest(self):
        self.testArticleRequest()
        index = self.result[0]

        try: del self.result
        except: pass

        try: del self.error
        except: pass

        d = self.backend.bodyRequest("alt.test.nntp", index)
        d.addCallbacks(self.callback, self.errback)

        id = reactor.callLater(5, self.timeout)
        while not hasattr(self, 'result') and not hasattr(self, 'error'):
            reactor.iterate()
        try:
            id.cancel()
        except ValueError: pass

        error = getattr(self, 'error', None)
        if error:
            raise error[0], error[1], error[2]

        self.failUnless(self.result[2][0:4] == 'this', "message body has been altered: " +
                        pformat(self.result[2][0:4]))
Esempio n. 19
0
    def _shutdownTestReactor(self, prevent_stop=False):
        """
        Called at the end of a test reactor run.

        When prevent_stop=True, the reactor will not be stopped.
        """
        if not self._timeout_reached:
            # Everything fine, disable timeout.
            if not self._reactor_timeout_call.cancelled:
                self._reactor_timeout_call.cancel()

        if prevent_stop:
            # Don't continue with stop procedure.
            return

        # Let the reactor know that we want to stop reactor.
        reactor.stop()
        # Let the reactor run one more time to execute the stop code.
        reactor.iterate()

        # Set flag to fake a clean reactor.
        reactor._startedBefore = False
        reactor._started = False
        reactor._justStopped = False
        reactor.running = False
        # Start running has consumed the startup events, so we need
        # to restore them.
        reactor.addSystemEventTrigger(
            'during', 'startup', reactor._reallyStartRunning)
Esempio n. 20
0
 def testHiddenException(self):
     import time
     cl = reactor.callLater(0, self.go)
     timeout = time.time() + 2
     while not (self.finished or time.time() > timeout):
         reactor.iterate(0.1)
     self.failUnless(self.finished)
    def setUp(self):
        CFTPClientTestBase.setUp(self)

        self.startServer()
        cmds = ('-p %i -l testuser '
               '--known-hosts kh_test '
               '--user-authentications publickey '
               '--host-key-algorithms ssh-rsa '
               '-K direct '
               '-i dsa_test '
               '-a --nocache '
               '-v '
               '127.0.0.1')
        port = self.server.getHost().port
        cmds = test_conch._makeArgs((cmds % port).split(), mod='cftp')
        log.msg('running %s %s' % (sys.executable, cmds))
        self.processProtocol = SFTPTestProcess()

        env = os.environ.copy()
        env['PYTHONPATH'] = os.pathsep.join(sys.path)
        reactor.spawnProcess(self.processProtocol, sys.executable, cmds,
                             env=env)

        timeout = time.time() + 10
        while (not self.processProtocol.buffer) and (time.time() < timeout):
            reactor.iterate(0.1)
        if time.time() > timeout:
            self.skip = "couldn't start process"
        else:
            self.processProtocol.clearBuffer()
Esempio n. 22
0
 def _getBatchOutput(self, f):
     fn = tempfile.mktemp()
     open(fn, 'w').write(f)
     l = []
     port = self.server.getHost().port
     cmds = ('-p %i -l testuser '
                 '--known-hosts kh_test '
                 '--user-authentications publickey '
                 '--host-key-algorithms ssh-rsa '
                 '-K direct '
                 '-i dsa_test '
                 '-a --nocache '
                 '-v -b %s 127.0.0.1') % (port, fn)
     cmds = test_conch._makeArgs(cmds.split(), mod='cftp')[1:]
     log.msg('running %s %s' % (sys.executable, cmds))
     env = os.environ.copy()
     env['PYTHONPATH'] = os.pathsep.join(sys.path)
     d = getProcessOutputAndValue(sys.executable, cmds, env=env)
     d.setTimeout(10)
     d.addBoth(l.append)
     while not l:
         if hasattr(self.server.factory, 'proto'):
             self.server.factory.proto.expectedLoseConnection = 1
         reactor.iterate(0.1)
     os.remove(fn)
     result = l[0]
     if isinstance(result, failure.Failure):
         raise result.value
     else:
         log.msg(result[1])
         return result[0]
Esempio n. 23
0
    def testStoreRetrieve(self):
        for i in range(10):
            K = newID()
            V = newID()
            
            for a in range(3):
                self.done = 0
                def _scb(key, value, result):
                    self.done = 1
                self.l[randrange(0, self.num)].storeValueForKey(K, V, _scb)
                while not self.done:
                    reactor.iterate()


                def _rcb(key, val):
                    if not val:
                        self.done = 1
                        self.failUnlessEqual(self.got, 1)
                    elif V in val:
                        self.got = 1
                for x in range(3):
                    self.got = 0
                    self.done = 0
                    self.l[randrange(0, self.num)].valueForKey(K, _rcb)
                    while not self.done:
                        reactor.iterate()
 def test_quit(self):
     """command quit"""
     # not using deferredResult here since it swallows timeout
     deferred = self.factory.check("quit")
     deferred.addCallback(self._assert_quit)
     while not self.done:
         reactor.iterate(0.1)
Esempio n. 25
0
 def testScheduling(self):
     c = Counter()
     for i in range(100):
         self.schedule(c.add)
     for i in range(100):
         reactor.iterate()
     self.assertEquals(c.index, 100)
Esempio n. 26
0
    def test_devicemap_as_deferred(self):
        """Return aggregated device map"""

        # safety timeout
        self.timeout = reactor.callLater(10, self.failed, "retrieving devicemap timed out ... failing")

        d = self.aggregator.getDeviceMap()
        assert isinstance(d, defer.Deferred)

        # caution: as this deferred is ready-to-go, the callback is called *immediately*
        d.addCallback(self.succeeded)
        # i.e., self.succeeded has now been called
        
        # idle until code above triggers succeeded or timeout causes failure
        while not self.done:
             reactor.iterate(0.1)
        
        # will arrive here eventually when either succeeded or failed method has fired
        if self.failure:
            self.failed(self.failure)

        devicemap = self.lastargs[0]
        print devicemap
        assert type(devicemap) == types.DictType
        assert len(devicemap) == 1
        assert 'PseudoDevice' in devicemap
        assert len(devicemap['PseudoDevice']) == 2
        assert 'Device1' in devicemap['PseudoDevice']
        assert 'Device2' in devicemap['PseudoDevice']
Esempio n. 27
0
 def testNotRunAtOnce(self):
     c = Counter()
     self.schedule(c.add)
     # scheduled tasks should not be run at once:
     self.assertEquals(c.index, 0)
     reactor.iterate()
     self.assertEquals(c.index, 1)
Esempio n. 28
0
 def testDumber(self):
     filename = tempfile.mktemp()
     l = reactor.listenUNIX(filename, Factory())
     reactor.connectUNIX(filename, TestClientFactory())
     for i in xrange(100):
         reactor.iterate()
     l.stopListening()
Esempio n. 29
0
 def tearDown(self):
     self.clientPort.stopListening()
     self.serverPort.stopListening()
     for d, uri in self.registry.users.values():
         d.cancel()
     reactor.iterate()
     reactor.iterate()
Esempio n. 30
0
    def _runTest(self, clientProto, serverProto, clientIsServer=False):
        self.clientProto = clientProto
        cf = self.clientFactory = protocol.ClientFactory()
        cf.protocol = lambda: clientProto
        if clientIsServer:
            cf.server = 0
        else:
            cf.client = 1

        self.serverProto = serverProto
        sf = self.serverFactory = protocol.ServerFactory()
        sf.protocol = lambda: serverProto
        if clientIsServer:
            sf.client = 0
        else:
            sf.server = 1

        if clientIsServer:
            inCharge = cf
        else:
            inCharge = sf
        inCharge.done = 0

        port = self.port = reactor.listenTCP(0, sf, interface="127.0.0.1")
        portNo = port.getHost().port

        reactor.connectTCP('127.0.0.1', portNo, cf)

        i = 0
        while i < 1000 and not inCharge.done:
            reactor.iterate(0.01)
            i += 1
        self.failUnless(
            inCharge.done,
            "Never finished reading all lines: %s" % (inCharge.lines,))
Esempio n. 31
0
    def test3(self):
        ## add another that has to catch up
        ## add another
        self.a = self.l[0]
        self.b = Storer(self.group)
        reactor.listenWith(twispread.MultiPort, protocol=self.b)
        #self.b.log.addHandler(logging.StreamHandler())
        #self.b.log.setLevel(logging.DEBUG)
        df = self.a.introduceNewServer(self.b.serverId)
        df.addCallback(self.done3)
        self.a.df.addCallback(self.done31)
        self.b.max = 5

        for i in xrange(100):
            reactor.iterate(0.01)
        self.assertEqual(len(self.x), len(self.a.store))
        self.assertEqual(self.a.store, self.b.store)
Esempio n. 32
0
 def test_unbind_clientEOF(self):
     server = self.createServer([ pureldap.LDAPBindResponse(resultCode=0),
                                  ],
                                [],
                                )
     server.dataReceived(str(pureldap.LDAPMessage(pureldap.LDAPBindRequest(), id=2)))
     reactor.iterate() #TODO
     client = server.client
     client.assertSent(pureldap.LDAPBindRequest())
     self.assertEqual(server.transport.value(),
                       str(pureldap.LDAPMessage(pureldap.LDAPBindResponse(resultCode=0), id=2)))
     server.connectionLost(error.ConnectionDone)
     reactor.iterate() #TODO
     client.assertSent(pureldap.LDAPBindRequest(),
                       'fake-unbind-by-LDAPClientTestDriver')
     self.assertEqual(server.transport.value(),
                       str(pureldap.LDAPMessage(pureldap.LDAPBindResponse(resultCode=0), id=2)))
Esempio n. 33
0
    def testClientStartStop(self):
        f = ClosingFactory()
        p = reactor.listenTCP(0, f, interface="127.0.0.1")
        self.n = p.getHost().port
        self.ports.append(p)
        f.port = p

        spinUntil(lambda: p.connected)

        factory = ClientStartStopFactory()
        reactor.connectTCP("127.0.0.1", self.n, factory)
        self.assert_(factory.started)
        reactor.iterate()
        reactor.iterate()

        spinUntil(lambda: factory.stopped)

        return self.cleanPorts(*self.ports)
Esempio n. 34
0
    def do_cleanPending(cls):
        # don't import reactor when module is loaded
        from twisted.internet import reactor

        # flush short-range timers
        reactor.iterate(0)
        reactor.iterate(0)

        pending = reactor.getDelayedCalls()
        if pending:
            s = PENDING_TIMED_CALLS_MSG
            for p in pending:
                s += " %s\n" % (p, )
                if p.active():
                    p.cancel()  # delete the rest
                else:
                    print "WEIRNESS! pending timed call not active+!"
            raise PendingTimedCallsError(s)
Esempio n. 35
0
    def test_bind_sasl_no_credentials(self):
        # result code 14 is saslInprogress, with some server credentials.
        server = self.createServer([
            pureldap.LDAPBindResponse(resultCode=14,
                                      serverSaslCreds='test123'),
        ])

        server.dataReceived(
            pureldap.LDAPMessage(pureldap.LDAPBindRequest(auth=('GSS-SPNEGO',
                                                                None),
                                                          sasl=True),
                                 id=4).toWire())
        reactor.iterate()  #TODO
        self.assertEqual(
            server.transport.value(),
            pureldap.LDAPMessage(pureldap.LDAPBindResponse(
                resultCode=14, serverSaslCreds='test123'),
                                 id=4).toWire())
Esempio n. 36
0
    def _cleanPending(self):
        """
        Cancel all pending calls and return their string representations.
        """
        reactor = self._getReactor()

        # flush short-range timers
        reactor.iterate(0)
        reactor.iterate(0)

        delayedCallStrings = []
        for p in reactor.getDelayedCalls():
            if p.active():
                delayedString = str(p)
                p.cancel()
            else:
                print("WEIRDNESS! pending timed call not active!")
            delayedCallStrings.append(delayedString)
        return delayedCallStrings
Esempio n. 37
0
def spinWhile(f,
              timeout=DEFAULT_TIMEOUT_DURATION,
              msg="f did not return false before timeout"):
    """spin the reactor while condition returned by f() == True or until
    timeout seconds have elapsed i.e. spin until f() is False
    """
    warnings.warn(
        "Do NOT use spinWhile.  Return a Deferred from your "
        "test.",
        stacklevel=2,
        category=DeprecationWarning)
    assert callable(f)
    from twisted.internet import reactor
    now = time.time()
    stop = now + timeout
    while f():
        if time.time() >= stop:
            raise defer.TimeoutError, msg
        reactor.iterate(0.1)
Esempio n. 38
0
    def testReadLimit(self):
        server = Server()
        c1, c2 = SimpleProtocol(), SimpleProtocol()
        now = time.time()
        tServer = policies.ThrottlingFactory(server, readLimit=10)
        port = reactor.listenTCP(0, tServer, interface="127.0.0.1")
        n = port.getHost()[2]
        self.doIterations()
        for c in c1, c2:
            reactor.connectTCP("127.0.0.1", n, SillyFactory(c))
            self.doIterations()

        c1.transport.write("0123456789")
        c2.transport.write("abcdefghij")
        self.doIterations()
        self.assertEquals(c1.buffer, "0123456789")
        self.assertEquals(c2.buffer, "abcdefghij")
        self.assertEquals(tServer.readThisSecond, 20)

        # we wrote 20 bytes, so after one second it should stop reading
        # and then a second later start reading again
        while time.time() - now < 1.05:
            reactor.iterate()
        self.assertEquals(tServer.readThisSecond, 0)

        # write some more - data should *not* get written for another second
        c1.transport.write("0123456789")
        c2.transport.write("abcdefghij")
        self.doIterations()
        self.assertEquals(c1.buffer, "0123456789")
        self.assertEquals(c2.buffer, "abcdefghij")
        self.assertEquals(tServer.readThisSecond, 0)

        while time.time() - now < 2.05:
            reactor.iterate()
        self.assertEquals(c1.buffer, "01234567890123456789")
        self.assertEquals(c2.buffer, "abcdefghijabcdefghij")
        c1.transport.loseConnection()
        c2.transport.loseConnection()
        port.stopListening()
        for p in tServer.protocols.keys():
            p.loseConnection()
        self.doIterations()
Esempio n. 39
0
 def loop(self, failMsg, cond=True, timeout=TIMEOUT):
     # Loops with a timeout
     self.done = False
     self.timeout = reactor.callLater(timeout, self.failed,
                                      "Timeout: " + failMsg)
     if cond == True:
         while not self.done:
             reactor.iterate(0.1)
     else:
         while self.done != cond:
             reactor.iterate(0.1)
     try:
         self.timeout.cancel()
     except (error.AlreadyCancelled, error.AlreadyCalled):
         pass
     if self.failure:
         self.fail(self.failure)
     if cond:
         self.failUnless((self.done == cond), "Failed: " + failMsg)
Esempio n. 40
0
    def update(self):
        self.milliseconds = self.pygame.time.get_ticks()

        #state machine
        if self.state == PlayerState.IDLE:
            if self.hardware.isWeaponButtonDown(0):
                self.shoot()
            self.handleHits()

        elif self.state == PlayerState.SHOOTING:
            #go back to idle state if the shot is over
            if self.milliseconds >= self.shotEnd:
                self.state = PlayerState.COOLDOWN
                self.cooldownEnd = self.milliseconds + self.cooldownDuration
                #TODO: reset hardware here later!
            self.handleHits()

        elif self.state == PlayerState.COOLDOWN:
            if self.milliseconds >= self.cooldownEnd:
                self.state = PlayerState.IDLE
            self.handleHits()

        elif self.state == PlayerState.DEAD:
            #reset hardware and jump to idle state
            if self.milliseconds >= self.deathEnd:
                self.state = PlayerState.IDLE
                self.hardware.setHitpointLED(i2cAddresses.HITPOINT_WEAPON, 0,
                                             255, 0)
                # flush hits during death
                self.hardware.getWeaponHitResults()
                self.hardware.getHitpointResults(i2cAddresses.HITPOINT_WEAPON)

        #update stuff on the display
        self.display.redraw()

        #update/poll networking stuff
        print("Polling")
        self.networkclient.poll()
        if self.myid == 0:  # if own playerid not yet set by the server
            self.onPlayerIdReceived(self.networkclient.getPlayerId())
        if self.role == PlayerRole.SERVER:
            reactor.iterate()
Esempio n. 41
0
    def blockOn(self):
        """Call this to block and get the result of the wrapped Deferred.
        
        On success this will return the result.
        
        On failure, it will raise an exception.
        """

        self.d.addCallback(self.gotResult)
        self.d.addErrback(self.gotFailure)

        while not self.finished:
            reactor.iterate(TIMEOUT)
            self.count += 1

        if isinstance(self.d.result, dict):
            f = self.d.result.get('failure', None)
            if isinstance(f, failure.Failure):
                f.raiseException()
        return self.d.result
Esempio n. 42
0
    def resolve(self, names):
        """Resolves DNS names in parallel"""
        self._finished = False
        self.results = defaultdict(list)

        deferred_list = []
        for name in names:
            for deferred in self.lookup(name):
                deferred.addCallback(self._extract_records, name)
                deferred.addErrback(self._errback, name)
                deferred_list.append(deferred)

        deferred_list = defer.DeferredList(deferred_list)
        deferred_list.addCallback(self._parse_result)
        deferred_list.addCallback(self._finish)

        while not self._finished:
            reactor.iterate()

        return dict(self.results)
Esempio n. 43
0
 def doSteps(self, steps=1):
     """ Returns true if the connection finished """
     count = 0
     while count < steps:
         reactor.iterate(0.01)
         self.con1ToCon2.clearBuffer()
         self.con2ToCon1.clearBuffer()
         if self.con1ToCon2.shouldLose:
             self.con1ToCon2.clearBuffer()
             count = -1
             break
         elif self.con2ToCon1.shouldLose:
             count = -1
             break
         else:
             count += 1
     if count == -1:
         self.disconnect()
         return True
     return False
Esempio n. 44
0
def loopback(server, client, logFile=None):
    """Run session between server and client.
    """
    from twisted.internet import reactor
    serverToClient = LoopbackRelay(client, logFile)
    clientToServer = LoopbackRelay(server, logFile)
    server.makeConnection(serverToClient)
    client.makeConnection(clientToServer)
    while 1:
        reactor.iterate(0.01)  # this is to clear any deferreds
        serverToClient.clearBuffer()
        clientToServer.clearBuffer()
        if serverToClient.shouldLose:
            serverToClient.clearBuffer()
            break
        elif clientToServer.shouldLose:
            break
    client.connectionLost(failure.Failure(main.CONNECTION_DONE))
    server.connectionLost(failure.Failure(main.CONNECTION_DONE))
    reactor.iterate()  # last gasp before I go away
Esempio n. 45
0
def loopbackTCP(server, client, port=0, noisy=True):
    """Run session between server and client protocol instances over TCP."""
    from twisted.internet import reactor
    f = protocol.Factory()
    f.noisy = noisy
    f.buildProtocol = lambda addr, p=server: p
    serverPort = reactor.listenTCP(port, f, interface='127.0.0.1')
    reactor.iterate()
    clientF = LoopbackClientFactory(client)
    clientF.noisy = noisy
    reactor.connectTCP('127.0.0.1', serverPort.getHost().port, clientF)

    # this needs to wait until:
    #  A: the client has disconnected
    #  B: the server has noticed, and its own socket has disconnected
    #  C: the listening socket has been shut down
    spinUntil(lambda: clientF.disconnected)  # A
    spinWhile(lambda: server.transport.connected)  # B
    serverPort.stopListening()
    spinWhile(lambda: serverPort.connected)  # C
Esempio n. 46
0
    def testIt(self):
        p = self.l[0]
        self.words = ['foo', 'bar', 'bang']
        for i in xrange(6):
            reactor.iterate(0.01)

        for w in self.words:
            p.transport.write(w, self.group)

        for i in xrange(10):
            reactor.doIteration(0.01)

        for p in self.l:
            p.store = [
                msg for msg in p.store if type(msg) == spread.RegularMsgType
            ]
            self.failUnlessEqual(len(p.store), len(self.words))

        for i in xrange(len(self.words)):
            for p in self.l:
                self.failUnlessEqual(p.store[i].message, self.words[i])
Esempio n. 47
0
    def testTcpNoDelay(self):
        f = MyServerFactory()
        port = reactor.listenTCP(0, f, interface="127.0.0.1")

        self.n = port.getHost().port
        self.ports.append(port)
        clientF = MyClientFactory()
        reactor.connectTCP("127.0.0.1", self.n, clientF)

        spinUntil(lambda: (f.called > 0 and getattr(clientF, 'protocol', None)
                           is not None))

        for p in clientF.protocol, f.protocol:
            transport = p.transport
            self.assertEquals(transport.getTcpNoDelay(), 0)
            transport.setTcpNoDelay(1)
            self.assertEquals(transport.getTcpNoDelay(), 1)
            transport.setTcpNoDelay(0)
            reactor.iterate()
            self.assertEquals(transport.getTcpNoDelay(), 0)

        return self.cleanPorts(clientF.protocol.transport, port)
Esempio n. 48
0
def async_test(f, *args, **kwargs):
    "Starts an asynchronous test, waits for it to complete, and returns its result."
    result = []

    def cb(value, good):
        result.append(good)
        result.append(value)

    inlineCallbacks(f)(*args, **kwargs).addCallbacks(callback=cb,
                                                     callbackArgs=[True],
                                                     errback=cb,
                                                     errbackArgs=[False])
    while not result:
        reactor.iterate(0.02)
    if result[0]:
        # Uncomment the following line to check that all the tests
        # really are being run to completion.
        #raise(Exception("Success"))
        return result[1]
    else:
        result[1].printTraceback()
        result[1].raiseException()
    def wait(self, queues=None):
        stop = [False]

        def later():
            stop[0] = True

        delayed_call = reactor.callLater(self.timeout, later)

        self.log_queues(queues)

        qa = self.queues_available(queues)
        while not qa and (not stop[0]):
            reactor.iterate(0.01)
            qa = self.queues_available(queues)

        if qa:
            delayed_call.cancel()
            e = self.pop_next (qa[0])
            self.log_event (e)
            return e
        else:
            raise TimeoutError
Esempio n. 50
0
    def test_bind_match_success(self):
        server = self.createServer(
            services=['svc1',
                      'svc2',
                      'svc3',
                      ],
            fallback=True,
            responses=[

            # svc1
            [ pureldap.LDAPSearchResultEntry(r'cn=svc1+owner=cn\=jack\,dc\=example\,dc\=com,dc=example,dc=com',
                                             attributes=[]),
              pureldap.LDAPSearchResultDone(ldaperrors.Success.resultCode) ],
            [ pureldap.LDAPBindResponse(resultCode=ldaperrors.Success.resultCode) ],
            ])

        server.dataReceived(str(pureldap.LDAPMessage(pureldap.LDAPBindRequest(dn='cn=jack,dc=example,dc=com', auth='secret'), id=4)))
        reactor.iterate() #TODO
        client = server.client

        client.assertSent(
            pureldap.LDAPSearchRequest(baseObject='dc=example,dc=com',
                                       derefAliases=0,
                                       sizeLimit=0,
                                       timeLimit=0,
                                       typesOnly=0,
                                       filter=ldapfilter.parseFilter('(&'
                                                                     +'(objectClass=serviceSecurityObject)'
                                                                     +'(owner=cn=jack,dc=example,dc=com)'
                                                                     +'(cn=svc1)'
                                                                     +('(|(!(validFrom=*))(validFrom<=%s))' % server.now)
                                                                     +('(|(!(validUntil=*))(validUntil>=%s))' % server.now)
                                                                     +')'),
                                       attributes=('1.1',)),
            pureldap.LDAPBindRequest(dn=r'cn=svc1+owner=cn\=jack\,dc\=example\,dc\=com,dc=example,dc=com', auth='secret'),
            )
        self.assertEquals(server.transport.value(),
                          str(pureldap.LDAPMessage(pureldap.LDAPBindResponse(resultCode=ldaperrors.Success.resultCode,
                                                                             matchedDN='cn=jack,dc=example,dc=com'), id=4)))
Esempio n. 51
0
 def test_script_kill(self):
     self._skipCheck()
     try:
         result = yield self.db.script_kill()
     except trex.exceptions.NoScriptRunning:
         pass
     except:
         raise self.failureException(
             '%s raised instead of %s:\n %s' %
             (sys.exc_info()[0], 'trex.exceptions.NoScriptRunning',
              failure.Failure().getTraceback()))
     else:
         raise self.failureException(
             '%s not raised (%r returned)' %
             ('trex.exceptions.ResponseError', result))
     # Run an infinite loop script from one connection
     # and kill it from another.
     inf_loop = "while 1 do end"
     self.db1 = yield redis.Connection(REDIS_HOST,
                                       REDIS_PORT,
                                       reconnect=False)
     eval_deferred = self.db1.eval(inf_loop)
     reactor.iterate()
     r = yield self.db.script_kill()
     self.assertEqual(r, 'OK')
     try:
         result = yield eval_deferred
     except trex.exceptions.ResponseError:
         pass
     except:
         raise self.failureException(
             '%s raised instead of %s:\n %s' %
             (sys.exc_info()[0], 'trex.exceptions.ResponseError',
              failure.Failure().getTraceback()))
     else:
         raise self.failureException(
             '%s not raised (%r returned)' %
             ('trex.exceptions.ResponseError', result))
Esempio n. 52
0
    def testOpenSSLBuffering(self):
        serverProto = self.serverProto = SingleLineServerProtocol()
        clientProto = self.clientProto = RecordingClientProtocol()

        server = protocol.ServerFactory()
        client = self.client = protocol.ClientFactory()

        server.protocol = lambda: serverProto
        client.protocol = lambda: clientProto
        client.buffer = []

        sCTX = ssl.DefaultOpenSSLContextFactory(certPath, certPath)
        cCTX = ssl.ClientContextFactory()

        port = self.port = reactor.listenSSL(0, server, sCTX, interface='127.0.0.1')
        reactor.connectSSL('127.0.0.1', port.getHost().port, client, cCTX)

        i = 0
        while i < 5000 and not client.buffer:
            i += 1
            reactor.iterate()

        self.assertEquals(client.buffer, ["+OK <some crap>\r\n"])
Esempio n. 53
0
    def resolve(self, names):
        """Resolves DNS names in parallel"""
        self._finished = False
        self.results = defaultdict(list)

        deferred_list = []
        for name in names:
            for deferred in self.lookup(name):
                deferred.addCallback(self._extract_records, name)
                deferred.addErrback(self._errback, name)
                deferred_list.append(deferred)

        deferred_list = defer.DeferredList(deferred_list)
        deferred_list.addCallback(self._parse_result)
        deferred_list.addCallback(self._finish)

        while not self._finished:
            reactor.iterate()
        # Although the results are in at this point, we may need an extra
        # iteration to ensure the resolver library closes its UDP sockets
        reactor.iterate()

        return dict(self.results)
Esempio n. 54
0
 def testCorrectOrder(self):
     o = Order()
     self.schedule(o.a)
     self.schedule(o.b)
     self.schedule(o.c)
     reactor.iterate()
     reactor.iterate()
     reactor.iterate()
     self.assertEquals(o.stage, 3)
Esempio n. 55
0
def command(player, command, args):
    if command == 'stop':
        player.kick_all('Server stopped')
        plugin.log(player.username + ' has stopped server')
        time.sleep(1)
        from twisted.internet import reactor
        reactor.removeAll()
        reactor.iterate()
        reactor.stop()
    if command == 'tp':
        if len(args) == 3:
            x, y, z = args[0], args[1], args[2]
            player.set_position(x, y, z)
            player.send_chat('Teleported to X:%s Y:%s Z:%s' %
                             (str(x), str(y), str(z)))
        else:
            player.send_chat('Need 3 args: x,y,z')
    if command == 'item' or command == 'give':
        if len(args) == 3:
            id, count, slot = int(args[0]), int(args[1]), int(args[2])
            player.send_set_slot(id, count, slot)
        else:
            player.send_chat('3 args need')
    if command == 'help':
        player.send_chat(
            '/tp [x, y, z]\n/stop\n/title line1 line2 2\n/give id count slot')
    if command == 'title':
        if len(args) == 3:
            player.send_title(args[0], args[1], 25, int(args[2]) * 25, 25)
        else:
            player.send_chat('3 args need')
    if command == 'brodcast' or command == 'bc':
        if len(args) == 0:
            player.send_chat('Enter your message')
        else:
            player.send_chat_all(''.join(args))
Esempio n. 56
0
def handler(player, command, args):
    if command == "stop":
        player.send_chat("Server stopping... You will be kicked in two seconds")
        time.sleep(2)
        plugin.log(player.nickname + " has stopped the server!")
        from twisted.internet import reactor  # Import reactor and call stop methods
        reactor.removeAll()
        reactor.iterate()
        reactor.stop()
    if command == "tppos":
        # If command called with 3 arguments
        if len(args) == 3:
            # Set player position
            try:
                x, y, z = [float(arg) for arg in args]  # Convert strings to floats
                player.set_position(x, y, z)
                player.send_chat("Teleported to X:{0} Y:{1} Z:{2} successfully".format(x, y, z))
            except ValueError:  # If we can't convert it
                player.send_chat("You need to supply numbers!")
        else:
            player.send_chat("tppos takes 3 arguments: x,y,z")
    if command == "help":
        # TODO: Make something useful here
        player.send_chat("There's nothing here, sorry :(")
Esempio n. 57
0
 def setUp(self):
     self.group = 'test-' + ''.join(
         [chr(random.randint(0, 255)) for x in range(4)]).encode('hex')
     self.a = hashtable.ReplicatedDict(self.group, ':memory:',
                                       random.randint(10000, 64000))
     reactor.iterate(0.01)
     reactor.iterate(0.01)
     reactor.iterate(0.01)
     self.b = hashtable.ReplicatedDict(self.group, ':memory:',
                                       random.randint(10000, 64000),
                                       'localhost:%s' % self.a.port)
Esempio n. 58
0
    def testCallInNextIteration(self):
        calls = []

        def f1():
            calls.append('f1')
            reactor.callLater(0.0, f2)

        def f2():
            calls.append('f2')
            reactor.callLater(0.0, f3)

        def f3():
            calls.append('f3')

        reactor.callLater(0, f1)
        self.assertEquals(calls, [])
        reactor.iterate()
        self.assertEquals(calls, ['f1'])
        reactor.iterate()
        self.assertEquals(calls, ['f1', 'f2'])
        reactor.iterate()
        self.assertEquals(calls, ['f1', 'f2', 'f3'])
Esempio n. 59
0
 def tearDown(self):
     self.port.stopListening()
     reactor.iterate()
     reactor.iterate()
     del self.port
Esempio n. 60
0
 def run(self):
     try:
         reactor.iterate(conf.supybot.drivers.poll())
     except:
         drivers.log.exception('Uncaught exception outside reactor:')