1. youngking
  2. starry

Commits

youngking  committed bf2c5ab

add test to starry

  • Participants
  • Parent commits d156ce5
  • Branches master

Comments (0)

Files changed (18)

File examples/proto/__init__.py

  • Ignore whitespace
Empty file removed.

File examples/proto/client.py

  • Ignore whitespace
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
-
-from protorpc import remote
-
-from pb2 import User,Users,Request
-
-from starry.transport  import TcpTransport
-
-address = 'tcp://0.0.0.0:5000'
-
-class TestService(remote.Service):
-
-    @remote.method(Request,User)
-    def get_member(cls, req):
-        raise NotImplementedError('Method get_member is not implemented')
-
-testservice = TestService.Stub(TcpTransport(address,'test_service'))
-print testservice.all_remote_methods()
-req = Request()
-req.value = 128
-member =  testservice.get_member(req)
-print member.name
-

File examples/proto/client2.py

  • Ignore whitespace
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
-import time
-
-from protorpc import remote
-
-from pb2 import User,Users,Request
-
-from starry.client import RPCClient
-from starry.exceptions import MethodNotFound
-from starry.transport  import TcpTransport
-
-address = 'tcp://0.0.0.0:5000'
-
-
-client = TcpTransport(address,'test_service')
-#print testservice.all_remote_methods()
-req = Request()
-req.value = 128
-result1 = client.call_remote('get_member', req, User)
-print result1.state
-
-try:
-    member = result1.response
-    print member.name
-    print result1.state
-except remote.NetworkError as e:
-    print e
-
-
-req = Request()
-req.value = 4
-result2 = client.call_remote('get_member', req, User, timeout=5)
-print result2.state
-
-member = result2.response
-print member.name
-print result2.state
-

File examples/proto/config.yaml

  • Ignore whitespace
-production:
-    debug: false
-    sqlStore:
-        default:
-            engine: mysql
-            host: localhost
-            database: zhihu
-            username: zhihu
-            password: abc112233
-        slave:
-            engine: mysql
-            host: localhost
-            database: zhihu
-            username: zhihu
-            password: abc112233
-    redisStore:
-        web:
-            host:localhost
-            port:6379
-            db:0
-    redisclusterStore:
-        timelines:
-            - 'name': 'server1'
-              'host': '127.0.0.1'
-              'port': 10000
-              'db': 0 
-            - 'name': 'server2'
-              'host': '127.0.0.1'
-              'port': 11000
-              'db': 0 
-
-testing:
-    __extends__: production
-    templates: test
-development:
-    __extends__: production
-    debug: true

File examples/proto/funk_tests/pb2.py

  • Ignore whitespace
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
-from protorpc import messages
-from protorpc import remote
-
-package = 'proto'
-
-class User(messages.Message):
-    id   = messages.IntegerField(1)
-    name = messages.StringField(2 ,required=True)
-    
-    class Status(messages.Enum):
-        NORMAL = 0
-        HEAVY  = 1
-        MUTE   = 3
-        HANG   = 7
-
-    status = messages.EnumField(Status, 3, required=True)
-
-class Users(messages.Message):
-
-    users = messages.MessageField(User, 1, repeated=True)
-
-class Request(messages.Message):
-    value = messages.IntegerField(1)
-
-

File examples/proto/funk_tests/proto.conf

  • Ignore whitespace
-[main]
-title = Load test for proto
-description = simple load test for proto
-url = http://localhost
-address = tcp://10.13.37.136:5000
-
-
-[test_transport]
-description=Access %(nb_time)s times the main address
-nb_time=20
-
-[ftest]
-
-# log_to destination =
-# console - to the screen
-# file - to a file
-log_to = console file
-
-# log_path = path and file name to store log file
-log_path = proto-test.log
-
-# result_path = path to store the xml result file
-result_path = proto-test.xml
-
-# ok_codes = list of successfull HTTP response code
-#ok_codes =  200:301:302
-
-# sleeptime_min = minimum amount of time in seconds to sleep between requests
-#                 to the host
-sleep_time_min = 0
-
-# sleeptime_max = maximum amount of time in seconds to sleep between requests
-#                 to the host
-sleep_time_max = 0
-
-
-[bench]
-
-# cycles = list of cycles with their number of concurrent users
-cycles = 1:5:10:20:30
-
-# duration = duration of a cycle in seconds
-duration = 15
-
-# startup_delay = time to wait between starting-up threads in seconds
-startup_delay = 0.1
-
-# sleep_time = time to wait between test in seconds
-sleep_time = 0.5
-
-# cycle_time = time to wait between cycle in seconds
-cycle_time = 20
-
-# same keys than in [ftest] section
-log_to = file
-log_path = proto-bench.log
-result_path = proto-bench.xml
-sleep_time_min = .1
-sleep_time_max = .2

File examples/proto/funk_tests/test_proto.py

  • Ignore whitespace
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
-import time
-from funkload.FunkLoadTestCase import FunkLoadTestCase
-
-
-class Proto(FunkLoadTestCase):
-    """This test use a configuration file Proto.conf."""
-
-    def setUp(self):
-        self.address = self.conf_get('main','address')
-        from starry.transport  import TcpTransport
-        self.client = TcpTransport(self.address,'test_service')
-
-    def test_transport(self):
-        from pb2 import User,Users,Request
-        req = Request()
-        req.value = 83
-        nb_time = self.conf_getInt('test_transport', 'nb_time')
-        for i in range(nb_time):
-            time.sleep(0.01)
-            result = self.client.call_remote('get_member', req, User)
-            member = result.response
-            self.assertEqual(member.name,u'黄海均')

File examples/proto/manager.py

  • Ignore whitespace
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
-import os
-import time
-
-from protorpc import remote
-from starry.datastore import sql
-
-from pb2 import User,Users,Request
-
-package = 'starrry'
-
-class TestService(remote.Service):
-
-    @remote.method(Request,User)
-    def get_member(cls, req):
-        _sql = "SELECT * FROM member WHERE id={0}".format(req.value)
-        member = sql.connect('default').execute(_sql).first()
-        user = User()
-        user.id = member.id
-        user.name = member.fullname
-        user.status = User.Status.lookup_by_number(member.status)
-        return user
-
-from starry.app import Application
-from starry.server import RPCServer
-
-project_path = os.path.abspath(os.path.dirname(__file__))
-app = Application(project_path)
-app.load_config(os.path.join(project_path,'config.yaml'))
-
-rpcserver = RPCServer('testserver','tcp://0.0.0.0:5000')
-test_service = TestService()
-rpcserver.register_service(test_service,'test_service')
-
-app.add_server(rpcserver)
-
-if __name__ == '__main__':
-    app.run()
-    #print test_service.definition_name()
-    ##print test_service.all_remote_methods()
-    #req = Request()
-    #req.value = 4
-    #with app.request_context():
-    #    user = test_service.get_member(req)
-    #    print user.name
-    #    print user.status.name  == 'NORMAL'
-    #    print user.status.number  == 0

File examples/proto/pb2.py

  • Ignore whitespace
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
-from protorpc import messages
-from protorpc import remote
-
-package = 'proto'
-
-class User(messages.Message):
-    id   = messages.IntegerField(1)
-    name = messages.StringField(2 ,required=True)
-    
-    class Status(messages.Enum):
-        NORMAL = 0
-        HEAVY  = 1
-        MUTE   = 3
-        HANG   = 7
-
-    status = messages.EnumField(Status, 3, required=True)
-
-class Users(messages.Message):
-
-    users = messages.MessageField(User, 1, repeated=True)
-
-class Request(messages.Message):
-    value = messages.IntegerField(1)
-
-

File examples/simple/__init__.py

  • Ignore whitespace
Empty file removed.

File examples/simple/client.py

  • Ignore whitespace
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
-
-from starry.client import RPCClient
-from starry.exceptions import MethodNotFound
-from custom_exceptions import ExampleError
-
-address = 'tcp://0.0.0.0:5000'
-
-client = RPCClient('testclient',address)
-#print client.member_service.model.get_member(4)
-print client.test.incr(3)
-print client.test.long_incr(3,rpc_timeout=3000)
-print client.Test2.decr(3)
-print client.test.get_member(4)
-try:
-    print client.test.decr(3)
-except MethodNotFound as e:
-    print e.code , e.msg
-try:
-    print client.test.error_method('haha')
-except ExampleError as e:
-    print e.code , e.msg
-
-

File examples/simple/config.yaml

  • Ignore whitespace
-production:
-    debug: false
-    sqlStore:
-        default:
-            engine: mysql
-            host: localhost
-            database: zhihu
-            username: zhihu
-            password: abc112233
-        slave:
-            engine: mysql
-            host: localhost
-            database: zhihu
-            username: zhihu
-            password: abc112233
-    redisStore:
-        web:
-            host:localhost
-            port:6379
-            db:0
-    redisclusterStore:
-        timelines:
-            - 'name': 'server1'
-              'host': '127.0.0.1'
-              'port': 10000
-              'db': 0 
-            - 'name': 'server2'
-              'host': '127.0.0.1'
-              'port': 11000
-              'db': 0 
-
-testing:
-    __extends__: production
-    templates: test
-development:
-    __extends__: production
-    debug: true

File examples/simple/custom_exceptions.py

  • Ignore whitespace
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
-from starry.exceptions import CustomError
-
-class ExampleError(CustomError):
-
-    code = 2
-    text_message = 'example error'

File examples/simple/manager.py

  • Ignore whitespace
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
-import os
-import time
-import sys
-from custom_exceptions import ExampleError
-from starry.datastore import sql
-
-class Test(object):
-
-    def __init__(self):
-        self.count = 0
-
-    def incr(self, num):
-        self.count += num
-        return self.count
-
-    def long_incr(self,num):
-        self.count += num
-        time.sleep(1)
-        return self.count
-
-    def error_method(self,msg):
-        raise ExampleError(msg)
-
-    def get_member(self,member_id):
-        _sql = "SELECT * FROM member WHERE id={0}".format(member_id)
-        return sql.connect('default').execute(_sql).first()
-
-class Test2(object):
-
-    def __init__(self):
-        self.count = 0
-
-    def decr(self, num):
-        self.count -= num
-        return self.count
-
-from starry.app import Application
-from starry.server import RPCServer
-
-rpcserver = RPCServer('testserver','tcp://0.0.0.0:5000')
-rpcserver.register_class(Test(),'test')
-rpcserver.register_class(Test2())
-
-project_path = os.path.abspath(os.path.dirname(__file__))
-app = Application(project_path)
-app.load_config(os.path.join(project_path,'config.yaml'))
-app.add_server(rpcserver)
-print app.settings
-app.run()

File requirements.txt

View file
  • Ignore whitespace
+nose
+flexmock

File setup.py

View file
  • Ignore whitespace
     package_dir = {'': '.'},
     include_package_data = True, 
     zip_safe=False,
+    test_suite='nose.collector',
+    tests_require=['Nose','mox'],
     install_requires=install_requires,
     dependency_links = [
         'https://github.com/youngking/python-protorpc/zipball/master#egg=python-protorpc-0.2',

File starry/transport.py

View file
  • Ignore whitespace
 
 logger = logging.getLogger('starry.transport')
 
+
 class ZmqRequest(object):
 
     ctxs = {}
         self.__service_name = service_name
         self.socket = socket
         self.poller = zmq.Poller()
-        self.poller.register(self.socket, zmq.POLLIN|zmq.POLLOUT)
+        self.poller.register(self.socket, zmq.POLLIN | zmq.POLLOUT)
 
         self._start_request(encoded_request)
 
         # inlitlize zmq
 
     def _start_request(self, encoded_request):
-        socks = dict(self.poller.poll(self._transport.timeout*1000)) # this shouldn't be timeout
-        assert socks.has_key(self.socket)
+        socks = dict(self.poller.poll(self._transport.timeout \
+                * 1000))  # this shouldn't be timeout
+        assert self.socket in socks
         assert socks[self.socket] == zmq.POLLOUT
         self.socket.send(encoded_request)
 
-
     def get_response(self):
         try:
             # receiving
-            socks = dict(self.poller.poll(self._transport.timeout*1000))
-            assert socks.has_key(self.socket)
+            socks = dict(self.poller.poll(self._transport.timeout * 1000))
+            assert self.socket in socks
             assert socks[self.socket] == zmq.POLLIN
             reply = self.socket.recv(zmq.NOBLOCK)
-            resp = self._transport.protocol.decode_message(Response,reply)
+            resp = self._transport.protocol.decode_message(Response, reply)
             if resp.response:
-                return resp.response,None
+                return resp.response, None
             else:
                 return None, resp.status
 
         except AssertionError as e:
             self.socket.setsockopt(zmq.LINGER, 0)
             self.socket.close()
-            return None, remote.RpcStatus(state=remote.RpcState.NETWORK_ERROR,
-                    error_message='Network Error ({0}) : {1}'.format(self.__service_name, e.message))
+            return None, remote.RpcStatus(
+                    state=remote.RpcState.NETWORK_ERROR,
+                    error_message='Network Error ({0}) : {1}'.format(\
+                            self.__service_name, e.message))
         except Exception as e:
-            return None, remote.RpcStatus(state=remote.RpcState.SERVER_ERROR,
-                    error_message='Server Error ({0}) : {1}'.format(self.__service_name, e.message) )
+            return None, remote.RpcStatus(
+                    state=remote.RpcState.SERVER_ERROR,
+                    error_message='Server Error ({0}) : {1}'.format(\
+                            self.__service_name, e.message))
+
 
 class TcpTransport(Transport):
 
-    def __init__(self, client_id, address, service_name, timeout=3, protocol=protobuf):
+    def __init__(self, client_id, address, service_name,\
+            timeout=3, protocol=protobuf):
         super(TcpTransport, self).__init__(protocol=protocol)
         self.client_id = client_id
         self.address = address
         req.method_name = method_name
         req.request = self.protocol.encode_message(request)
         tcp_request = self.__request_type(
-                                            transport=self,
-                                            service_name = self.__service_name,
-                                            encoded_request= self.protocol.encode_message(req),
-                                        )
+                            transport=self,
+                            service_name=self.__service_name,
+                            encoded_request=self.protocol.encode_message(req),
+                            )
         rpc = Rpc(request)
 
         def wait_impl():

File test/test_protobuf.py

View file
  • Ignore whitespace
+#!/usr/bin/env python
+import unittest
+from flexmock import flexmock
+
+from protorpc import messages
+from protorpc import remote
+from protorpc import transport
+
+
+class Request(messages.Message):
+    """Test request message."""
+    value = messages.StringField(1)
+
+
+class Response(messages.Message):
+    """Test response message."""
+    value = messages.StringField(1)
+
+
+class SimpleResponse(messages.Message):
+    """Simple response message type used for tests."""
+
+
+class SomeService(remote.Service):
+
+    @remote.method(Request, Response)
+    def one_method(self, request):
+        response = Response()
+        response.value = request.value
+        return response
+
+    @remote.method(Request, SimpleResponse)
+    def two_method(self, request):
+        response = SimpleResponse()
+        return response
+
+
+class InterfaceTest(unittest.TestCase):
+
+    def test_method(self):
+        self.assertEquals(Request, \
+                SomeService.one_method.remote.request_type)
+        self.assertEquals(Response, \
+                SomeService.one_method.remote.response_type)
+
+    def test_message(self):
+        request = Request()
+        service = SomeService()
+        self.assertEquals(SimpleResponse(), service.two_method(request))
+
+
+class ClientTest(unittest.TestCase):
+
+    def setUp(self):
+        self.transport = flexmock()
+
+    def test_remote_methods(self):
+        stub = SomeService.Stub(self.transport)
+        self.assertEquals(stub.all_remote_methods(), \
+                SomeService.all_remote_methods())
+
+    def test_with_request(self):
+        stub = SomeService.Stub(self.transport)
+        request = Request()
+        request.value = "hello"
+        response = Response()
+
+        rpc = transport.Rpc(request)
+        rpc.set_response(response)
+        self.transport.should_receive('send_rpc').with_args(
+                SomeService.one_method.remote, request).and_return(rpc)
+
+        self.assertEquals(Response(), stub.one_method(request))
+        self.assertEquals(Response(), stub.one_method(value="hello"))