Exemplo n.º 1
0
class MLLPClientTest(unittest.TestCase):
    def setUp(self):
        # use a mock version of socket
        self.socket_patch = patch('hl7.client.socket.socket')
        self.mock_socket = self.socket_patch.start()

        self.client = MLLPClient('localhost', 6666)

    def tearDown(self):
        # unpatch socket
        self.socket_patch.stop()

    def test_connect(self):
        self.mock_socket.assert_called_once_with(socket.AF_INET,
                                                 socket.SOCK_STREAM)
        self.client.socket.connect.assert_called_once_with(('localhost', 6666))

    def test_close(self):
        self.client.close()
        self.client.socket.close.assert_called_once_with()

    def test_send(self):
        self.client.socket.recv.return_value = 'thanks'

        result = self.client.send('foobar\n')
        self.assertEqual(result, 'thanks')

        self.client.socket.send.assert_called_once_with('foobar\n')
        self.client.socket.recv.assert_called_once_with(4096)

    def test_send_message(self):
        self.client.socket.recv.return_value = 'thanks'

        result = self.client.send_message('foobar')
        self.assertEqual(result, 'thanks')

        self.client.socket.send.assert_called_once_with('\x0bfoobar\x1c\x0d')

    def test_context_manager(self):
        with MLLPClient('localhost', 6666) as client:
            client.send('hello world')

        self.client.socket.send.assert_called_once_with('hello world')
        self.client.socket.close.assert_called_once_with()

    def test_context_manager_exception(self):
        try:
            with MLLPClient('localhost', 6666):
                raise Exception()
            self.fail()
        except:
            # expected
            pass

        # socket.close should be called via the with statement
        self.client.socket.close.assert_called_once_with()
Exemplo n.º 2
0
class MLLPClientTest(unittest.TestCase):
    def setUp(self):
        # use a mock version of socket
        self.socket_patch = patch('hl7.client.socket.socket')
        self.mock_socket = self.socket_patch.start()

        self.client = MLLPClient('localhost', 6666)

    def tearDown(self):
        # unpatch socket
        self.socket_patch.stop()

    def test_connect(self):
        self.mock_socket.assert_called_once_with(socket.AF_INET,
                                                 socket.SOCK_STREAM)
        self.client.socket.connect.assert_called_once_with(('localhost', 6666))

    def test_close(self):
        self.client.close()
        self.client.socket.close.assert_called_once_with()

    def test_send(self):
        self.client.socket.recv.return_value = 'thanks'

        result = self.client.send('foobar\n')
        self.assertEqual(result, 'thanks')

        self.client.socket.send.assert_called_once_with('foobar\n')
        self.client.socket.recv.assert_called_once_with(4096)

    def test_send_message(self):
        self.client.socket.recv.return_value = 'thanks'

        result = self.client.send_message('foobar')
        self.assertEqual(result, 'thanks')

        self.client.socket.send.assert_called_once_with('\x0bfoobar\x1c\x0d')

    def test_context_manager(self):
        with MLLPClient('localhost', 6666) as client:
            client.send('hello world')

        self.client.socket.send.assert_called_once_with('hello world')
        self.client.socket.close.assert_called_once_with()

    def test_context_manager_exception(self):
        try:
            with MLLPClient('localhost', 6666):
                raise Exception()
            self.fail()
        except:
            # expected
            pass

        # socket.close should be called via the with statement
        self.client.socket.close.assert_called_once_with()
Exemplo n.º 3
0
def send_message(server, port):
    client = MLLPClient(server, port)

    f = open('test.dat', 'r')
    ff = f.read()

    messages = hl7.parse_batch(ff)

    for message in messages:

        client.send_message(str(message))
Exemplo n.º 4
0
    def test_context_manager_exception(self):
        with self.assertRaises(Exception):
            with MLLPClient("localhost", 6666):
                raise Exception()

        # socket.close should be called via the with statement
        self.client.socket.close.assert_called_once_with()
Exemplo n.º 5
0
    def test_context_manager_exception(self):
        try:
            with MLLPClient('localhost', 6666):
                raise Exception()
            self.fail()
        except:
            # expected
            pass

        # socket.close should be called via the with statement
        self.client.socket.close.assert_called_once_with()
Exemplo n.º 6
0
def post_to_mllp_client(port, original_amount):
        with MLLPClient(settings.HOST, port) as client:
            amount = original_amount/len(MESSAGES)
            count = 1
            for i in xrange(amount):
                for raw_message in MESSAGES:
                    message = read_message(copy.copy(raw_message))
                    message[2][3][0][0][0] = str(random.randint(0, 1000000000))
                    client.send_message(str(message))
                    print "sending {0} of {1} to {2}".format(
                        count, original_amount, port
                    )
                    count += 1
Exemplo n.º 7
0
def main():
    signal_handler = SignalHandler()
    while True:
        try:
            with MLLPClient(server_name, port_number) as client:
                print(f"Connecting to {server_name} on {port_number}", flush=True)
                while not signal_handler.received_signal:
                    messages = queue.receive_messages(
                        AttributeNames=["All"],
                        MaxNumberOfMessages=10,
                        WaitTimeSeconds=10,
                    )
                    for message in messages:
                        try:
                            print("Processing message...", flush=True)
                            process_message(client, message.body)
                            # BrokenPipeError: [Errno 32] Broken pipe
                            # ConnectionResetError: [Errno 104] Connection reset by peer
                        except ConnectionResetError as exc:
                            client.close()
                            raise exc
                        except ConnectionAbortedError as exc:
                            client.close()
                            raise exc
                        except Exception as exc:
                            client.close()
                            logger.exception(
                                f"Exception: {repr(exc)} Connection: {server_name}:{port_number}",
                                exc_info=exc,
                            )
                            # raise RuntimeError(
                            #     f"Parameters: {server_name}:{port_number}"
                            # ) from exc
                            raise exc
                        else:
                            message.delete()
        except ConnectionResetError as exc:
            print(f"Reconnecting due to {exc}")
            continue
        except ConnectionAbortedError as exc:
            print(f"Reconnecting due to {exc}")
            continue
        except BrokenPipeError as exc:
            print(f"Reconnecting due to {exc}")
            continue
        except TimeoutError as exc:
            print(f"Reconnecting due to {exc}")
            continue
Exemplo n.º 8
0
    def test_context_manager(self):
        with MLLPClient('localhost', 6666) as client:
            client.send('hello world')

        self.client.socket.send.assert_called_once_with('hello world')
        self.client.socket.close.assert_called_once_with()
Exemplo n.º 9
0
    def setUp(self):
        # use a mock version of socket
        self.socket_patch = patch('hl7.client.socket.socket')
        self.mock_socket = self.socket_patch.start()

        self.client = MLLPClient('localhost', 6666)
Exemplo n.º 10
0
    def setUp(self):
        # use a mock version of socket
        self.socket_patch = patch('hl7.client.socket.socket')
        self.mock_socket = self.socket_patch.start()

        self.client = MLLPClient('localhost', 6666)
Exemplo n.º 11
0
class MLLPClientTest(TestCase):
    def setUp(self):
        # use a mock version of socket
        self.socket_patch = patch("hl7.client.socket.socket")
        self.mock_socket = self.socket_patch.start()

        self.client = MLLPClient("localhost", 6666)

    def tearDown(self):
        # unpatch socket
        self.socket_patch.stop()

    def test_connect(self):
        self.mock_socket.assert_called_once_with(socket.AF_INET,
                                                 socket.SOCK_STREAM)
        self.client.socket.connect.assert_called_once_with(("localhost", 6666))

    def test_close(self):
        self.client.close()
        self.client.socket.close.assert_called_once_with()

    def test_send(self):
        self.client.socket.recv.return_value = "thanks"

        result = self.client.send("foobar\n")
        self.assertEqual(result, "thanks")

        self.client.socket.send.assert_called_once_with("foobar\n")
        self.client.socket.recv.assert_called_once_with(4096)

    def test_send_message_unicode(self):
        self.client.socket.recv.return_value = "thanks"

        result = self.client.send_message(u"foobar")
        self.assertEqual(result, "thanks")

        self.client.socket.send.assert_called_once_with(b"\x0bfoobar\x1c\x0d")

    def test_send_message_bytestring(self):
        self.client.socket.recv.return_value = "thanks"

        result = self.client.send_message(b"foobar")
        self.assertEqual(result, "thanks")

        self.client.socket.send.assert_called_once_with(b"\x0bfoobar\x1c\x0d")

    def test_send_message_hl7_message(self):
        self.client.socket.recv.return_value = "thanks"

        message = hl7.parse(r"MSH|^~\&|GHH LAB|ELAB")

        result = self.client.send_message(message)
        self.assertEqual(result, "thanks")

        self.client.socket.send.assert_called_once_with(
            b"\x0bMSH|^~\\&|GHH LAB|ELAB\x1c\x0d")

    def test_context_manager(self):
        with MLLPClient("localhost", 6666) as client:
            client.send("hello world")

        self.client.socket.send.assert_called_once_with("hello world")
        self.client.socket.close.assert_called_once_with()

    def test_context_manager_exception(self):
        with self.assertRaises(Exception):
            with MLLPClient("localhost", 6666):
                raise Exception()

        # socket.close should be called via the with statement
        self.client.socket.close.assert_called_once_with()
Exemplo n.º 12
0
def send_messages(messages):
    port = PORTS[0]
    with MLLPClient(HOST, port) as client:
        for message in messages:
            client.send_message(message)