Esempio n. 1
0
File: test.py Progetto: ildarkit/hw3
def has_storage():
    result = False
    try:
        store = Store(connect_timeout=2, attempts=2)
        store.connect()
        result = True
    except redis.ConnectionError:
        pass
    return result
Esempio n. 2
0
File: test.py Progetto: ildarkit/hw3
class StorageTest(unittest.TestCase):
    def setUp(self):
        self.store = Store(connect_timeout=5, attempts=3)

    def test_on_disconnected_store_cache_set_cache_get(self):
        self.store = Store(port=9999, connect_timeout=1, attempts=1)

        key = "uid:c20ad4d76fe97759aa27a0c99bff6710"
        self.store.cache_set(key, -1, 60)
        value = self.store.cache_get(key) or 0
        self.assertEqual(value, 0)

    @cases([{
        'key': "uid:123456",
        'value': 1
    }, {
        'key': "uid:123456",
        'value': -3.7
    }])
    def test_on_connected_store_get_cache_get(self, kwargs):
        self.store.connect()

        key = kwargs['key']
        self.store.cache_set(key, kwargs['value'], 60 * 60)
        value = self.store.cache_get(key) or 0
        self.assertEqual(value, kwargs['value'])

    @cases([{
        'key': "uid:654321",
        'value': 'books'
    }, {
        'key': "uid:654321",
        'value': 'путешествие'
    }])
    def test_on_connected_store_get(self, kwargs):
        self.store.connect()

        key = kwargs['key']
        self.store.redis.delete(key)
        self.store.redis.rpush(key, kwargs['value'])
        value = self.store.get(key) or None
        self.assertEqual(value, [kwargs['value']])
Esempio n. 3
0
class TestStore(unittest.TestCase):
    def setUp(self):
        self.redis_storage = RedisStore(host=os.getenv('REDIS_HOST',
                                                       'localhost'),
                                        port=os.getenv('REDIS_PORT', 6379))
        self.store = Store(self.redis_storage)
        self.store.connect()
        self.key = 'sample_key'
        self.value = 'sample_value'

    def test_connect_to_store(self):
        self.assertTrue(self.store.ping())

    def test_connect_to_redis(self):
        self.assertTrue(self.redis_storage.ping())

    def test_redis_set_value(self):
        key = self.key + '_redis'
        self.assertTrue(self.redis_storage.set(key, self.value, 30))
        self.assertEqual(self.store.cache_get(key), self.value)

    def test_redis_connection_error(self):
        self.redis_storage.client.disconnect()
        self.assertRaises(ConnectionError, self.store.cache_get, self.key)

    def test_cache_set_value(self):
        key = self.key + '_cache'
        self.assertTrue(self.store.cache_set(key, self.value, 30))
        self.assertEqual(self.store.cache_get(key), self.value)

    def test_cache_connection_error(self):
        self.redis_storage.client.disconnect()

        self.assertRaises(ConnectionError, self.store.cache_get, self.key)
        self.assertRaises(ConnectionError, self.store.cache_set, self.key,
                          self.value)

    def test_store_set_value(self):
        key = self.key + '_store'
        self.assertTrue(self.store.set(key, self.value, 30))
        self.assertEqual(self.store.get(key), self.value)
Esempio n. 4
0
class Session(IFilter):
    MAGGC = '^&*)('
    MAGIC = ')(*&^'

    MAGGCL = len(MAGGC)
    MAGICL = len(MAGIC)

    S_None      = 0
    S_Prefix    = 1


    PT_PURIFY   = 1
    PT_PROTO    = 2

    def __init__(self, sock, address):
        IFilter.__init__(self)
        self.logger = logging.getLogger("Session")
        self.sock = sock
        self.addr = address
        self.buffer = ""
        self.status = Session.S_None

        if not self.init_db():           raise "redis connection failed."
        if not self.init_manager():      raise "install chain manager failed."

    def init_db(self):
        self.store = Store("10.10.69.54")
        return self.store.connect()

    def getStore(self):
        if not hasattr(self, store):
            return None
        return self.store

    def init_manager(self):
        self.cm = ChainManager(self)
        return self.cm.install()

    def process(self, data):
        print data
        print "==============================="
        '''
            status should be 0 =====> 1 by find MAGGC
                             1 =====> 0 by find MAGIC and buffer length less then 1M, we don't support 1M purify maybe.
                             
                             let me take consider about this state machine.eh? 
        '''
        while data and len(data) > 0:
            if Session.S_None == self.status:
                idx = data.find(Session.MAGGC)
                if idx == -1:
                    break
                else:
                    self.status = Session.S_Prefix
                    data = data[idx + Session.MAGGCL:]

            if len(data) == 0: break

            if Session.S_Prefix == self.status:
                idx = data.find(Session.MAGIC)
                if -1 != idx:
                    offset = 0
                    ggc = data.find(Session.MAGGC)
                    self.status = Session.S_None
                    if ggc != -1 and ggc < idx:
                        self.buffer = ''
                        offset = gcc + Session.MAGGCL

                    idx = idx - len(data) 
                    rtn = self.buffer + data[offset:idx]

                    # really output.........
                    self.logger.debug(rtn)
                    self.dispatch(rtn)

                    data = data[idx + len(Session.MAGIC):] #jump magic chars
                    self.buffer = ''
                else:
                    ggc = string.rfind(data, Session.MAGGC)
                    if ggc != -1:
                        self.buffer = data[(gcc + Session.MAGGCL):]
                    else:
                        self.buffer += data
                    break
            else:
                 self.logger.error("impossible state of Session.")

    def dispatch(self, packet):
        '''
            dispatch the string packet to specified Chain in our system
        '''
        if not packet:
            return

        fields = packet.strip().split('$$')
        size = len(fields)
        if size < 2:
            return

        protocol_type = int(fields[0])
        if protocol_type < 0 or protocol_type > 9999:
            return

        if protocol_type == Session.PT_PURIFY:
            # simple dispatch to readability
            print "====== simply dispatch to readability. ======"
            #check the count of parameters
            if size < 3:
                self.logger.error("protocol type (%d), parameter count error:(%d)", protocol_type, size)
                return

            if not self.cm.determin(fields[1], content = fields[2]):
                self.logger.error("determin domain:(%s) error: [%s]", fields[1], fields[2])

        elif protocol_type == Session.PT_PROTO:
            self.logger.debug("request is a protocol. type:%d command:%s" % (protocol_type, fields[1]))
            return


    def onProcess(self, request):
        IFilter.onProcess(self, request)
        self.logger.debug("SessionFilter onProcess...")
        try:
            if request.error:
                self.sock.send('1$$Error:%s' %(request.error, ))
            else:
                if not request.result: request.result = ''

                self.sock.send('0$$%s' % (request.result, ))
        except Exception, e:
            self.logger.error("Error: %s " % (e, ))