コード例 #1
0
    def test_litmus_with_authentication(self):
        """Run litmus test suite on HTTP with authentification.

        This test passes
        """
        try:
            proc = Process(target=run_wsgidav_server, args=(True, False))
            proc.daemon = True
            proc.start()
            time.sleep(1)

            try:
                self.assertEqual(
                    subprocess.call([
                        "litmus", "http://127.0.0.1:8080/", "tester", "secret"
                    ]), 0, "litmus suite failed: check the log")
            except OSError:
                print "*" * 70
                print "This test requires the litmus test suite."
                print "See http://www.webdav.org/neon/litmus/"
                print "*" * 70
                raise

        finally:
            proc.terminate()
            proc.join()
コード例 #2
0
ファイル: test_litmus.py プロジェクト: Nonolost/wsgidav
    def test_litmus_with_authentication(self):
        """Run litmus test suite on HTTP with authentification.

        This test passes
        """
        try:
            proc = Process(target=run_wsgidav_server, args=(True, False))
            proc.daemon = True
            proc.start()
            time.sleep(1)

            try:
                self.assertEqual(subprocess.call(["litmus", "http://127.0.0.1:8080/", "tester", "secret"]),
                                 0,
                                 "litmus suite failed: check the log")
            except OSError:
                print "*" * 70
                print "This test requires the litmus test suite."
                print "See http://www.webdav.org/neon/litmus/"
                print "*" * 70
                raise

        finally:
            proc.terminate()
            proc.join()
コード例 #3
0
ファイル: downloader.py プロジェクト: supremefist/KinectBats
class Downloader(object):
    def __init__(self, timeout=30, retries=100, wait=1):
        self.timeout = timeout
        self.retries = retries
        self.wait = wait
        
        self.manager = SyncManager()
        self.manager.start()
        
    def retry_fetch_data(self, url):
        market_data = self.fetch_data(url)
        
        retries = 1
        while not market_data and retries < self.retries:
            print "Retry #%s..." % str(retries)
            market_data = self.fetch_data(url)
            if market_data:
                print "Fetched: " + str(len(market_data))
            else:
                print "Fetched nothing!"
            retries += 1
        
        return market_data
    
    def fetch_data(self, url):
        limit = 60
        msg = "Downloading " + url[0: min(limit, len(url))] 
        if len(url) > limit:
            msg += "(+" + str(len(url) - limit) + ")"
        print msg
            
        return_dict = self.manager.dict()
        self.job = Process(target=get_page_data, args=(url, return_dict))
        self.job.start()
        
        self.job.join(self.timeout)
        if self.job.is_alive():
            self.job.terminate()
        self.job = None
        
        market_data = None
        if 'page' in return_dict:
            market_data = return_dict['page']
        
        if self.wait > 0:
            time.sleep(self.wait)
        
        return market_data
コード例 #4
0
 def test_cleanup(self):
     """
     Test the cleanup thread
     """
     # We want the cleanup thread to run in this context
     # but it is an infitite loop.  So let's delay a shtudown.
     sd2 = "/tmp/shut2"
     rec = {
         'address': '1.2.3.4',
         'ip': '10.128.0.1',
         'router': 'router',
         'last_associated': time(),
         'end_time': 0,
         'uid': 501,
         'user': '******',
         'jobid': '1233',
         'status': 'used'
     }
     self.db.routes2.insert(rec)
     # We want to shutdown the thread that is started on init
     with open(sd2, 'w') as f:
         f.write('1')
     settings = self.settings.copy()
     settings['COLLECTION'] = 'routes2'
     settings['SHUTFILE'] = sd2
     rt = router.Router(settings)
     # Wait for the init thread to shutdown
     sleep(0.2)
     # Now let's start our own
     if os.path.exists(sd2):
         os.remove(sd2)
     shut = Process(target=self._shutdown)
     shut.start()
     rt.cleanup()
     shut.terminate()
     r = self.db.routes2.find_one({'address': '1.2.3.4'})
     self.assertEquals(r['status'], 'available')
     self.db.routes2.remove({})
     rv = rt.cleanup()
     self.assertEquals(-1, rv)
コード例 #5
0
def prepare_proxies(configdata):
    
    if configdata[const.PROXY_CONFIG].get(const.PROXY_CONFIG_SOURCE_TYPE, u'1') != u'2':
        return 
    
    p = Process(group=None, target=fetch_proxy,)
    p.start()
    p.join()
    
    print u'%s get %d free proxy' % (datetime.datetime.now(),
                                   len(open(u'proxy.txt', u'r').readlines()))
    
    c = Process(group=None, target=valid_proxy,)
    c.start()
    
    valid_time = int(configdata[const.PROXY_CONFIG].get(const.PROXY_VALID_TIME))
    print u'%s following %d seconds will valid the proxy' % (datetime.datetime.now(), valid_time)
    time.sleep(valid_time)
    c.terminate()
    
    print u'%s get %d effective proxy' % (datetime.datetime.now(),
                                len(open(u'enable_proxies.txt', u'r').readlines()))
コード例 #6
0
ファイル: chord.py プロジェクト: tomusdrw/mgr.p2p.proxy
class ChordNode:

    process = None
    node = None

    def createNode(self, nodeId, port, cacheStorage, knownHosts):
        BlockingChordNode(nodeId, knownHosts, port, cacheStorage)

    def __init__(self, knownHosts, port=4000, cacheStorage=None):
        # TODO id?
        nodeId = random.randrange(NETWORK_SIZE)

        self.process = Process(target=self.createNode,
                               args=(nodeId, port, cacheStorage, knownHosts))
        self.process.start()
        self.node = ServerProxy(('', port), nodeId, ('localhost', port))

    def terminate(self):
        """
        Kill node process
        """
        self.process.terminate()
コード例 #7
0
class Router:
    def __init__(self, settings):
        logging.debug("Initializing Router")
        self.dbhost = settings['DBHOST']
        self.poll = float(settings['POLLINTERVAL'])
        self.agent = settings['JOBSURL']
        user = settings['RTRUSER']
        mapfile = settings['MAPFILE']
        key = None
        if 'RTRKEY' in settings:
            key = settings['RTRKEY']
        self.map = self._load_mapfile(mapfile)
        self.vyos = vyos_interface.vyosInterface(user, key=key)
        self.ddns = self._init_dns(settings)
        self.shutdown_file = settings.get('SHUTFILE', "/tmp/shutdown_sdn")
        self.collection = settings.get('COLLECTION', 'routes')
        self.cleanup_proc = Process(target=self.cleanup, name='CleanupThread')
        self.cleanup_proc.start()
        client = MongoClient(self.dbhost)
        self.routes = client.sdn[self.collection]
        if self.routes.find_one() is None:
            self.cleanup_proc.terminate()
            raise OSError('DB not initialized')

    def _init_dns(self, settings):
        has_ddns_param = False
        dnsparams = [
            'DNS_BASE', 'DNS_ZONE', 'DNS_SERVER', 'DNS_KEYFILE', 'DNS_PREFIX'
        ]
        for p in dnsparams:
            if p in settings:
                has_ddns_param = True
        if has_ddns_param:
            # Now make sure all are provided
            for param in dnsparams:
                if param not in settings:
                    raise ValueError("Missing %s parameter" % (param))
            return DDNS(base=settings['DNS_BASE'],
                        prefix=settings['DNS_PREFIX'],
                        keyfile=settings['DNS_KEYFILE'],
                        server=settings['DNS_SERVER'],
                        zone=settings['DNS_ZONE'])
        return None

    def shutdown(self):
        self.cleanup_proc.terminate()

    def cleanup(self):
        client = MongoClient(self.dbhost)
        self.routes = client.sdn[self.collection]
        if self.routes.find_one() is None:
            print("DB not intialized")
            return -1
        while True:
            if os.path.exists(self.shutdown_file):
                print("Shut down triggered")
                return 0
            # Look for expired jobs
            now = time.time()
            for route in self.routes.find({
                    'end_time': {
                        '$lt': now
                    },
                    'status': 'used'
            }):
                ip = route['ip']
                logging.warn("route expired %s" % (ip))
                self.release(ip)
            # Get jobs, ignore failures
            try:
                running = self.get_jobs()
                self.check_jobs(running)
            except:
                pass
            time.sleep(self.poll)

    def check_jobs(self, running):
        for route in self.routes.find({'status': 'used'}):
            # check job
            if 'jobid' not in route:
                continue
            if route['jobid'] not in running:
                jobid = route['jobid']
                logging.warn("Job no longer running %s" % (jobid))
                self.release(route['ip'])

    def _get_slurm(self):
        p = Popen('squeue -h -t R -o %%A',
                  shell=True,
                  stdout=PIPE,
                  stderr=STDOUT)
        jobs = []
        for line in p.stdout.readlines():
            jobs.append(line.rstrip())
        return jobs

    def get_jobs(self):
        if self.agent == 'local':
            jobs = self._get_slurm()
        else:
            r = requests.get(self.agent)
            jobs = json.loads(r.text)
        return jobs

    def _load_mapfile(self, mapfile):
        map = dict()
        with open(mapfile) as f:
            for line in f:
                (ip, router) = line.rstrip().split(':')
                map[ip] = router
        return map

    def _get_router(self, ip):
        # Compute the appropriate router for the ip
        # Probably replace with a mongo table
        if ip not in self.map:
            raise ValueError('Bad IP')
        return self.map[ip]

    def available(self):
        addresses = []
        for rec in self.routes.find({'status': AVAILABLE}):
            addresses.append(rec['address'])
        return addresses

    def status(self):
        resp = []
        for rec in self.routes.find({}):
            rec.pop('_id')
            resp.append(rec)
        return resp

    def check_data(self, data):
        required = ['uid', 'end_time', 'jobid']
        for f in required:
            if f not in data:
                raise ValueError('Missing %s' % (f))

    def associate(self, session, data):
        # allocate an address and assocaite it with ip
        ip = session['ip']
        router = self._get_router(ip)
        self.check_data(data)
        if router is None:
            raise ValueError('Invalid IP')
        rec = self.routes.find_one({'ip': ip})
        if rec is not None:
            logging.warn("Already mapped")
            return rec['address']
        rec = self.routes.find_one({'status': AVAILABLE})
        if rec is None:
            logging.warn("No available addresses found")
            return None
        address = rec['address']
        if 'user' not in data:
            data['user'] = '******'
        update = {
            'status': ASSIGNING,
            'ip': ip,
            'router': router,
            'end_time': data['end_time'],
            'user': data['user'],
            'uid': data['uid'],
            'jobid': str(data['jobid']),
            'last_associated': time.time()
        }
        self.routes.update({'address': address}, {'$set': update})
        self.vyos.add_nat(ip, router, address)
        update = {'status': USED, 'last_associated': time.time()}
        self.routes.update({'address': address}, {'$set': update})
        if self.ddns is not None:
            self.ddns.add_dns(str(data['jobid']), address)
        return address

    def release(self, ip):
        # release ip
        rec = self.routes.find_one({'ip': ip})
        if rec is None:
            return 'released'
        update = {
            'status': RELEASING,
        }
        self.routes.update({'ip': ip}, {'$set': update})
        self.vyos.remove_nat(rec['router'], rec['address'])
        update = {
            'status': AVAILABLE,
            'ip': None,
            'router': None,
            'end_time': None,
            'jobid': None,
            'user': None,
            'uid': None
        }
        self.routes.update({'ip': ip}, {'$set': update})
        if self.ddns is not None:
            self.ddns.del_dns(rec['jobid'])
        return "released"
コード例 #8
0
class RemoteTest(unittest.TestCase):
    """
    Test the Component, DataFlow, and VAs when shared remotely.
    The test cases are run as "clients" and at start a server is started.
    """
    container_name = "test"

    def setUp(self):
        # Use Thread for debug:
        if USE_THREADS:
            self.server = threading.Thread(target=ServerLoop,
                                           args=(self.container_name, ))
        else:
            self.server = Process(target=ServerLoop,
                                  args=(self.container_name, ))
        self.server.start()

        self.count = 0
        self.data_arrays_sent = 0
        time.sleep(0.1)  # give it some time to start
        self.rdaemon = Pyro4.Proxy("PYRO:Pyro.Daemon@./u:" +
                                   self.container_name)
        self.comp = self.rdaemon.getObject("mycomp")

    def tearDown(self):
        self.comp.stopServer()
        time.sleep(0.1)  # give it some time to terminate

        if self.server.is_alive():
            if not USE_THREADS:
                print "Warning: killing server still alive"
                self.server.terminate()

#    @unittest.skip("simple")

    def test_simple(self):
        """
        start a component, ping, and stop it
        """

        ret = self.comp.ping()
        self.assertEqual(ret, "pong", "Ping failed")

#    @unittest.skip("simple")

    def test_exception(self):

        # test it raises
        self.assertRaises(MyError, self.comp.bad_call)

        # test it raises when wrong argument
        self.assertRaises(TypeError, self.comp.ping, ("non needed arg", ))

        # non existing method
        self.assertRaises(AttributeError, self.comp.non_existing_method)

#    @unittest.skip("simple")

    def test_roattributes(self):
        """
        check roattributes
        """
        val = self.comp.my_value
        self.assertEqual(val, "ro", "Reading attribute failed")

#    @unittest.skip("simple")

    def test_async(self):
        """
        test futures
        MyComponent queues the future in order of request
        """
        self.comp.set_number_futures(0)

        ft1 = self.comp.do_long(2)  # long enough we can cancel ft2
        ft2 = self.comp.do_long(1)  # shorter than ft1
        self.assertFalse(ft1.done(), "Future finished too early")
        self.assertFalse(ft2.done(), "Future finished too early")
        self.assertFalse(ft2.cancelled(),
                         "future doesn't claim being cancelled")
        self.assertFalse(ft2.cancelled(),
                         "future doesn't claim being cancelled")
        self.assertGreater(ft2.result(), 1)  # wait for ft2
        self.assertFalse(ft2.cancel(), "could cancel the finished future")

        self.assertTrue(ft1.done(), "Future not finished")
        self.assertGreater(ft1.result(), 2)

        self.assertEqual(self.comp.get_number_futures(), 2)

#    @unittest.skip("simple")

    def test_unref_futures(self):
        """
        test many futures which don't even get referenced
        It should behave as if the function does not return anything
        """
        self.comp.set_number_futures(0)

        expected = 100  # there was a bug with expected > threadpool size (=24)
        start = time.time()
        for i in range(expected):
            self.comp.do_long(0.1)

        ft_last = self.comp.do_long(0.1)
        ft_last.result()
        duration = time.time() - start
        self.assertGreaterEqual(duration, expected * 0.1)

        self.assertEqual(self.comp.get_number_futures(), expected + 1)

#    @unittest.skip("simple")

    def test_ref_futures(self):
        """
        test many futures which get referenced and accumulated
        It should behave as if the function does not return anything
        """
        self.comp.set_number_futures(0)
        small_futures = []

        expected = 100  # there was a bug with expected > threadpool size (=24)
        start = time.time()
        for i in range(expected):
            small_futures.append(self.comp.do_long(0.1))

        ft_last = self.comp.do_long(0.1)
        ft_last.result()
        duration = time.time() - start
        self.assertGreaterEqual(duration, expected * 0.1)

        for f in small_futures:
            self.assertTrue(f.done())

        self.assertEqual(self.comp.get_number_futures(), expected + 1)

#    @unittest.skip("simple")

    def test_async_cancel(self):
        """
        test futures
        """
        self.comp.set_number_futures(0)

        ft1 = self.comp.do_long(2)  # long enough we can cancel ft2
        ft2 = self.comp.do_long(1)  # shorter than ft1
        self.assertTrue(ft2.cancel(), "couldn't cancel the future")
        self.assertTrue(ft2.cancelled(),
                        "future doesn't claim being cancelled")
        self.assertRaises(futures.CancelledError, ft2.result)

        # wait for ft1
        res1a = ft1.result()
        self.assertGreater(res1a, 2)
        self.assertTrue(ft1.done(), "Future not finished")
        res1b = ft1.result()
        self.assertEqual(res1a, res1b)
        self.assertGreater(res1b, 2)

        self.assertEqual(self.comp.get_number_futures(), 2)

    def test_prog_future(self):
        """
        Test ProgressiveFuture (remotely)
        """
        self._done_calls = 0
        self._progess_calls = 0
        self._start = None
        self._end = None
        ft = self.comp.do_progressive_long(5)
        ft.add_update_callback(self._on_future_proress)
        ft.add_done_callback(self._on_future_done)

        # we should have received at least one progress update already
        self.assertGreaterEqual(self._progess_calls, 1)

        ft.result()
        self.assertGreaterEqual(self._progess_calls, 5)
        self.assertEqual(self._done_calls, 1)

    def _on_future_done(self, f):
        self._done_calls += 1

    def _on_future_proress(self, f, start, end):
        self._progess_calls += 1
        logging.info("Received future update for %f -> %f", start, end)

        self._start = start
        self._end = end

#    @unittest.skip("simple")

    def test_subcomponents(self):
        # via method and via roattributes
        # need to test cycles

        p = self.rdaemon.getObject("parent")
        self.assertEqual(len(p.children.value), 1,
                         "parent doesn't have one child")
        c = list(p.children.value)[0]
        #        self.assertEqual(c.parent, p, "Component and parent of child is different")
        self.assertEqual(p.value, 42)
        self.assertEqual(c.value, 43)
        self.assertEqual(len(c.children.value), 0,
                         "child shouldn't have children")

#    @unittest.skip("simple")

    def test_dataflow_subscribe(self):
        self.count = 0
        self.expected_shape = (2048, 2048)
        self.data_arrays_sent = 0
        self.comp.data.reset()

        self.comp.data.subscribe(self.receive_data)
        time.sleep(0.5)
        self.comp.data.unsubscribe(self.receive_data)
        count_end = self.count
        print "received %d arrays over %d" % (self.count,
                                              self.data_arrays_sent)

        time.sleep(0.1)
        self.assertEqual(count_end, self.count)
        self.assertGreaterEqual(count_end, 1)

    def test_synchronized_df(self):
        """
        Tests 2 dataflows, one synchronized on the event of acquisition started
        on the other dataflow.
        """
        number = 20
        self.count = 0
        self.left = number
        self.expected_shape = (2, 2)
        self.expected_shape_au = (2048, 2048)
        self.data_arrays_sent = 0
        dfe = self.comp.data
        dfs = self.comp.datas
        dfe.reset()

        dfs.synchronizedOn(self.comp.startAcquire)
        dfs.subscribe(self.receive_data)

        time.sleep(
            0.2)  # long enough to be after the first data if it generates data
        # ensure that datas hasn't generated anything yet
        self.assertEqual(self.count, 0)

        dfe.subscribe(self.receive_data_auto_unsub)
        for i in range(number):
            # end early if it's already finished
            if self.left == 0:
                break
            time.sleep(0.2)  # 0.2s should be more than enough in any case

        self.assertEqual(0, self.left)
        self.assertEqual(number, self.count)
        print "received %d arrays over %d" % (self.count,
                                              self.data_arrays_sent)
        max_lat = dfs.get_max_lat()
        if max_lat:
            print "latency: %r, max= %f, avg= %f" % (
                max_lat, max(max_lat), sum(max_lat) / len(max_lat))

        time.sleep(0.1)
        self.assertEqual(number, self.count)
        dfs.unsubscribe(self.receive_data)
        dfs.synchronizedOn(None)
        time.sleep(0.1)
        self.assertEqual(number, self.count)

#    @unittest.skip("simple")

    def test_dataflow_stridden(self):
        # test that stridden array can be passed (even if less efficient)
        self.count = 0
        self.data_arrays_sent = 0
        self.expected_shape = (2048, 2045)
        self.comp.cut.value = 3
        self.comp.data.reset()

        self.comp.data.subscribe(self.receive_data)
        time.sleep(0.5)
        self.comp.data.unsubscribe(self.receive_data)
        self.comp.cut.value = 0  # put it back
        count_end = self.count
        print "received %d stridden arrays over %d" % (self.count,
                                                       self.data_arrays_sent)

        time.sleep(0.1)
        self.assertEqual(count_end, self.count)
        self.assertGreaterEqual(count_end, 1)

    def test_dataflow_empty(self):
        """
        test passing empty DataArray
        """
        self.count = 0
        self.data_arrays_sent = 0
        self.comp.data.setShape((0, ), 16)
        self.expected_shape = (0, )

        self.comp.data.subscribe(self.receive_data)
        time.sleep(0.5)
        self.comp.data.unsubscribe(self.receive_data)
        count_end = self.count
        print "received %d stridden arrays over %d" % (self.count,
                                                       self.data_arrays_sent)

        time.sleep(0.1)
        self.assertEqual(count_end, self.count)
        self.assertGreaterEqual(count_end, 1)

    def receive_data(self, dataflow, data):
        self.count += 1
        self.assertEqual(data.shape, self.expected_shape)
        if data.ndim >= 2:
            self.data_arrays_sent = data[0][0]
            self.assertGreaterEqual(self.data_arrays_sent, self.count)

    def receive_data_auto_unsub(self, dataflow, data):
        """
        callback for df
        """
        self.assertEqual(data.shape, self.expected_shape_au)
        self.left -= 1
        if self.left <= 0:
            dataflow.unsubscribe(self.receive_data_auto_unsub)

    def receive_data_and_unsubscribe(self, dataflow, data):
        self.count += 1
        self.assertEqual(data.shape, (2048, 2048))
        self.data_arrays_sent = data[0][0]
        self.assertGreaterEqual(self.data_arrays_sent, self.count)
        dataflow.unsubscribe(self.receive_data_and_unsubscribe)

#    @unittest.skip("simple")

    def test_dataflow_unsubscribe_from_callback(self):
        self.count = 0
        self.data_arrays_sent = 0
        self.comp.data.reset()

        self.comp.data.subscribe(self.receive_data_and_unsubscribe)
        time.sleep(0.3)
        self.assertEqual(self.count, 1)
        # It should be 1, or if the generation went very fast, it might be bigger
        self.assertGreaterEqual(self.data_arrays_sent, 1)
#        print "received %d arrays over %d" % (self.count, self.data_arrays_sent)

#        data = comp.data
#        del comp
#        print gc.get_referrers(data)
#        gc.collect()
#        print gc.get_referrers(data)

#    @unittest.skip("simple")

    def test_dataflow_get(self):
        self.comp.data.reset()
        array = self.comp.data.get()
        self.assertEqual(array.shape, (2048, 2048))
        self.assertEqual(array[0][0], 0)

        array = self.comp.data.get()
        self.assertEqual(array.shape, (2048, 2048))
        self.assertEqual(array[0][0], 0)

#    @unittest.skip("simple")

    def test_va_update(self):
        prop = self.comp.prop
        self.assertIsInstance(prop, VigilantAttributeBase)
        self.assertEqual(prop.value, 42)
        prop.value += 1
        self.assertEqual(prop.value, 43)

        self.called = 0
        self.last_value = None
        prop.subscribe(self.receive_va_update)
        # now count
        prop.value = 3  # +1
        prop.value = 0  # +1
        prop.value = 0  # nothing because same value
        time.sleep(0.1)  # give time to receive notifications
        prop.unsubscribe(self.receive_va_update)

        self.assertEqual(prop.value, 0)
        self.assertEqual(self.last_value, 0)
        # called once or twice depending if the brief 3 was seen
        self.assertTrue(1 <= self.called and self.called <= 2)
        called_before = self.called

        # check we are not called anymore
        prop.value = 3  # +1
        self.assertEqual(self.called, called_before)

        # re-subscribe
        prop.subscribe(self.receive_va_update)
        # change remotely
        self.comp.change_prop(45)
        time.sleep(0.1)  # give time to receive notifications
        self.assertEqual(prop.value, 45)
        prop.unsubscribe(self.receive_va_update)
        self.assertEqual(self.called, called_before + 1)

        try:
            prop.value = 7.5
            self.fail("Assigning float to a int should not be allowed.")
        except TypeError:
            pass  # as it should be

    def receive_va_update(self, value):
        self.called += 1
        self.last_value = value
        self.assertIsInstance(value, (int, float))

#    @unittest.skip("simple")

    def test_enumerated_va(self):
        # enumerated
        self.assertEqual(self.comp.enum.value, "a")
        self.assertEqual(self.comp.enum.choices, set(["a", "c", "bfds"]))
        self.comp.enum.value = "c"
        self.assertEqual(self.comp.enum.value, "c")

        try:
            self.comp.enum.value = "wfds"
            self.fail("Assigning out of bound should not be allowed.")
        except IndexError:
            pass  # as it should be

    def test_continuous_va(self):
        # continuous
        self.assertEqual(self.comp.cont.value, 2)
        self.assertEqual(self.comp.cont.range, (-1, 3.4))

        self.comp.cont.value = 3.0
        self.assertEqual(self.comp.cont.value, 3)

        try:
            self.comp.cont.value = 4.0
            self.fail("Assigning out of bound should not be allowed.")
        except IndexError:
            pass  # as it should be

    def test_list_va(self):
        # List
        l = self.comp.listval
        self.assertEqual(len(l.value), 2)
        self.called = 0
        l.subscribe(self.receive_listva_update)
        l.value += [3]
        self.assertEqual(len(l.value), 3)
        time.sleep(0.01)
        l.value[-1] = 4
        self.assertEqual(l.value[-1], 4)
        time.sleep(0.01)
        l.value.reverse()
        self.assertEqual(l.value[0], 4)
        time.sleep(0.1)
        self.assertEqual(self.called, 3)
        l.unsubscribe(self.receive_listva_update)

    def receive_listva_update(self, value):
        self.called += 1
        self.last_value = value
        self.assertIsInstance(value, list)
コード例 #9
0
class AuthorizationCodeTestCase(unittest.TestCase):
    def setUp(self):
        self.client = None
        self.provider = None

    def test_request_access_token(self):
        def run_provider(queue):
            try:

                redirect_uri = "http://127.0.0.1:15487/callback"

                stores = store_factory(client_identifier="abc",
                                       client_secret="xyz",
                                       redirect_uris=[redirect_uri])

                provider = Provider(access_token_store=stores["access_token_store"],
                                    auth_code_store=stores["auth_code_store"],
                                    client_store=stores["client_store"],
                                    site_adapter=TestSiteAdapter(),
                                    token_generator=Uuid4())

                provider.add_grant(AuthorizationCodeGrant(expires_in=120))
                provider.add_grant(RefreshToken(expires_in=60))

                app = Wsgi(server=provider)

                httpd = make_server('', 15486, app,
                                    handler_class=NoLoggingHandler)

                queue.put({"result": 0})

                httpd.serve_forever()
            except Exception as e:
                queue.put({"result": 1, "error_message": str(e)})

        def run_client(queue):
            try:
                app = ClientApplication(
                    callback_url="http://127.0.0.1:15487/callback",
                    client_id="abc",
                    client_secret="xyz",
                    provider_url="http://127.0.0.1:15486")

                httpd = make_server('', 15487, app,
                                    handler_class=NoLoggingHandler)

                queue.put({"result": 0})

                httpd.serve_forever()
            except Exception as e:
                queue.put({"result": 1, "error_message": str(e)})

        uuid_regex = "^[a-z0-9]{8}\-[a-z0-9]{4}\-[a-z0-9]{4}\-[a-z0-9]{4}-[a-z0-9]{12}$"

        ready_queue = Queue()

        self.provider = Process(target=run_provider, args=(ready_queue,))
        self.provider.start()

        provider_started = ready_queue.get()

        if provider_started["result"] != 0:
            raise Exception("Error starting Provider process with message"
                            "'{0}'".format(provider_started["error_message"]))

        self.client = Process(target=run_client, args=(ready_queue,))
        self.client.start()

        client_started = ready_queue.get()

        if client_started["result"] != 0:
            raise Exception("Error starting Client Application process with "
                            "message '{0}'"
                            .format(client_started["error_message"]))

        access_token_result = urlopen("http://127.0.0.1:15487/app").read()

        access_token_data = json.loads(access_token_result.decode('utf-8'))

        self.assertEqual(access_token_data["token_type"], "Bearer")
        self.assertEqual(access_token_data["expires_in"], 120)
        self.assertRegexpMatches(access_token_data["access_token"],
                                 uuid_regex)
        self.assertRegexpMatches(access_token_data["refresh_token"],
                                 uuid_regex)

        request_data = {"grant_type": "refresh_token",
                        "refresh_token": access_token_data["refresh_token"],
                        "client_id": "abc",
                        "client_secret": "xyz"}

        refresh_token_result = urlopen(
            "http://127.0.0.1:15486/token",
            urlencode(request_data).encode('utf-8')
        )

        refresh_token_data = json.loads(refresh_token_result.read().decode('utf-8'))

        self.assertEqual(refresh_token_data["token_type"], "Bearer")
        self.assertEqual(refresh_token_data["expires_in"], 120)
        self.assertRegexpMatches(refresh_token_data["access_token"],
                                 uuid_regex)

    def tearDown(self):
        if self.client is not None:
            self.client.terminate()
            self.client.join()

        if self.provider is not None:
            self.provider.terminate()
            self.provider.join()
コード例 #10
0
class SmtpMessageServer(object):
    """
    This class can start an SMTP debugging server,
    configure LinOTP to talk to it and read the
    results back to the parent tester.

    On open, an SMTP server is set up to listen locally.
    Derived classes can define a hook to set the LinOTP
    configuration to point to this server.

    Example usage:

    with SmtpMessageServer(testcase) as smtp:
        get_otp()
    """
    def __init__(self, testcase, message_timeout):
        self.testcase = testcase

        # We need a minimum version of 2.9.2 to set the SMTP port number, so
        # skip if testing an earlier version
        self.testcase.need_linotp_version('2.9.2')

        self.timeout = message_timeout

        self.set_config = SetConfig(testcase.http_protocol, testcase.http_host,
                                    testcase.http_port, testcase.http_username,
                                    testcase.http_password)

        # We advertise the local SMTP server hostname
        # using the IP address that connects to LinOTP
        self.addr = self._get_local_ip()
        self.msg_payload = None

    def __enter__(self):
        self.smtp_process_queue = Queue()
        self.smtp_process = Process(target=get_otp_mail,
                                    args=(self.smtp_process_queue,
                                          self.timeout))
        self.smtp_process.start()
        self.port = self.smtp_process_queue.get(True, 5)
        self._do_lintop_config()

        return self

    def _do_lintop_config(self):
        parameters = self.get_config_parameters()

        logger.debug("Configuration parameters: %s", parameters)
        result = self.set_config.setConfig(parameters)

        assert result, "It was not possible to set the config. Result:%s" % result

    def get_config_parameters(self):
        # This function can be overridden to provide configuration parameters to configure
        # specific parts of LinOTP
        assert False, "This function should be overridden"

    def get_otp(self):
        messagestr = self.smtp_process_queue.get(True, 10)
        msg = email.message_from_string(messagestr)
        otp = msg.get_payload()

        logger.debug("Received email message payload:%s", otp)

        return otp

    def __exit__(self, *args):
        self.smtp_process_queue.close()
        self.smtp_process.terminate()
        self.smtp_process.join(5)

    def _get_local_ip(self):
        """
        Get the IP address of the interface that connects to
        LinOTP
        """

        with closing(
                socket.create_connection(
                    (self.testcase.http_host, int(self.testcase.http_port)),
                    10)) as s:
            addr = s.getsockname()[0]

        return addr
コード例 #11
0
class SmtpMessageServer(object):
    """
    This class can start an SMTP debugging server,
    configure LinOTP to talk to it and read the
    results back to the parent tester.

    On open, an SMTP server is set up to listen locally.
    Derived classes can define a hook to set the LinOTP
    configuration to point to this server.

    Example usage:

    with SmtpMessageServer(testcase) as smtp:
        get_otp()
    """

    def __init__(self, testcase, message_timeout):
        self.testcase = testcase

        # We need a minimum version of 2.9.2 to set the SMTP port number, so
        # skip if testing an earlier version
        self.testcase.need_linotp_version('2.9.2')

        self.timeout = message_timeout

        self.set_config = SetConfig(testcase.http_protocol,
                                    testcase.http_host,
                                    testcase.http_port,
                                    testcase.http_username,
                                    testcase.http_password)

        # We advertise the local SMTP server hostname
        # using the IP address that connects to LinOTP
        self.addr = self._get_local_ip()
        self.msg_payload = None

    def __enter__(self):
        self.smtp_process_queue = Queue()
        self.smtp_process = Process(
            target=get_otp_mail, args=(self.smtp_process_queue, self.timeout))
        self.smtp_process.start()
        self.port = self.smtp_process_queue.get(True, 5)
        self._do_lintop_config()

        return self

    def _do_lintop_config(self):
        parameters = self.get_config_parameters()

        logger.debug("Configuration parameters: %s", parameters)
        result = self.set_config.setConfig(parameters)

        assert result, "It was not possible to set the config. Result:%s" % result

    def get_config_parameters(self):
        # This function can be overridden to provide configuration parameters to configure
        # specific parts of LinOTP
        assert False, "This function should be overridden"

    def get_otp(self):
        messagestr = self.smtp_process_queue.get(True, 10)
        msg = email.message_from_string(messagestr)
        otp = msg.get_payload()

        logger.debug("Received email message payload:%s", otp)

        return otp

    def __exit__(self, *args):
        self.smtp_process_queue.close()
        self.smtp_process.terminate()
        self.smtp_process.join(5)

    def _get_local_ip(self):
        """
        Get the IP address of the interface that connects to
        LinOTP
        """

        with closing(socket.create_connection((self.testcase.http_host,
                                               int(self.testcase.http_port)),
                                              10)) as s:
            addr = s.getsockname()[0]

        return addr
コード例 #12
0
class AuthorizationCodeTestCase(unittest.TestCase):
    def setUp(self):
        self.client = None
        self.provider = None

    def test_request_access_token(self):
        def run_provider(queue):
            try:

                redirect_uri = "http://127.0.0.1:15487/callback"

                stores = store_factory(client_identifier="abc",
                                       client_secret="xyz",
                                       redirect_uris=[redirect_uri])

                provider = Provider(
                    access_token_store=stores["access_token_store"],
                    auth_code_store=stores["auth_code_store"],
                    client_store=stores["client_store"],
                    site_adapter=TestSiteAdapter(),
                    token_generator=Uuid4())

                provider.add_grant(AuthorizationCodeGrant(expires_in=120))
                provider.add_grant(RefreshToken(expires_in=60))

                app = Wsgi(server=provider)

                httpd = make_server('',
                                    15486,
                                    app,
                                    handler_class=NoLoggingHandler)

                queue.put({"result": 0})

                httpd.serve_forever()
            except Exception as e:
                queue.put({"result": 1, "error_message": str(e)})

        def run_client(queue):
            try:
                app = ClientApplication(
                    callback_url="http://127.0.0.1:15487/callback",
                    client_id="abc",
                    client_secret="xyz",
                    provider_url="http://127.0.0.1:15486")

                httpd = make_server('',
                                    15487,
                                    app,
                                    handler_class=NoLoggingHandler)

                queue.put({"result": 0})

                httpd.serve_forever()
            except Exception as e:
                queue.put({"result": 1, "error_message": str(e)})

        uuid_regex = "^[a-z0-9]{8}\-[a-z0-9]{4}\-[a-z0-9]{4}\-[a-z0-9]{4}-[a-z0-9]{12}$"

        ready_queue = Queue()

        self.provider = Process(target=run_provider, args=(ready_queue, ))
        self.provider.start()

        provider_started = ready_queue.get()

        if provider_started["result"] != 0:
            raise Exception("Error starting Provider process with message"
                            "'{0}'".format(provider_started["error_message"]))

        self.client = Process(target=run_client, args=(ready_queue, ))
        self.client.start()

        client_started = ready_queue.get()

        if client_started["result"] != 0:
            raise Exception("Error starting Client Application process with "
                            "message '{0}'".format(
                                client_started["error_message"]))

        access_token_result = urlopen("http://127.0.0.1:15487/app").read()

        access_token_data = json.loads(access_token_result.decode('utf-8'))

        self.assertEqual(access_token_data["token_type"], "Bearer")
        self.assertEqual(access_token_data["expires_in"], 120)
        self.assertRegexpMatches(access_token_data["access_token"], uuid_regex)
        self.assertRegexpMatches(access_token_data["refresh_token"],
                                 uuid_regex)

        request_data = {
            "grant_type": "refresh_token",
            "refresh_token": access_token_data["refresh_token"],
            "client_id": "abc",
            "client_secret": "xyz"
        }

        refresh_token_result = urlopen("http://127.0.0.1:15486/token",
                                       urlencode(request_data).encode('utf-8'))

        refresh_token_data = json.loads(
            refresh_token_result.read().decode('utf-8'))

        self.assertEqual(refresh_token_data["token_type"], "Bearer")
        self.assertEqual(refresh_token_data["expires_in"], 120)
        self.assertRegexpMatches(refresh_token_data["access_token"],
                                 uuid_regex)

    def tearDown(self):
        if self.client is not None:
            self.client.terminate()
            self.client.join()

        if self.provider is not None:
            self.provider.terminate()
            self.provider.join()
コード例 #13
0
ファイル: mongoredis.py プロジェクト: pnegahdar/MongoRedis
class MongoRedis(object):
    def __init__(self, mongo_db, collection_name='cache'):
        # Ensure index
        if not isinstance(mongo_db, Database):
            raise ValueError(
                'mongo_db must be instance of pymongo.database.Database')
        self.col = mongo_db[collection_name]
        self.col.ensure_index('k', unique=True)
        self.col.ensure_index('exp')
        self.prune_expired()

    def start(self):
        """
        Starts the background process that prunes expired items
        """
        def task():
            while True:
                self.prune_expired()
                pytime.sleep(.5)

        self.processs = Process(target=task)
        self.processs.start()

    def end(self):
        """
        End the background process that prunes expired items
        """
        if not hasattr(self, 'process'):
            self.processs.terminate()

    def prune_expired(self):
        """
        Deletes expired keys from the db, returns count deleted
        """
        now = pytime.time()
        result = self.col.remove({'exp': {'$exists': True, '$lte': now}})
        return result['n']

    ### REDIS COMMANDS ###
    def delete(self, *names):
        """
        Delete one or more keys specified by ``names``
        """
        return self.col.remove({'k': {'$in': names}})['n']

    __delitem__ = delete

    def expire(self, name, time):
        """
        Set an expire flag on key ``name`` for ``time`` seconds. ``time``
        can be represented by an integer or a Python timedelta object.
        """
        expire_at = pytime.time()
        if isinstance(time, datetime.timedelta):
            time = time.seconds + time.days * 24 * 3600
        expire_at += time
        return bool(
            self.col.update({'k': name}, {'$set': {
                'exp': expire_at
            }})['n'])

    def flushdb(self):
        """
        Delete all keys in the current database
        """
        self.col.remove()
        return True

    def get(self, name):
        """
        Return the value at key ``name``, or None if the key doesn't exist
        """
        now = pytime.time()
        result = self.col.find_one({'k': name}) or {}
        if result.get('exp', now) < now:
            return None
        return result.get('v')

    def set(self, name, value, ex=None, px=None, nx=False, xx=False):
        """
        Set the value at key ``name`` to ``value``

        ``ex`` sets an expire flag on key ``name`` for ``ex`` seconds.

        ``px`` sets an expire flag on key ``name`` for ``px`` milliseconds.

        ``nx`` if set to True, set the value at key ``name`` to ``value`` if it
            does not already exist.

        ``xx`` if set to True, set the value at key ``name`` to ``value`` if it
            already exists.
        """
        upsert = True
        expire_at = pytime.time()
        if px:
            # if isinstance(px, datetime.timedelta):
            #     ms = int(px.microseconds / 1000)
            #     px = (px.seconds + px.days * 24 * 3600) * 1000 + ms
            # expire_at += px * 0.001
            raise NotImplementedError  # Millis to fine grained
        elif ex:
            if isinstance(ex, datetime.timedelta):
                ex = ex.seconds + ex.days * 24 * 3600
            expire_at += ex
        if nx:
            try:
                data = {'k': name, 'v': value, 'exp': expire_at}
                if ex:
                    data['exp'] = expire_at
                self.col.save(data)
                return True
            except DuplicateKeyError:
                return None
        elif xx:
            upsert = False
        data = {'v': value}
        if ex:
            data['exp'] = expire_at
        result = self.col.update({'k': name}, {'$set': data}, upsert=upsert)
        return True if result['n'] == 1 else None

    __setitem__ = set

    def ttl(self, name):
        """
        Returns the number of seconds until the key ``name`` will expire
        """
        now = pytime.time()
        exp = (self.col.find_one({'k': name}) or {}).get('exp', now)
        diff = exp - now
        return long(-1) if diff <= 0 else diff
コード例 #14
0
ファイル: remote_test.py プロジェクト: PierreBizouard/odemis
class RemoteTest(unittest.TestCase):
    """
    Test the Component, DataFlow, and VAs when shared remotely.
    The test cases are run as "clients" and at start a server is started.
    """
    container_name = "test"
    
    def setUp(self):
        # Use Thread for debug:
        if USE_THREADS:
            self.server = Thread(target=ServerLoop, args=(self.container_name,))
        else:
            self.server = Process(target=ServerLoop, args=(self.container_name,))
        self.server.start()
        
        self.count = 0
        self.data_arrays_sent = 0
        time.sleep(0.1) # give it some time to start
        self.rdaemon = Pyro4.Proxy("PYRO:Pyro.Daemon@./u:"+self.container_name)
        self.comp = self.rdaemon.getObject("mycomp")

    def tearDown(self):
        self.comp.stopServer()
        time.sleep(0.1) # give it some time to terminate
        
        if self.server.is_alive():
            if not USE_THREADS:
                print "Warning: killing server still alive"
                self.server.terminate()

#    @unittest.skip("simple")
    def test_simple(self):
        """
        start a component, ping, and stop it
        """

        ret = self.comp.ping()
        self.assertEqual(ret, "pong", "Ping failed")

#    @unittest.skip("simple")
    def test_exception(self):
        
        # test it raises
        self.assertRaises(MyError, self.comp.bad_call)
        
        # test it raises when wrong argument
        self.assertRaises(TypeError, self.comp.ping, ("non needed arg",))
        
        # non existing method
        self.assertRaises(AttributeError, self.comp.non_existing_method)
        

#    @unittest.skip("simple")
    def test_roattributes(self):
        """
        check roattributes
        """
        val = self.comp.my_value
        self.assertEqual(val, "ro", "Reading attribute failed")
    
#    @unittest.skip("simple")
    def test_async(self):
        """
        test futures
        MyComponent queues the future in order of request
        """
        self.comp.set_number_futures(0)
        
        ft1 = self.comp.do_long(2) # long enough we can cancel ft2
        ft2 = self.comp.do_long(1) # shorter than ft1
        self.assertFalse(ft1.done(), "Future finished too early")
        self.assertFalse(ft2.done(), "Future finished too early")
        self.assertFalse(ft2.cancelled(), "future doesn't claim being cancelled")
        self.assertFalse(ft2.cancelled(), "future doesn't claim being cancelled")
        self.assertGreater(ft2.result(), 1) # wait for ft2
        self.assertFalse(ft2.cancel(), "could cancel the finished future")

        self.assertTrue(ft1.done(), "Future not finished")
        self.assertGreater(ft1.result(), 2)
        
        self.assertEqual(self.comp.get_number_futures(), 2)
        
#    @unittest.skip("simple")
    def test_unref_futures(self):
        """
        test many futures which don't even get referenced
        It should behave as if the function does not return anything
        """
        self.comp.set_number_futures(0)
        
        expected = 100 # there was a bug with expected > threadpool size (=24)
        start = time.time()
        for i in range(expected):
            self.comp.do_long(0.1)
            
        ft_last = self.comp.do_long(0.1)
        ft_last.result()
        duration = time.time() - start
        self.assertGreaterEqual(duration, expected * 0.1)
        
        self.assertEqual(self.comp.get_number_futures(), expected + 1)
        
#    @unittest.skip("simple")
    def test_ref_futures(self):
        """
        test many futures which get referenced and accumulated
        It should behave as if the function does not return anything
        """
        self.comp.set_number_futures(0)
        small_futures = []
        
        expected = 100 # there was a bug with expected > threadpool size (=24)
        start = time.time()
        for i in range(expected):
            small_futures.append(self.comp.do_long(0.1))
        
        ft_last = self.comp.do_long(0.1)
        ft_last.result()
        duration = time.time() - start
        self.assertGreaterEqual(duration, expected * 0.1)
        
        for f in small_futures:
            self.assertTrue(f.done())
        
        self.assertEqual(self.comp.get_number_futures(), expected + 1)
        
#    @unittest.skip("simple")
    def test_async_cancel(self):
        """
        test futures
        """
        self.comp.set_number_futures(0)
        
        ft1 = self.comp.do_long(2) # long enough we can cancel ft2
        ft2 = self.comp.do_long(1) # shorter than ft1
        self.assertTrue(ft2.cancel(), "couldn't cancel the future")
        self.assertTrue(ft2.cancelled(), "future doesn't claim being cancelled")
        self.assertRaises(futures.CancelledError, ft2.result)

        # wait for ft1
        res1a = ft1.result()
        self.assertGreater(res1a, 2)
        self.assertTrue(ft1.done(), "Future not finished")
        res1b = ft1.result()
        self.assertEqual(res1a, res1b)
        self.assertGreater(res1b, 2)
        
        self.assertEqual(self.comp.get_number_futures(), 2)
        
#    @unittest.skip("simple")
    def test_subcomponents(self):
        # via method and via roattributes
        # need to test cycles
        
        p = self.rdaemon.getObject("parent")
        self.assertEqual(len(p.children), 1, "parent doesn't have one child")
        c = list(p.children)[0]
#        self.assertEqual(c.parent, p, "Component and parent of child is different")
        self.assertEqual(p.value, 42)
        self.assertEqual(c.value, 43)
        self.assertEqual(len(c.children), 0, "child shouldn't have children")
                
#    @unittest.skip("simple")
    def test_dataflow_subscribe(self):
        self.count = 0
        self.expected_shape = (2048, 2048)
        self.data_arrays_sent = 0
        self.comp.data.reset()
        
        self.comp.data.subscribe(self.receive_data)
        time.sleep(0.5)
        self.comp.data.unsubscribe(self.receive_data)
        count_end = self.count
        print "received %d arrays over %d" % (self.count, self.data_arrays_sent)
        
        time.sleep(0.1)
        self.assertEqual(count_end, self.count)
        self.assertGreaterEqual(count_end, 1)
        

    def test_synchronized_df(self):
        """
        Tests 2 dataflows, one synchronized on the event of acquisition started
        on the other dataflow.
        """ 
        number = 20
        self.count = 0
        self.left = number
        self.expected_shape = (2, 2)
        self.expected_shape_au = (2048, 2048)
        self.data_arrays_sent = 0
        dfe = self.comp.data
        dfs = self.comp.datas
        dfe.reset()
        
        dfs.synchronizedOn(self.comp.startAcquire)
        dfs.subscribe(self.receive_data)
        
        time.sleep(0.2) # long enough to be after the first data if it generates data
        # ensure that datas hasn't generated anything yet
        self.assertEqual(self.count, 0)
        
        dfe.subscribe(self.receive_data_auto_unsub)
        for i in range(number):
            # end early if it's already finished
            if self.left == 0:
                break
            time.sleep(0.2) # 0.2s should be more than enough in any case
            
        self.assertEqual(0, self.left)
        self.assertEqual(number, self.count)
        print "received %d arrays over %d" % (self.count, self.data_arrays_sent)
        max_lat = dfs.get_max_lat()
        if max_lat:
            print "latency: %r, max= %f, avg= %f" % (max_lat, max(max_lat), sum(max_lat)/len(max_lat))
        
        time.sleep(0.1)
        self.assertEqual(number, self.count)
        dfs.unsubscribe(self.receive_data)
        dfs.synchronizedOn(None)
        time.sleep(0.1)
        self.assertEqual(number, self.count)
        
#    @unittest.skip("simple")
    def test_dataflow_stridden(self):
        # test that stridden array can be passed (even if less efficient)
        self.count = 0
        self.data_arrays_sent = 0
        self.expected_shape = (2048, 2045)
        self.comp.cut.value = 3
        self.comp.data.reset()
        
        self.comp.data.subscribe(self.receive_data)
        time.sleep(0.5)
        self.comp.data.unsubscribe(self.receive_data)
        self.comp.cut.value = 0 # put it back
        count_end = self.count
        print "received %d stridden arrays over %d" % (self.count, self.data_arrays_sent)
        
        time.sleep(0.1)
        self.assertEqual(count_end, self.count)
        self.assertGreaterEqual(count_end, 1)
        
    def receive_data(self, dataflow, data):
        self.count += 1
        self.assertEqual(data.shape, self.expected_shape)
        self.data_arrays_sent = data[0][0]
        self.assertGreaterEqual(self.data_arrays_sent, self.count)
        
    def receive_data_auto_unsub(self, dataflow, data):
        """
        callback for df
        """
        self.assertEqual(data.shape, self.expected_shape_au)
        self.left -= 1
        if self.left <= 0:
            dataflow.unsubscribe(self.receive_data_auto_unsub)

    def receive_data_and_unsubscribe(self, dataflow, data):
        self.count += 1
        self.assertEqual(data.shape, (2048, 2048))
        self.data_arrays_sent = data[0][0]
        self.assertGreaterEqual(self.data_arrays_sent, self.count)
        dataflow.unsubscribe(self.receive_data_and_unsubscribe)

#    @unittest.skip("simple")
    def test_dataflow_unsubscribe_from_callback(self):
        self.count = 0
        self.data_arrays_sent = 0
        self.comp.data.reset()
        
        self.comp.data.subscribe(self.receive_data_and_unsubscribe)
        time.sleep(0.3)
        self.assertEqual(self.count, 1)
        # It should be 1, or if the generation went very fast, it might be bigger
        self.assertGreaterEqual(self.data_arrays_sent, 1)
#        print "received %d arrays over %d" % (self.count, self.data_arrays_sent)
        
#        data = comp.data
#        del comp
#        print gc.get_referrers(data)
#        gc.collect()
#        print gc.get_referrers(data)
    

#    @unittest.skip("simple")
    def test_dataflow_get(self):
        self.comp.data.reset()
        array = self.comp.data.get()
        self.assertEqual(array.shape, (2048, 2048))
        self.assertEqual(array[0][0], 0)
        
        array = self.comp.data.get()
        self.assertEqual(array.shape, (2048, 2048))
        self.assertEqual(array[0][0], 0)
        
#    @unittest.skip("simple")
    def test_va_update(self):
        prop = self.comp.prop
        self.assertIsInstance(prop, VigilantAttributeBase)
        self.assertEqual(prop.value, 42)
        prop.value += 1
        self.assertEqual(prop.value, 43)
        
        self.called = 0
        self.last_value = None
        prop.subscribe(self.receive_va_update)
        # now count
        prop.value = 3 # +1
        prop.value = 0 # +1
        prop.value = 0 # nothing because same value
        time.sleep(0.1) # give time to receive notifications
        prop.unsubscribe(self.receive_va_update)
        
        self.assertEqual(prop.value, 0)
        self.assertEqual(self.last_value, 0)
        # called once or twice depending if the brief 3 was seen
        self.assertTrue(1 <= self.called and self.called <= 2)
        called_before = self.called
        
        # check we are not called anymore
        prop.value = 3 # +1
        self.assertEqual(self.called, called_before)
        
        # re-subscribe
        prop.subscribe(self.receive_va_update)
        # change remotely
        self.comp.change_prop(45)
        time.sleep(0.1) # give time to receive notifications
        self.assertEqual(prop.value, 45)
        prop.unsubscribe(self.receive_va_update)
        self.assertEqual(self.called, called_before+1)
        
        try:
            prop.value = 7.5
            self.fail("Assigning float to a int should not be allowed.")
        except TypeError:
            pass # as it should be
        
    def receive_va_update(self, value):
        self.called += 1
        self.last_value = value
        self.assertIsInstance(value, (int, float))
    
#    @unittest.skip("simple")
    def test_complex_va(self):
        # enumerated
        self.assertEqual(self.comp.enum.value, "a")
        self.assertEqual(self.comp.enum.choices, set(["a", "c", "bfds"]))
        self.comp.enum.value = "c"
        self.assertEqual(self.comp.enum.value, "c")
                
        try:
            self.comp.enum.value = "wfds"
            self.fail("Assigning out of bound should not be allowed.")
        except IndexError:
            pass # as it should be
        
        # continuous
        self.assertEqual(self.comp.cont.value, 2)
        self.assertEqual(self.comp.cont.range, (-1, 3.4))
        
        self.comp.cont.value = 3.0
        self.assertEqual(self.comp.cont.value, 3)
        
        try:
            self.comp.cont.value = 4.0
            self.fail("Assigning out of bound should not be allowed.")
        except IndexError:
            pass # as it should be
        
        # List
        l = self.comp.listval
        self.assertEqual(len(l.value), 2)
        self.called = 0
        l.subscribe(self.receive_listva_update)
        l.value += [3]
        self.assertEqual(len(l.value), 3)
        time.sleep(0.1)
        self.assertEqual(self.called, 1)
        l.unsubscribe(self.receive_listva_update)

    def receive_listva_update(self, value):
        self.called += 1
        self.last_value = value
        self.assertIsInstance(value, list)
コード例 #15
0
    logFile = open(
        'validationLog.log', 'a'
    )  #open a log file for output -> in case it already exists delete old one?
    try:
        TO_DO = [
            "Data", "Data_HLT", "FullSim", "FullSim_HLT", "FullSim_PU",
            "FullSim_PU_HLT", "FastSim", "FastSim_HLT", "FastSim_PU",
            "FastSim_PU_HLT", "FullFastSim", "FullFastSim_HLT", "Generator"
        ]

        proc.start()
        for i in TO_DO:
            do_some_work(i, logFile)

        #Do some work here
        proc.terminate()
        logFile.close()
        #os.system("echo 'Done.\nElapsed time:\n"+secondsToStr(time.time() - start_time)+"' | mail -s 'RelMon\n' [email protected]")
        os.system(
            "echo 'Done.\nElapsed time:\n%s' | mail -s 'RelMon' [email protected]"
            % (secondsToStr(time.time() - start_time)))
    except SystemExit:
        os.system(
            "echo 'Error' | mail -s 'RelMon' [email protected]")
        sys.exit()
    except:
        proc.terminate()
        print "\n"
        print "Error in main script"
        print traceback.format_exc()
        os.system(
コード例 #16
0
class WhenFunctionalTestingGameClient(unittest.TestCase):
    def setUp(self):
        #setup game server to run on a seperate process
        self.game_server = Process(target=start_server)
        self.game_server.start()

        #create the game client
        self.client = GameClient(host="127.0.0.1", port="5000")

        self.player_one = "Arthur"
        self.player_one_suspect = game_state.PEACOCK
        self.player_two = "Steven"
        self.player_two_suspect = game_state.PLUM

    def test_game_client(self):
        try:
            #give the game server process a chance to start
            time.sleep(3)

            #test registering players and choosing suspects
            self.client.register_player(self.player_one)
            self.client.choose_suspect(self.player_one,
                                       self.player_one_suspect)
            self.client.register_player(self.player_two)
            self.client.choose_suspect(self.player_two,
                                       self.player_two_suspect)

            #retreive the registered players with the client and validate the
            #return values
            players = self.client.get_players()
            for player in players:
                self.assertIsInstance(player, game_state.Player)
            self.assertTrue(
                self.player_one in [player.username for player in players])
            self.assertTrue(
                self.player_two in [player.username for player in players])
            self.assertTrue(self.player_one_suspect in
                            [player.suspect for player in players])
            self.assertTrue(self.player_two_suspect in
                            [player.suspect for player in players])

            #start a new game with the client and validate a GameState object
            #is returned
            game = self.client.start_new_game()
            self.assertTrue(game, game_state.GameState)

            game = self.client.get_game_state(game.game_id)
            self.assertTrue(game, game_state.GameState)

            #move player 1  from start space to hallway
            player = game.current_player
            player_1_current_space = game.game_board[player.suspect]
            move_space = player_1_current_space.connected_spaces[0]
            game = self.client.move_player(player.username, player.suspect,
                                           move_space)
            self.assertEqual(game.turn_status,
                             game_state.AWAITING_ACCUSATION_OR_END_TURN)
            game = self.client.end_turn(player.username)
            player_1_current_space = game.game_board[move_space]
            self.assertEqual(game.turn_status, game_state.AWAITING_MOVE)

            #move player 2  from start space to hallway
            player = game.current_player
            player_2_current_space = game.game_board[player.suspect]
            move_space = player_2_current_space.connected_spaces[0]
            game = self.client.move_player(player.username, player.suspect,
                                           move_space)
            self.assertEqual(game.turn_status,
                             game_state.AWAITING_ACCUSATION_OR_END_TURN)
            game = self.client.end_turn(player.username)

            player_2_current_space = game.game_board[move_space]
            self.assertEqual(game.turn_status, game_state.AWAITING_MOVE)

            #move player 1  from hallway to room
            player = game.current_player
            move_space = player_1_current_space.connected_spaces[0]
            game = self.client.move_player(player.username, player.suspect,
                                           move_space)
            self.assertEqual(game.turn_status, game_state.AWAITING_SUGGESTION)

            #make suggestion based on room player is currently in
            game = self.client.make_suggestion(player.username,
                                               game_state.MUSTARD,
                                               game_state.REVOLVER, move_space)

            #if there is a player that can prove the suggestion false
            #then test the suggestion response
            if game.suggestion_response_player:
                with self.assertRaises(errors.GameClientException):
                    game = self.client.move_player(player.username,
                                                   player.suspect, move_space)
                self.assertEqual(game.turn_status,
                                 game_state.AWAITING_SUGGESTION_RESPONSE)

                response_player = game.suggestion_response_player
                suggestion = game.current_suggestion
                gamecard_item = list(
                    {suggestion.weapon, suggestion.room, suggestion.suspect}
                    & set(card.item for card in response_player.game_cards))[0]
                game = self.client.make_suggestion_response(
                    response_player.username, gamecard_item)

            self.assertEqual(game.turn_status,
                             game_state.AWAITING_ACCUSATION_OR_END_TURN)
            game = self.client.end_turn(player.username)

            self.assertEqual(game.turn_status, game_state.AWAITING_MOVE)

            last_player = player
            player = game.current_player
            self.assertNotEqual(player.username, last_player.username)

            #test accusation
            suspect = [
                card.item for card in game.case_file
                if card.type == game_state.SUSPECT
            ][0]
            weapon = [
                card.item for card in game.case_file
                if card.type == game_state.WEAPON
            ][0]
            room = [
                card.item for card in game.case_file
                if card.type == game_state.ROOM
            ][0]

            game = self.client.make_accusation(player.username, suspect,
                                               weapon, room)

            for message in game.player_messages:
                print message

            self.client.destroy_game(game.game_id)

        finally:
            self.game_server.terminate()
コード例 #17
0
class AuthorizationCodeTestCase(unittest.TestCase):
    def setUp(self):
        self.client = None
        self.server = None

    def test_tornado(self):
        def run_provider(queue):
            try:
                provider = create_provider()

                app = TornadoApplication([
                    url(r"/authorize", OAuth2Handler, dict(provider=provider)),
                    url(r"/token", OAuth2Handler, dict(provider=provider))
                ], debug=True)
                app.listen(15486)

                queue.put({"result": 0})

                IOLoop.current().start()
            except Exception as e:
                queue.put({"result": 1, "error_message": str(e)})

        ready_queue = Queue()

        self.server = Process(target=run_provider, args=(ready_queue,))
        self.server.start()

        provider_started = ready_queue.get()

        if provider_started["result"] != 0:
            raise Exception("Error starting Provider process with message"
                            "'{0}'".format(provider_started["error_message"]))

        self.client = Process(target=run_client, args=(ready_queue,))
        self.client.start()

        client_started = ready_queue.get()

        if client_started["result"] != 0:
            raise Exception("Error starting Client Application process with "
                            "message '{0}'"
                            .format(client_started["error_message"]))

        self.access_token()

    def test_wsgi(self):
        def run_provider(queue):
            try:
                provider = create_provider()

                app = Application(provider=provider)

                httpd = make_server('', 15486, app,
                                    handler_class=NoLoggingHandler)

                queue.put({"result": 0})

                httpd.serve_forever()
            except Exception as e:
                queue.put({"result": 1, "error_message": str(e)})

        ready_queue = Queue()

        self.server = Process(target=run_provider, args=(ready_queue,))
        self.server.start()

        provider_started = ready_queue.get()

        if provider_started["result"] != 0:
            raise Exception("Error starting Provider process with message"
                            "'{0}'".format(provider_started["error_message"]))

        self.client = Process(target=run_client, args=(ready_queue,))
        self.client.start()

        client_started = ready_queue.get()

        if client_started["result"] != 0:
            raise Exception("Error starting Client Application process with "
                            "message '{0}'"
                            .format(client_started["error_message"]))

        self.access_token()

    def test_wsgi_404(self):
        def run_provider(queue):
            try:
                provider = create_provider()

                app = Application(provider=provider)

                httpd = make_server('', 15486, app,
                                    handler_class=NoLoggingHandler)

                queue.put({"result": 0})

                httpd.serve_forever()
            except Exception as e:
                queue.put({"result": 1, "error_message": str(e)})

        ready_queue = Queue()

        self.server = Process(target=run_provider, args=(ready_queue,))
        self.server.start()

        provider_started = ready_queue.get()

        if provider_started["result"] != 0:
            raise Exception("Error starting Provider process with message"
                            "'{0}'".format(provider_started["error_message"]))

        try:
            urlopen("http://127.0.0.1:15486/invalid-path").read()
        except HTTPError as e:
            self.assertEqual(404, e.code)

    def access_token(self):
        uuid_regex = "^[a-z0-9]{8}\-[a-z0-9]{4}\-[a-z0-9]{4}\-[a-z0-9]{4}-[a-z0-9]{12}$"

        try:
            access_token_result = urlopen("http://127.0.0.1:15487/app").read()
        except HTTPError as e:
            print(e.read())
            exit(1)

        access_token_data = json.loads(access_token_result.decode('utf-8'))

        self.assertEqual(access_token_data["token_type"], "Bearer")
        self.assertEqual(access_token_data["expires_in"], 120)
        self.assertRegexpMatches(access_token_data["access_token"],
                                 uuid_regex)
        self.assertRegexpMatches(access_token_data["refresh_token"],
                                 uuid_regex)

        request_data = {"grant_type": "refresh_token",
                        "refresh_token": access_token_data["refresh_token"],
                        "client_id": "abc",
                        "client_secret": "xyz"}

        refresh_token_result = urlopen(
            "http://127.0.0.1:15486/token",
            urlencode(request_data).encode('utf-8')
        )

        refresh_token_data = json.loads(refresh_token_result.read().decode('utf-8'))

        self.assertEqual(refresh_token_data["token_type"], "Bearer")
        self.assertEqual(refresh_token_data["expires_in"], 120)
        self.assertRegexpMatches(refresh_token_data["access_token"],
                                 uuid_regex)

    def tearDown(self):
        if self.client is not None:
            self.client.terminate()
            self.client.join()

        if self.server is not None:
            self.server.terminate()
            self.server.join()
コード例 #18
0
class WhenFunctionalTestingGameClient(unittest.TestCase):

    def setUp(self):
        #setup game server to run on a seperate process
        self.game_server = Process(target=start_server)
        self.game_server.start()

        #create the game client
        self.client = GameClient(host="127.0.0.1", port="5000")

        self.player_one = "Arthur"
        self.player_one_suspect = game_state.PEACOCK
        self.player_two = "Steven"
        self.player_two_suspect = game_state.PLUM

    def test_game_client(self):
        try:
            #give the game server process a chance to start
            time.sleep(3)

            #test registering players and choosing suspects
            self.client.register_player(self.player_one)
            self.client.choose_suspect(
                self.player_one, self.player_one_suspect)
            self.client.register_player(
                self.player_two)
            self.client.choose_suspect(
                self.player_two, self.player_two_suspect)

            #retreive the registered players with the client and validate the
            #return values
            players = self.client.get_players()
            for player in players:
                self.assertIsInstance(player, game_state.Player)
            self.assertTrue(
                self.player_one in [player.username
                for player in players])
            self.assertTrue(
                self.player_two in [player.username
                for player in players])
            self.assertTrue(
                self.player_one_suspect in [player.suspect
                for player in players])
            self.assertTrue(
                self.player_two_suspect in [player.suspect
                for player in players])

            #start a new game with the client and validate a GameState object
            #is returned
            game = self.client.start_new_game()
            self.assertTrue(game, game_state.GameState)

            game = self.client.get_game_state(game.game_id)
            self.assertTrue(game, game_state.GameState)

            #move player 1  from start space to hallway
            player = game.current_player
            player_1_current_space = game.game_board[player.suspect]
            move_space = player_1_current_space.connected_spaces[0]
            game = self.client.move_player(
                player.username, player.suspect, move_space)
            self.assertEqual(
                game.turn_status, game_state.AWAITING_ACCUSATION_OR_END_TURN)
            game = self.client.end_turn(player.username)
            player_1_current_space = game.game_board[move_space]
            self.assertEqual(game.turn_status, game_state.AWAITING_MOVE)

            #move player 2  from start space to hallway
            player = game.current_player
            player_2_current_space = game.game_board[player.suspect]
            move_space = player_2_current_space.connected_spaces[0]
            game = self.client.move_player(
                player.username, player.suspect, move_space)
            self.assertEqual(
                game.turn_status, game_state.AWAITING_ACCUSATION_OR_END_TURN)
            game = self.client.end_turn(player.username)

            player_2_current_space = game.game_board[move_space]
            self.assertEqual(game.turn_status, game_state.AWAITING_MOVE)

            #move player 1  from hallway to room
            player = game.current_player
            move_space = player_1_current_space.connected_spaces[0]
            game = self.client.move_player(
                player.username, player.suspect, move_space)
            self.assertEqual(
                game.turn_status, game_state.AWAITING_SUGGESTION)

            #make suggestion based on room player is currently in
            game = self.client.make_suggestion(
                player.username, game_state.MUSTARD,
                game_state.REVOLVER,
                move_space
            )

            #if there is a player that can prove the suggestion false
            #then test the suggestion response
            if game.suggestion_response_player:
                with self.assertRaises(errors.GameClientException):
                    game = self.client.move_player(
                        player.username, player.suspect, move_space)
                self.assertEqual(
                    game.turn_status, game_state.AWAITING_SUGGESTION_RESPONSE)

                response_player = game.suggestion_response_player
                suggestion = game.current_suggestion
                gamecard_item = list(
                    {suggestion.weapon, suggestion.room, suggestion.suspect}
                    &
                    set(card.item for card in response_player.game_cards))[0]
                game = self.client.make_suggestion_response(
                    response_player.username, gamecard_item)

            self.assertEqual(
                game.turn_status, game_state.AWAITING_ACCUSATION_OR_END_TURN)
            game = self.client.end_turn(player.username)

            self.assertEqual(game.turn_status, game_state.AWAITING_MOVE)

            last_player = player
            player = game.current_player
            self.assertNotEqual(player.username, last_player.username)

            #test accusation
            suspect = [
                card.item for card in game.case_file
                if card.type == game_state.SUSPECT
            ][0]
            weapon = [
                card.item for card in game.case_file
                if card.type == game_state.WEAPON
            ][0]
            room = [
                card.item for card in game.case_file
                if card.type == game_state.ROOM
            ][0]

            game = self.client.make_accusation(
                player.username, suspect, weapon, room)

            for message in  game.player_messages:
                print message

            self.client.destroy_game(game.game_id)

        finally:
            self.game_server.terminate()
コード例 #19
0
ファイル: mongoredis.py プロジェクト: pnegahdar/MongoRedis
class MongoRedis(object):
    def __init__(self, mongo_db, collection_name='cache'):
        # Ensure index
        if not isinstance(mongo_db, Database):
            raise ValueError(
                'mongo_db must be instance of pymongo.database.Database')
        self.col = mongo_db[collection_name]
        self.col.ensure_index('k', unique=True)
        self.col.ensure_index('exp')
        self.prune_expired()

    def start(self):
        """
        Starts the background process that prunes expired items
        """

        def task():
            while True:
                self.prune_expired()
                pytime.sleep(.5)

        self.processs = Process(target=task)
        self.processs.start()

    def end(self):
        """
        End the background process that prunes expired items
        """
        if not hasattr(self, 'process'):
            self.processs.terminate()

    def prune_expired(self):
        """
        Deletes expired keys from the db, returns count deleted
        """
        now = pytime.time()
        result = self.col.remove({'exp': {'$exists': True, '$lte': now}})
        return result['n']

    ### REDIS COMMANDS ###
    def delete(self, *names):
        """
        Delete one or more keys specified by ``names``
        """
        return self.col.remove({'k': {'$in': names}})['n']

    __delitem__ = delete

    def expire(self, name, time):
        """
        Set an expire flag on key ``name`` for ``time`` seconds. ``time``
        can be represented by an integer or a Python timedelta object.
        """
        expire_at = pytime.time()
        if isinstance(time, datetime.timedelta):
            time = time.seconds + time.days * 24 * 3600
        expire_at += time
        return bool(
            self.col.update({'k': name}, {'$set': {'exp': expire_at}})['n'])

    def flushdb(self):
        """
        Delete all keys in the current database
        """
        self.col.remove()
        return True

    def get(self, name):
        """
        Return the value at key ``name``, or None if the key doesn't exist
        """
        now = pytime.time()
        result = self.col.find_one({'k': name}) or {}
        if result.get('exp', now) < now:
            return None
        return result.get('v')

    def set(self, name, value, ex=None, px=None, nx=False, xx=False):
        """
        Set the value at key ``name`` to ``value``

        ``ex`` sets an expire flag on key ``name`` for ``ex`` seconds.

        ``px`` sets an expire flag on key ``name`` for ``px`` milliseconds.

        ``nx`` if set to True, set the value at key ``name`` to ``value`` if it
            does not already exist.

        ``xx`` if set to True, set the value at key ``name`` to ``value`` if it
            already exists.
        """
        upsert = True
        expire_at = pytime.time()
        if px:
            # if isinstance(px, datetime.timedelta):
            #     ms = int(px.microseconds / 1000)
            #     px = (px.seconds + px.days * 24 * 3600) * 1000 + ms
            # expire_at += px * 0.001
            raise NotImplementedError # Millis to fine grained
        elif ex:
            if isinstance(ex, datetime.timedelta):
                ex = ex.seconds + ex.days * 24 * 3600
            expire_at += ex
        if nx:
            try:
                data = {'k': name, 'v': value, 'exp': expire_at}
                if ex:
                    data['exp'] = expire_at
                self.col.save(data)
                return True
            except DuplicateKeyError:
                return None
        elif xx:
            upsert = False
        data = {'v': value}
        if ex:
            data['exp'] = expire_at
        result = self.col.update({'k': name}, {'$set': data}, upsert=upsert)
        return True if result['n'] == 1 else None

    __setitem__ = set

    def ttl(self, name):
        """
        Returns the number of seconds until the key ``name`` will expire
        """
        now = pytime.time()
        exp = (self.col.find_one({'k': name}) or {}).get('exp', now)
        diff = exp - now
        return long(-1) if diff <= 0 else diff