def test_add_servicer_with_valid_instance(self):
     """
     Ensure servicers are added to the server properly.
     """
     with mock.patch('test.helloworld_pb2') as _hw:
         server = Petty()
         server.add_servicer(_hw, Greeter())
         _hw.add_GreeterServicer_to_server.assert_called_once()
Exemple #2
0
 def test_stop_when_not_running(self):
     """
     Ensure stop skips running stop when the server is not running.
     """
     with mock.patch('grpc.server') as _grpc_server:
         server = Petty()
         server.stop()
         _grpc_server().stop.assert_not_called()
Exemple #3
0
 def test_add_servicer_with_valid_instance(self):
     """
     Ensure servicers are added to the server properly.
     """
     with mock.patch('test.helloworld_pb2') as _hw:
         server = Petty()
         server.add_servicer(_hw, Greeter())
         _hw.add_GreeterServicer_to_server.assert_called_once()
 def test_stop_when_not_running(self):
     """
     Ensure stop skips running stop when the server is not running.
     """
     with mock.patch('grpc.server') as _grpc_server:
         server = Petty()
         server.stop()
         _grpc_server().stop.assert_not_called()
Exemple #5
0
 def test_stop_when_running(self):
     """
     Ensure stop does stop the server when it is running.
     """
     with mock.patch('grpc.server') as _grpc_server:
         server = Petty()
         server.start()
         server.stop()
         _grpc_server().stop.assert_called_once()
Exemple #6
0
 def test_start_with_defaults(self):
     """
     Ensure start infact does start the server with defaults without looping.
     """
     with mock.patch('grpc.server') as _grpc_server:
         server = Petty()
         server.start()
         _grpc_server().add_insecure_port.assert_called_once_with(
             '127.0.0.1:8080')
 def test_start_with_defaults(self):
     """
     Ensure start infact does start the server with defaults without looping.
     """
     with mock.patch('grpc.server') as _grpc_server:
         server = Petty()
         server.start()
         _grpc_server().add_insecure_port.assert_called_once_with(
             '127.0.0.1:8080')
Exemple #8
0
 def test_run_forever_without_start(self):
     """
     Ensure run executes start if the server has not already been started.
     """
     with mock.patch('grpc.server') as _grpc_server, \
             mock.patch('time.sleep') as _sleep:
         # Raise so that we don't loop forever
         _sleep.side_effect = KeyboardInterrupt
         server = Petty()
         server.run_forever()
         _grpc_server().start.assert_called_once()
         _sleep.assert_called_once()
 def test_run_forever_without_start(self):
     """
     Ensure run executes start if the server has not already been started.
     """
     with mock.patch('grpc.server') as _grpc_server, \
             mock.patch('time.sleep') as _sleep:
         # Raise so that we don't loop forever
         _sleep.side_effect = KeyboardInterrupt
         server = Petty()
         server.run_forever()
         _grpc_server().start.assert_called_once()
         _sleep.assert_called_once()
Exemple #10
0
 def test_run_forever_skips_start_if_already_running(self):
     """
     Ensure run executes start if the server has not already been started.
     """
     with mock.patch('grpc.server') as _grpc_server, \
             mock.patch('time.sleep') as _sleep:
         # Raise so that we don't loop forever
         _sleep.side_effect = KeyboardInterrupt
         server = Petty()
         server.start()
         self.assertEquals(1, _grpc_server().start.call_count)
         server.run_forever()
         # We should still have only called start once
         self.assertEquals(1, _grpc_server().start.call_count)
         _sleep.assert_called_once()
Exemple #11
0
 def test__init_logger_no_config(self):
     """
     Ensure the server logger is created properly without a config.
     """
     with mock.patch('grpc.server'):
         server = Petty()
         self.assertEquals('petty', server.logger.name)
         self.assertFalse(server.logger.propagate)
Exemple #12
0
 def test_start_with_addresses_and_ports(self):
     """
     Ensure start does start the server with input addresses/ports.
     """
     with mock.patch('grpc.server') as _grpc_server:
         for kwargs in (
             {'host': '127.0.0.2', 'port': 8000},
             {'port': 8000},
             {'host': '127.0.0.2'},
         ):
             _grpc_server().add_insecure_port.reset_mock()
             expected_host = kwargs.get('host', '127.0.0.1')
             expected_port = kwargs.get('port', 8080)
             server = Petty()
             server.start(**kwargs)
             _grpc_server().add_insecure_port.assert_called_once_with(
                 '{}:{}'.format(expected_host, expected_port))
Exemple #13
0
 def test__init_logger_with_missing_config(self):
     """
     Ensure the server logger is created properly with a missing config.
     """
     with mock.patch('grpc.server'), \
             mock.patch('yaml.safe_load') as _yaml_safe_load:
         server = Petty(logger_conf_file='./doesnotexist')
         self.assertEquals('petty', server.logger.name)
         self.assertFalse(server.logger.propagate)
         _yaml_safe_load.assert_not_called()
Exemple #14
0
 def test__init_logger_no_yaml(self):
     """
     Ensure the server logger is created properly without a config.
     """
     with mock.patch('grpc.server'), \
             mock.patch('yaml.safe_load') as _yaml_safe_load:
         _yaml_safe_load.side_effect = ImportError
         server = Petty()
         self.assertEquals('petty', server.logger.name)
         self.assertFalse(server.logger.propagate)
Exemple #15
0
    def test__init_logger_with_invalid_config(self):
        """
        Ensure the server logger is created properly with an invalid config.
        """
        with mock.patch('grpc.server'), \
                mock.patch('yaml.safe_load') as _yaml_safe_load:

            _yaml_safe_load.return_value = '!!--//s'
            server = Petty(logger_conf_file='../conf/logging.yaml')
            self.assertEquals('petty', server.logger.name)
            self.assertFalse(server.logger.propagate)
            _yaml_safe_load.assert_called_once()
Exemple #16
0
 def test_stop_when_running(self):
     """
     Ensure stop does stop the server when it is running.
     """
     with mock.patch('grpc.server') as _grpc_server:
         server = Petty()
         server.start()
         server.stop()
         _grpc_server().stop.assert_called_once()
Exemple #17
0
 def test_start_with_addresses_and_ports(self):
     """
     Ensure start does start the server with input addresses/ports.
     """
     with mock.patch('grpc.server') as _grpc_server:
         for kwargs in (
             {
                 'host': '127.0.0.2',
                 'port': 8000
             },
             {
                 'port': 8000
             },
             {
                 'host': '127.0.0.2'
             },
         ):
             _grpc_server().add_insecure_port.reset_mock()
             expected_host = kwargs.get('host', '127.0.0.1')
             expected_port = kwargs.get('port', 8080)
             server = Petty()
             server.start(**kwargs)
             _grpc_server().add_insecure_port.assert_called_once_with(
                 '{}:{}'.format(expected_host, expected_port))
Exemple #18
0
 def test_run_forever_skips_start_if_already_running(self):
     """
     Ensure run executes start if the server has not already been started.
     """
     with mock.patch('grpc.server') as _grpc_server, \
             mock.patch('time.sleep') as _sleep:
         # Raise so that we don't loop forever
         _sleep.side_effect = KeyboardInterrupt
         server = Petty()
         server.start()
         self.assertEquals(1, _grpc_server().start.call_count)
         server.run_forever()
         # We should still have only called start once
         self.assertEquals(1, _grpc_server().start.call_count)
         _sleep.assert_called_once()
Exemple #19
0
def main():
    s = Petty(logger_conf_file='conf/logging.yaml')
    s.add_servicer(helloworld_pb2, Greeter())
    s.run_forever()
Exemple #20
0
def main():
    s = Petty(logger_conf_file='conf/logging.yaml')
    s.add_servicer(helloworld_pb2, Greeter())
    s.run_forever()