# Buggy code
def execute(self, code, timeout=DEFAULT_TIMEOUT):
    """
    Executes the code provided and returns the result of that execution.
    """
    response = []
    try:
        msg_id = self._send_request(code)

        post_idle = False
        while True:
            response_message = self._get_response(msg_id, timeout, post_idle)
            if response_message:
                response_message_type = response_message["msg_type"]

                if response_message_type == "error":
                    response.append(
                        "{}:{}:{}".format(
                            response_message["content"]["ename"],
                            response_message["content"]["evalue"],
                            response_message["content"]["traceback"],
                        )
                    )
                elif response_message_type == "stream":
                    response.append(
                        Kernel._convert_raw_response(
                            response_message["content"]["text"]
                        )
                    )

                elif (
                    response_message_type == "execute_result"
                    or response_message_type == "display_data"
                ):
                    if "text/plain" in response_message["content"]["data"]:
                        response.append(
                            Kernel._convert_raw_response(
                                response_message["content"]["data"]["text/plain"]
                            )
                        )
                    elif "text/html" in response_message["content"]["data"]:
                        response.append(
                            Kernel._convert_raw_response(
                                response_message["content"]["data"]["text/html"]
                            )
                        )

                elif response_message_type == "status":
                    if response_message["content"]["execution_state"] == "idle":
                        post_idle = True  # indicate we're at the logical end and timeout poll for next message
                        continue

            if post_idle and response_message is None:
                break

    except BaseException as b:
        print(b)

    return "\n".join(response)


# Traceback (AssertionError)
======================================================================
FAIL: test_hello_world (enterprise_gateway.itests.test_scala_kernel.TestScalaKernelLocal)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/travis/build/kevin-bates/enterprise_gateway/enterprise_gateway/itests/test_scala_kernel.py", line 13, in test_hello_world
self.assertRegexpMatches(result, 'Hello World')
AssertionError: Regex didn't match: 'Hello World' not found in 'Hel\nlo World\n'

Pulled response from queue for kernel with msg_id: 3596c2f3741f48b49f27637002e4bbc1
{'buffers': [],
'channel': 'iopub',
'content': {'name': 'stdout', 'text': 'Hel'},
'header': {'date': '2018-08-25T03:28:48.696524Z',
'msg_id': '235a1ed5-5906-4a09-928e-9921a16ca6ab',
'msg_type': 'stream',
'session': 'b777e71c-f760-459a-a18e-72857bdb76cc',
'username': 'elyra',
'version': '5.0'},
'metadata': {'timestamp': '1535167728691'},
'msg_id': '235a1ed5-5906-4a09-928e-9921a16ca6ab',
'msg_type': 'stream',
'parent_header': {'msg_id': '3596c2f3741f48b49f27637002e4bbc1',
'msg_type': 'execute_request',
'session': '',
'username': '',
'version': '5.0'}}
<<<<<<<<<<<<<<<
Pulled response from queue for kernel with msg_id: 3596c2f3741f48b49f27637002e4bbc1
{'buffers': [],
'channel': 'iopub',
'content': {'name': 'stdout', 'text': 'lo World\n'},
'header': {'date': '2018-08-25T03:28:48.706633Z',
'msg_id': '8251396e-dfcb-4a48-95e4-08eb6911c37f',
'msg_type': 'stream',
'session': 'b777e71c-f760-459a-a18e-72857bdb76cc',
'username': 'elyra',
'version': '5.0'},
'metadata': {'timestamp': '1535167728702'},
'msg_id': '8251396e-dfcb-4a48-95e4-08eb6911c37f',
'msg_type': 'stream',
'parent_header': {'msg_id': '3596c2f3741f48b49f27637002e4bbc1',
'msg_type': 'execute_request',
'session': '',
'username': '',
'version': '5.0'}}

# Fixed code
def execute(self, code, timeout=DEFAULT_TIMEOUT):
    """
    Executes the code provided and returns the result of that execution.
    """
    response = []
    try:
        msg_id = self._send_request(code)

        post_idle = False
        while True:
            response_message = self._get_response(msg_id, timeout, post_idle)
            if response_message:
                response_message_type = response_message["msg_type"]

                if response_message_type == "error":
                    response.append(
                        "{}:{}:{}".format(
                            response_message["content"]["ename"],
                            response_message["content"]["evalue"],
                            response_message["content"]["traceback"],
                        )
                    )
                elif response_message_type == "stream":
                    response.append(
                        Kernel._convert_raw_response(
                            response_message["content"]["text"]
                        )
                    )

                elif (
                    response_message_type == "execute_result"
                    or response_message_type == "display_data"
                ):
                    if "text/plain" in response_message["content"]["data"]:
                        response.append(
                            Kernel._convert_raw_response(
                                response_message["content"]["data"]["text/plain"]
                            )
                        )
                    elif "text/html" in response_message["content"]["data"]:
                        response.append(
                            Kernel._convert_raw_response(
                                response_message["content"]["data"]["text/html"]
                            )
                        )

                elif response_message_type == "status":
                    if response_message["content"]["execution_state"] == "idle":
                        post_idle = True  # indicate we're at the logical end and timeout poll for next message
                        continue

            if post_idle and response_message is None:
                break

    except BaseException as b:
        print(b)

    return "".join(response)