Esempio n. 1
0
def test_input(kernel_details):
    kernel_id, kernel_proc = kernel_details
    code = '''
s = input('Enter something: ')
print(s)
'''

    message = form_message(kernel_id, 'code_execution', {
        'reverse_path': CLIENT_CHANNEL,
        'code': code
    })
    disque_client.add_job(kernel_id, json.dumps(message))

    collected_messages = fetch_messages({'input_request': 1})
    assert 'input_request' in collected_messages
    assert len(collected_messages['input_request']) == 1
    assert collected_messages['input_request'][0]['msg_data'][
        'prompt'] == 'Enter something: '

    message = form_message(kernel_id, 'input_response', {'content': 'xyz'})
    disque_client.add_job(kernel_id, json.dumps(message))

    collected_messages = fetch_messages({
        'stdout': 1,
        'code_execution_complete': 1
    })
    assert 'stdout' in collected_messages
    assert len(collected_messages['stdout']) == 1
    assert collected_messages['stdout'][0]['msg_data']['content'] == 'xyz\n'
Esempio n. 2
0
def test_max_code_execution_time(kernel_details):
    kernel_id, kernel_proc = kernel_details

    code = '''
import time
while True:
    time.sleep(20)
'''

    message = form_message(kernel_id, 'code_execution', {
        'reverse_path': CLIENT_CHANNEL,
        'code': code
    })
    disque_client.add_job(kernel_id, json.dumps(message))

    collected_messages = fetch_messages(
        {
            'stderr': 1,
            'code_execution_complete': 1
        }, 5)

    assert 'stderr' in collected_messages
    assert len(collected_messages['stderr']) == 1
    assert collected_messages['stderr'][0]['msg_data'][
        'content'] == 'Code is executing for too long (>2 secs). Quota over.\n'
Esempio n. 3
0
def test_shutdown(kernel_details):
    kernel_id, kernel_proc = kernel_details

    message = form_message(kernel_id, 'shutdown')
    disque_client.add_job(kernel_id, json.dumps(message))

    try:
        kernel_proc.wait(timeout=1)
    except subprocess.TimeoutExpired:
        pass

    assert kernel_proc.returncode != None
Esempio n. 4
0
def test_session_mode(kernel_details_session_mode):
    kernel_id, kernel_proc = kernel_details_session_mode
    code1 = 'i=2'
    code2 = 'print(i)'

    message = form_message(kernel_id, 'code_execution', {
        'reverse_path': CLIENT_CHANNEL,
        'code': code1
    })
    disque_client.add_job(kernel_id, json.dumps(message))

    collected_messages = fetch_messages({'code_execution_complete': 1})

    message = form_message(kernel_id, 'code_execution', {
        'reverse_path': CLIENT_CHANNEL,
        'code': code2
    })
    disque_client.add_job(kernel_id, json.dumps(message))

    collected_messages = fetch_messages({
        'stdout': 1,
        'code_execution_complete': 1
    })

    assert 'stdout' in collected_messages
    assert len(collected_messages['stdout']) == 1

    mesg = collected_messages['stdout'][0]

    assert mesg['msg_data']['content'] == '2\n'

    message = form_message(kernel_id, 'shutdown')
    disque_client.add_job(kernel_id, json.dumps(message))

    try:
        kernel_proc.wait(timeout=1)
    except subprocess.TimeoutExpired:
        pass

    assert kernel_proc.returncode != None
Esempio n. 5
0
def test_code_execution_complete_message(kernel_details):
    kernel_id, kernel_proc = kernel_details
    code = '''i = 2'''
    message = form_message(kernel_id, 'code_execution', {
        'reverse_path': CLIENT_CHANNEL,
        'code': code
    })
    disque_client.add_job(kernel_id, json.dumps(message))

    desired_response = None
    for queue_name, system_message_id, message in disque_client.get_job(
        [CLIENT_CHANNEL], timeout=2000):
        disque_client.ack_job(system_message_id)
        message = json.loads(message)
        if message['header']['msg_type'] == 'code_execution_complete':
            desired_response = message

    assert desired_response != None
Esempio n. 6
0
def test_exception(kernel_details):
    kernel_id, kernel_proc = kernel_details
    code = '''
prin(10)
'''
    message = form_message(kernel_id, 'code_execution', {
        'reverse_path': CLIENT_CHANNEL,
        'code': code
    })
    disque_client.add_job(kernel_id, json.dumps(message))

    collected_messages = fetch_messages({
        'stderr': 10,
        'code_execution_complete': 1
    })

    assert len(collected_messages['stderr']) > 0
    assert collected_messages['stderr'][0]['msg_data']['content'].find("Traceback") > -1 \
            or collected_messages['stderr'][1]['msg_data']['content'].find("Traceback") > -1
Esempio n. 7
0
def test_stdout_message(kernel_details):
    kernel_id, kernel_proc = kernel_details
    code = '''
print("message on stdout - 0")
print("message on stdout - 1")
'''
    message = form_message(kernel_id, 'code_execution', {
        'reverse_path': CLIENT_CHANNEL,
        'code': code
    })
    disque_client.add_job(kernel_id, json.dumps(message))

    collected_messages = fetch_messages({'stdout': 2})

    assert 'stdout' in collected_messages
    assert len(collected_messages['stdout']) == 2

    mesg0 = collected_messages['stdout'][0]
    mesg1 = collected_messages['stdout'][1]

    assert mesg0['msg_data']['content'] == 'message on stdout - 0\n'
    assert mesg1['msg_data']['content'] == 'message on stdout - 1\n'
Esempio n. 8
0
def test_stdout_stderr_in_code_execution_complete(kernel_details):
    kernel_id, kernel_proc = kernel_details
    code = '''
import sys
sys.stdout.write('hello, world')
sys.stderr.write('hello, world')
'''
    message = form_message(kernel_id, 'code_execution', {
        'reverse_path': CLIENT_CHANNEL,
        'code': code
    })
    disque_client.add_job(kernel_id, json.dumps(message))

    collected_messages = fetch_messages({'code_execution_complete': 1})

    assert 'code_execution_complete' in collected_messages
    assert len(collected_messages['code_execution_complete']) == 1

    msg = collected_messages['code_execution_complete'][0]
    assert 'stdout' in msg['msg_data']
    assert msg['msg_data']['stdout'] == 'hello, world'
    assert 'stderr' in msg['msg_data']
    assert msg['msg_data']['stderr'] == 'hello, world'
Esempio n. 9
0
def send_ping_request(kernel_id):
    message = form_message(kernel_id, 'ping_request',
                           {'reverse_path': CLIENT_CHANNEL})
    disque_client.add_job(kernel_id, json.dumps(message))