Exemple #1
0
    def test_subscriptions_post(self):
        pvnames = ("test:long1", "test:long2")
        modedefs = ({"mode": "Monitor", "delta": 1.1},
                    {"mode": "Scan", "period": 0.1})
        self._subscribe_to_many(pvnames, modedefs)

        # read them back
        response = self.fetch('/subscriptions/')
        self.assertEqual(response.code, 200)
        self.assertFalse(response.error)

        res = [ {'pvname': pvname, 'mode': SubscriptionMode.parse(modedef)} \
                for pvname, modedef in zip(pvnames, modedefs) ]
        expected = envelope(True, 200, 'ok', res) 
        decodedbody = json_decode(response.body)
        self.assertEqual(decodedbody['status'], expected['status'])
        
        d = dict( zip(pvnames, modedefs) )
        responses = decodedbody['response']
        self.assertEqual( len(responses), len(pvnames) )
        for pvname, pvdict in responses.iteritems():
            self.assertEqual(pvname, pvdict['name'])
            self.assertIn(pvdict['name'], pvnames)
            m = SubscriptionMode.parse( d[pvdict['name']] )
            self.assertEquals(pvdict['mode'], m)
Exemple #2
0
    def test_parse_monitor(self):
        monitor_ok = {"mode": "Monitor", "delta": 1.23, "max_freq": 7, "it's alright": "to have extra fields"}
        monitor_nodelta = {"mode": "Monitor", "max_freq": 7}
        monitor_nofreq = {"mode": "Monitor", "delta": 7.1}
        monitor_empty = {"mode": "Monitor"}

        d = monitor_ok
        m = SubscriptionMode.parse(d)
        self.assertEqual(m.name, d["mode"])
        self.assertEqual(m.delta, d["delta"])
        self.assertEqual(m.max_freq, d["max_freq"])

        d = monitor_nodelta
        m = SubscriptionMode.parse(d)
        self.assertEqual(m.name, d["mode"])
        self.assertEqual(m.delta, 0.0)  # delta defaults to 0.0
        self.assertEqual(m.max_freq, d["max_freq"])

        d = monitor_nofreq
        m = SubscriptionMode.parse(d)
        self.assertEqual(m.name, d["mode"])
        self.assertEqual(m.delta, d["delta"])
        self.assertEqual(m.max_freq, None)  # max_freq defaults to None

        d = monitor_empty
        m = SubscriptionMode.parse(d)
        self.assertEqual(m.name, d["mode"])
        self.assertEqual(m.delta, 0.0)
        self.assertEqual(m.max_freq, None)
Exemple #3
0
    def test_parse_monitor(self):
        monitor_ok = {'mode': 'Monitor', 'delta': 1.23, 'max_freq': 7, "it's alright": "to have extra fields"}
        monitor_nodelta= {'mode': 'Monitor', 'max_freq': 7}
        monitor_nofreq= {'mode': 'Monitor', 'delta': 7.1}
        monitor_empty= {'mode': 'Monitor'}
        
        d = monitor_ok
        m = SubscriptionMode.parse(d)
        self.assertEqual(m.name, d['mode'])
        self.assertEqual(m.delta, d['delta'])
        self.assertEqual(m.max_freq, d['max_freq'])

        d = monitor_nodelta
        m = SubscriptionMode.parse(d)
        self.assertEqual(m.name, d['mode'])
        self.assertEqual(m.delta, 0.0) # delta defaults to 0.0
        self.assertEqual(m.max_freq, d['max_freq'])
        
        d = monitor_nofreq
        m = SubscriptionMode.parse(d)
        self.assertEqual(m.name, d['mode'])
        self.assertEqual(m.delta, d['delta']) 
        self.assertEqual(m.max_freq, None) # max_freq defaults to None

        d = monitor_empty
        m = SubscriptionMode.parse(d)
        self.assertEqual(m.name, d['mode'])
        self.assertEqual(m.delta, 0.0) 
        self.assertEqual(m.max_freq, None)
Exemple #4
0
    def test_subscriptions_put(self):
        # create subscription, check it. Then modify it and recheck
        pvname = "test:long1"
        mode1 = {"mode": "Monitor", "delta": 1.1}
        mode2 = {"mode": "Scan", "period": 1.1}

        reqbody = json_encode({'mode': mode1})
        response = self.fetch('/subscriptions/'+pvname, method='PUT', body=reqbody)
        self.assertEqual(response.code, 200)
        self.assertFalse(response.error)

        response = self.fetch('/subscriptions/'+pvname)
        self.assertEqual(response.code, 200)
        self.assertFalse(response.error)

        res = json_decode(response.body)['response']
        self.assertEqual(pvname, res['name'])
        self.assertEqual(res['mode'], SubscriptionMode.parse(mode1))

        # modify it
        reqbody = json_encode({'mode': mode2})
        response = self.fetch('/subscriptions/'+pvname, method='PUT', body=reqbody)
        self.assertEqual(response.code, 200)
        self.assertFalse(response.error)

        response = self.fetch('/subscriptions/'+pvname)
        self.assertEqual(response.code, 200)
        self.assertFalse(response.error)

        res = json_decode(response.body)['response']
        self.assertEqual(pvname, res['name'])
        self.assertEqual(res['mode'], SubscriptionMode.parse(mode2))
Exemple #5
0
    def test_as_dict(self):
        monitor_ok = {"mode": "Monitor", "delta": 1.23, "max_freq": 7, "it's alright": "to have extra fields"}
        m = SubscriptionMode.parse(monitor_ok)

        m_back = m.as_dict()
        m2 = SubscriptionMode.parse(m_back)

        self.assertEqual(m, m2)
Exemple #6
0
    def test_as_dict(self):
        monitor_ok = {'mode': 'Monitor', 'delta': 1.23, 'max_freq': 7, 
                "it's alright": "to have extra fields"}
        m = SubscriptionMode.parse(monitor_ok)

        m_back = dict(m)
        m2 = SubscriptionMode.parse(m_back)

        self.assertEqual(m, m2)
Exemple #7
0
    def test_parse_scan(self):
        scan_ok = {"mode": "Scan", "period": 3.14}
        scan_empty = {"mode": "Scan", "this": "ain't used"}

        d = scan_ok
        m = SubscriptionMode.parse(d)
        self.assertEqual(m.name, d["mode"])
        self.assertEqual(m.period, d["period"])

        d = scan_empty
        m = SubscriptionMode.parse(d)
        self.assertEqual(m.name, d["mode"])
        self.assertEqual(m.period, 0.0)
Exemple #8
0
    def test_subscriptions_get(self):
        # this first fetch with a trailing slash
        response = self.fetch('/subscriptions/')
        self.assertEqual(response.code, 200, response.error)

        # empty
        expected = envelope(True, 200, 'ok', {})
        decodedbody = json_decode(response.body)
        self.assertEqual(decodedbody, expected)

        # test alphabetical ordering of returned pvs
        pvnames = ("test:long1", "test:double1", "test:double2", "test:long3")
        modedefs = ({"mode": "Monitor", "delta": 1.1},
                    {"mode": "Scan", "period": 0.1},
                    {"mode": "Scan", "period": 0.2},
                    {"mode": "Scan", "period": 0.3}, )
        self._subscribe_to_many(pvnames, modedefs)
        # second fetch WITHOUT a trailing slash
        response = self.fetch('/subscriptions')
        self.assertEqual(response.code, 200)
        self.assertFalse(response.error)

        responses = json_decode(response.body)['response']
        self.assertEqual(len(responses), len(pvnames))

        self.assertItemsEqual( responses.keys(), pvnames )

        for pvname, modedef in zip(pvnames, modedefs):
            self.assertEqual(responses[pvname]['name'], pvname)
            self.assertEqual(responses[pvname]['mode'], SubscriptionMode.parse(modedef))
Exemple #9
0
    def test_subscriptions_delete(self):
        pvnames = ("test:long1", "test:long2")
        modedefs = ({"mode": "Monitor", "delta": 1.1},
                    {"mode": "Scan", "period": 0.1})

        self._subscribe_to_many(pvnames, modedefs)

        response = self.fetch('/subscriptions/')
        self.assertEqual(response.code, 200)
        self.assertFalse(response.error)

        responses = json_decode(response.body)['response']
        self.assertEqual(len(responses), len(pvnames))
        self.assertItemsEqual( responses.keys(), pvnames )

        for pvname, modedef in zip(pvnames, modedefs):
            self.assertEqual(responses[pvname]['name'], pvname)
            self.assertEqual(responses[pvname]['mode'], SubscriptionMode.parse(modedef))

        # delete now
        response = self.fetch('/subscriptions/', method='DELETE')

        # all subscriptions should be gone now
        response = self.fetch('/subscriptions/')
        self.assertEqual(response.code, 200)
        self.assertFalse(response.error)

        responses = json_decode(response.body)['response']
        self.assertEqual(len(responses), 0)
Exemple #10
0
 def _parse(pvname, cols):
     subscribed = cols['subscribed']
     since = cols['since']
     if subscribed:
         modedict = loads(cols['mode'])
         mode = SubscriptionMode.parse(modedict)
         return ArchivedPV(pvname, subscribed, mode, since)
     return ArchivedPV(pvname, subscribed, since=since)
Exemple #11
0
def list_by_mode(modename):
    if not SubscriptionMode.is_registered(modename):
        raise ValueError("'%s' is not a valid mode" % modename)

    expr = pycassa.index.create_index_expression('modename', modename)
    cls = pycassa.index.create_index_clause([expr], count=2**31)
    rowsgen = _cf('pvs').get_indexed_slices(cls)
    return ((k, ArchivedPV(k, **v)) for k,v in rowsgen)
Exemple #12
0
    def test_subscriptions_put(self):
        pvnames = ("test:long1", "test:long2")
        modedefs = ({"mode": "Monitor", "delta": 1.1},
                    {"mode": "Scan", "period": 0.1})

        self._subscribe_to_many(pvnames, modedefs)

        response = self.fetch('/subscriptions/')
        self.assertEqual(response.code, 200)
        self.assertFalse(response.error)

        responses = json_decode(response.body)['response']
        self.assertEqual(len(responses), len(pvnames))
        self.assertItemsEqual( responses.keys(), pvnames )

        for pvname, modedef in zip(pvnames, modedefs):
            self.assertEqual(responses[pvname]['name'], pvname)
            self.assertEqual(responses[pvname]['mode'], SubscriptionMode.parse(modedef))

        # up to here, same as get's test
        
        pvnames = ("test:double1", "test:double2", "test:double3")
        modedefs = ({"mode": "Monitor", "delta": 2.1},
                    {"mode": "Scan", "period": 3.1},
                    {"mode": "Monitor", "period": 4.1},
                    )
        to_sub = [{'pvname': pvname, 'mode': modedef} \
                    for pvname, modedef in zip(pvnames, modedefs)]
        reqbody = json_encode(to_sub)
        response = self.fetch('/subscriptions/', method='PUT', body=reqbody)
        self.assertEqual(response.code, 200)
        self.assertFalse(response.error)

        response = self.fetch('/subscriptions/')
        self.assertEqual(response.code, 200)
        self.assertFalse(response.error)

        responses = json_decode(response.body)['response']
        self.assertEqual(len(responses), len(pvnames))
        self.assertItemsEqual( responses.keys(), pvnames )

        for pvname, modedef in zip(pvnames, modedefs):
            self.assertEqual(responses[pvname]['name'], pvname)
            self.assertEqual(responses[pvname]['mode'], SubscriptionMode.parse(modedef))
Exemple #13
0
    def test_parse_scan(self):
        scan_ok = {'mode': 'Scan', 'period': 3.14}
        scan_empty= {'mode': 'Scan', 'this': "ain't used"}
        
        d = scan_ok
        m = SubscriptionMode.parse(d)
        self.assertEqual(m.name, d['mode'])
        self.assertEqual(m.period, d['period'])

        d = scan_empty
        self.assertRaises(ValueError, SubscriptionMode.parse, d)
Exemple #14
0
def load_config(configstr):
    """ Restore the state defined by the config.
    
        Returns a list with the receipts of the restored subscriptions.
    """
    
    receipts = []
    without_comments = [ line for line in configstr.splitlines() if not line.lstrip().startswith('#') ]
    jsondata = ''.join(without_comments)
    decoded_l = json.loads(jsondata)
    names = []
    modes = []
    for item in decoded_l:
        names.append(item['name'])
        modes.append(SubscriptionMode.parse(item['mode']))
    return msubscribe(names, modes)
Exemple #15
0
    def _subscribe(self):
        body_json = self.request.body
        if not body_json:
            self.fail("Empty request")
        try:
            body = json.loads(body_json)
            # body should of the form
            # [ {pvname: ..., mode: ...}, {pvname: ..., mode: ...}, ... ]
            pvnames = []
            modes = []
            for subscr_dict in body:
                pvnames.append(subscr_dict["pvname"])
                modes.append(SubscriptionMode.parse(subscr_dict["mode"]))

            futures = controller.msubscribe(pvnames, modes)
            results = dict((pvname, f.get(TIMEOUT)) for pvname, f in zip(pvnames, futures))
            self.win(results)
        except Exception as e:
            logger.exception(e)
            self.fail(str(e))
Exemple #16
0
    def test_subscriptions_get(self):
        # non existent
        response = self.fetch('/subscriptions/foobar')
        self.assertEqual(response.code, 200, response.error)
        decodedbody = json_decode(response.body)
        self.assertEqual( decodedbody['status']['code'], 404)
        self.assertFalse( decodedbody['response'] )

        # test alphabetical ordering of returned pvs
        pvnames = ("test:long1", "test:double1", "test:double2", "test:long3")
        modedefs = ({"mode": "Monitor", "delta": 1.1},
                    {"mode": "Scan", "period": 0.1},
                    {"mode": "Scan", "period": 0.2},
                    {"mode": "Scan", "period": 0.3}, )
        self._subscribe_to_many(pvnames, modedefs)

        response = self.fetch('/subscriptions/'+pvnames[0])
        self.assertEqual(response.code, 200)
        self.assertFalse(response.error)

        res = json_decode(response.body)['response']
        self.assertEqual(pvnames[0], res['name'])
        self.assertEqual(res['mode'], SubscriptionMode.parse(modedefs[0]))
Exemple #17
0
    def test_subscriptions_delete(self):
        pvnames = ("test:long1", "test:long2")
        modedefs = ({"mode": "Monitor", "delta": 1.1},
                    {"mode": "Scan", "period": 0.1})

        self._subscribe_to_many(pvnames, modedefs)
 
        response = self.fetch('/subscriptions/'+pvnames[0])
        self.assertEqual(response.code, 200)
        self.assertFalse(response.error)

        res = json_decode(response.body)['response']
        self.assertTrue(res)
        self.assertEqual(pvnames[0], res['name'])
        self.assertEqual(res['mode'], SubscriptionMode.parse(modedefs[0]))

        # delete now
        response = self.fetch('/subscriptions/'+pvnames[0], method='DELETE')

        response = self.fetch('/subscriptions/'+pvnames[0])
        self.assertEqual(response.code, 200)
        self.assertFalse(response.error)

        res = json_decode(response.body)['response']
        self.assertFalse(res)

        # delete non existent
        response = self.fetch('/subscriptions/foobar', method='DELETE')
        # the actual request must succeed at the HTTP level...
        self.assertEqual(response.code, 200)
        self.assertFalse(response.error)

        # ... but fail at OUR level
        body = json_decode(response.body)
        self.assertFalse(body['status']['success'])
        self.assertEqual(body['status']['code'], 404)
        self.assertFalse(body['response'])
Exemple #18
0
    def put(self, pvname):
        # modify if exists, ie, unsubscribe and resubscribe
        apv = controller.get_apv(pvname)
        if apv and apv.subscribed:
            controller.unsubscribe(pvname)

        # at this point, subscribe no matter what: if we were
        # already subscribed, we've just unsubscribed.

        body_json = self.request.body
        body = json.loads(body_json)
        # body should of the form
        # {mode: ...}
        mode = SubscriptionMode.parse(body["mode"])
        try:
            future = controller.subscribe(pvname, mode)
            result = future.get(TIMEOUT)
            if result:
                self.win({"pvname": pvname, "mode": mode})
            else:
                self.fail("Couldn't subscribe", {"pvname": pvname, "mode": mode})
        except Exception as e:
            logger.exception(e)
            self.fail(str(e))