Permalink
Browse files

monitor command, transaction tests

  • Loading branch information...
gleicon committed Jul 21, 2012
1 parent 2d7a4ab commit 73175c122d42aebd5a609f398ef9dcf2409b2e39
Showing with 120 additions and 3 deletions.
  1. +3 −1 .gitignore
  2. +49 −0 examples/monitor.py
  3. +1 −1 setup.py
  4. +41 −0 tests/test_transactions.py
  5. +26 −1 txredisapi.py
View
@@ -1,5 +1,7 @@
*.swp
*.pyc
build
-_trial_temp/
+_trial_temp
tmp
+dist
+txredisapi.egg-info
View
@@ -0,0 +1,49 @@
+#!/usr/bin/env twistd -ny
+# coding: utf-8
+# Copyright 2012 Gleicon Moraes/Alexandre Fiori
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# run: twistd -ny monitor.tac
+# it takes the full connection so no extra commands can be issued
+
+
+import txredisapi
+
+from twisted.application import internet
+from twisted.application import service
+
+
+class myMonitor(txredisapi.MonitorProtocol):
+ def connectionMade(self):
+ print "waiting for monitor data"
+ print "use the redis client to send commands in another terminal"
+ self.monitor()
+
+ def messageReceived(self, message):
+ print ">> %s" % message
+
+ def connectionLost(self, reason):
+ print "lost connection:", reason
+
+
+class myFactory(txredisapi.MonitorFactory):
+ # also a wapper for the ReconnectingClientFactory
+ maxDelay = 120
+ continueTrying = True
+ protocol = myMonitor
+
+
+application = service.Application("monitor")
+srv = internet.TCPClient("127.0.0.1", 6379, myFactory())
+srv.setServiceParent(application)
View
@@ -18,7 +18,7 @@
setuptools.setup(
name="txredisapi",
- version="0.6",
+ version="0.7",
py_modules=["txredisapi"],
install_requires=["twisted"],
author="Alexandre Fiori",
View
@@ -0,0 +1,41 @@
+# coding: utf-8
+# Copyright 2009 Alexandre Fiori
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import txredisapi
+from twisted.trial import unittest
+from twisted.internet import defer, reactor
+from twisted.python import log
+import sys
+
+log.startLogging(sys.stdout)
+
+redis_host="localhost"
+redis_port=6379
+
+class TestRedisConnections(unittest.TestCase):
+ @defer.inlineCallbacks
+ def testRedisConnection(self):
+ rapi = yield txredisapi.Connection(redis_host, redis_port)
+
+ # test set() operation
+ transaction = yield rapi.multi()
+ print "oi"
+ for key, value in (("txredisapi:test_transaction", "foo"), ("txredisapi:test_transaction", "bar")):
+ yield transaction.set(key, value)
+ yield transaction.commit()
+ result = yield rapi.get("txredisapi:test_transaction")
+ self.assertEqual(result, "bar")
+
+ yield rapi.disconnect()
View
@@ -1224,8 +1224,29 @@ def info(self):
"""
return self.execute_command("INFO")
- # monitor and slaveof should are missing
+ # slaveof is missing
+class MonitorProtocol(RedisProtocol):
+ """
+ monitor command has the same behavior as subscribe: takes the whole connection.
+ take care with the performance impact:
+ http://redis.io/commands/monitor
+ """
+
+ def connectionLost(self, why):
+ pass
+
+ def messageReceived(self, message):
+ pass
+
+ def replyReceived(self, reply):
+ self.messageReceived(reply)
+
+ def monitor(self):
+ return self.execute_command("MONITOR")
+
+ def stop(self):
+ self.transport.loseConnection()
class SubscriberProtocol(RedisProtocol):
def connectionLost(self, why):
@@ -1267,6 +1288,10 @@ class SubscriberFactory(protocol.ReconnectingClientFactory):
continueTrying = True
protocol = SubscriberProtocol
+class MonitorFactory(protocol.ReconnectingClientFactory):
+ maxDelay = 120
+ continueTrying = True
+ protocol = MonitorProtocol
class ConnectionHandler(object):
def __init__(self, factory):

0 comments on commit 73175c1

Please sign in to comment.