Пример #1
0
def should_store_password_values():
    engine = engine_fake()
    gruel_schema = gruel_schema_new()
    gruel_press = gruel_press_new(
        gruel_schema=gruel_schema,
        mtu=engine.mtu)
    gruel_puff = gruel_puff_new(
        gruel_schema=gruel_schema,
        mtu=engine.mtu)
    #
    orb = nearcast_orb_new(
        engine=engine,
        nearcast_schema=gs_nearcast_schema_new())
    server_customs_cog = orb.init_cog(
        fn=server_customs_cog_new)
    r = orb.init_cog(
        fn=receiver_cog_fake)
    #
    # scenario: password message
    our_password = '******'
    r.nc_start_service(
        ip='does not matter',
        port=1234,
        password=our_password)
    #
    # confirm effects
    assert server_customs_cog.expected_password == our_password
    #
    return True
Пример #2
0
def should_allow_authorised_ip():
    engine = engine_fake()
    nearcast_schema = gs_nearcast_schema_new()
    nearcast_snoop = nearcast_snoop_fake(nearcast_schema=nearcast_schema)
    nearcast_snoop.disable()
    orb = nearcast_orb_new(engine=engine,
                           nearcast_schema=nearcast_schema,
                           nearcast_snoop=nearcast_snoop)
    r = orb.init_cog(fn=receiver_cog_fake)
    ipval_cog = orb.init_cog(fn=ipval_cog_new)
    #
    ip = '127.0.0.1'
    port = 5000
    #
    # scenario: we see permission for a differnt ip and then a connect
    r.nc_ipval_add_ip(ip='203.15.93.2')
    r.nc_announce_tcp_connect(ip=ip, port=port)
    #
    # check effects: we should still see a boot messaage
    assert 1 == r.count_please_tcp_boot()
    # but we do get a note to say it's ok
    assert 0 == r.count_nearnote()
    #
    # scenario: now we permit the relevant ip, and then see a connect
    r.nc_ipval_add_ip(ip=ip)
    r.nc_announce_tcp_connect(ip=ip, port=port)
    #
    # check effects: we should see no more boot messages
    assert 1 == r.count_please_tcp_boot()
    # but we do get a note to say it's ok
    assert 1 == r.count_nearnote()
    #
    return True
Пример #3
0
def should_clear_state_in_announce_connect():
    engine = engine_fake()
    clock = engine.get_clock()
    gruel_schema = gruel_schema_new()
    gruel_press = gruel_press_new(
        gruel_schema=gruel_schema,
        mtu=engine.mtu)
    gruel_puff = gruel_puff_new(
        gruel_schema=gruel_schema,
        mtu=engine.mtu)
    #
    orb = nearcast_orb_new(
        engine=engine,
        nearcast_schema=gs_nearcast_schema_new())
    server_customs_cog = orb.init_cog(
        fn=server_customs_cog_new)
    r = orb.init_cog(
        fn=receiver_cog_fake)
    #
    r.nc_announce_tcp_connect(
        ip='does not matter',
        port=1234)
    assert 1 == r.count_nearnote()
    #
    r.nc_announce_tcp_condrop()
    assert 2 == r.count_nearnote()
    #
    return True
Пример #4
0
def should_throw_alg_exception_if_packet_seen_before_password():
    engine = engine_fake()
    gruel_schema = gruel_schema_new()
    gruel_press = gruel_press_new(
        gruel_schema=gruel_schema,
        mtu=engine.mtu)
    gruel_puff = gruel_puff_new(
        gruel_schema=gruel_schema,
        mtu=engine.mtu)
    #
    orb = nearcast_orb_new(
        engine=engine,
        nearcast_schema=gs_nearcast_schema_new())
    server_customs_cog = orb.init_cog(
        fn=server_customs_cog_new)
    r = orb.init_cog(
        fn=receiver_cog_fake)
    #
    # scenario: gruel_recv happens without a password having been set
    b_error = False
    try:
        r.nc_gruel_recv(
            d_gruel=gruel_puff.unpack(
                payload=gruel_press.create_heartbeat_payload()))
    except:
        b_error = True
    if b_error:
        return True
    else:
        log('expected an exception, did not get one')
        return False
Пример #5
0
def should_start_and_stop():
    engine = engine_fake()
    nearcast_schema = gs_nearcast_schema_new()
    snoop = nearcast_snoop_fake(nearcast_schema=nearcast_schema)
    snoop.disable()
    orb = nearcast_orb_new(engine=engine,
                           nearcast_schema=nearcast_schema,
                           nearcast_snoop=snoop)
    r = orb.init_cog(fn=receiver_cog_fake)
    tcp_server_cog = orb.init_cog(fn=tcp_server_cog_new)
    #
    addr = '127.0.0.1'
    port = 5000
    password = '******'
    #
    # confirm starting state
    orb.cycle()
    assert tcp_server_cog.server_sid == None
    assert tcp_server_cog.client_sid == None
    #
    # scenario: start the server
    r.nc_start_service(ip=addr, port=port, password=password)
    #
    # confirm effects
    assert tcp_server_cog.server_sid != None
    assert tcp_server_cog.client_sid == None
    #
    # scenario: stop the service
    stop_service(orb=orb)
    #
    # confirm effects
    assert tcp_server_cog.server_sid == None
    assert tcp_server_cog.client_sid == None
    #
    return True
Пример #6
0
def should_send_gruel_send_data_to_a_connected_client():
    gruel_schema = gruel_schema_new()
    gruel_press = gruel_press_new(gruel_schema=gruel_schema, mtu=MTU)
    gruel_puff = gruel_puff_new(gruel_schema=gruel_schema, mtu=MTU)
    #
    engine = engine_fake()
    nearcast_schema = gs_nearcast_schema_new()
    snoop = nearcast_snoop_fake(nearcast_schema=nearcast_schema)
    snoop.disable()
    orb = nearcast_orb_new(engine=engine,
                           nearcast_schema=nearcast_schema,
                           nearcast_snoop=snoop)
    r = orb.init_cog(fn=receiver_cog_fake)
    tcp_server_cog = orb.init_cog(fn=tcp_server_cog_new)
    #
    addr = '127.0.0.1'
    port = 5000
    password = '******'
    #
    # confirm starting state
    assert 0 == r.count_announce_tcp_connect()
    assert tcp_server_cog.server_sid == None
    assert tcp_server_cog.client_sid == None
    #
    # scenario: start the server
    start_service(orb=orb, ip=addr, port=port, password=password)
    #
    # confirm effects
    assert 0 == r.count_announce_tcp_connect()
    assert tcp_server_cog.server_sid != None
    assert tcp_server_cog.client_sid == None
    #
    # scenario: client connects
    client_addr = '203.15.93.150'
    client_port = 6000
    simulate_client_connect(engine=engine,
                            orb=orb,
                            tcp_server_cog=tcp_server_cog,
                            ip=client_addr,
                            port=client_port)
    #
    # confirm baseline
    assert 1 == r.count_announce_tcp_connect()
    assert tcp_server_cog.server_sid == None
    assert tcp_server_cog.client_sid != None
    #
    # scenario: tcp_server_cog gets gruel_send but client is not connected
    r.nc_gruel_send(payload=gruel_press.create_client_login_payload(
        password='******', heartbeat_interval=4))
    orb.cycle()
    #
    # confirm effects: client should still be connected, and we should have
    # sent no packets to the engine.
    assert tcp_server_cog.server_sid == None
    assert tcp_server_cog.client_sid != None
    assert 1 == len(engine.sent_data)
    #
    return True
Пример #7
0
def should_run_a_rejection_sequence():
    engine = engine_fake()
    clock = engine.get_clock()
    gruel_schema = gruel_schema_new()
    gruel_press = gruel_press_new(
        gruel_schema=gruel_schema,
        mtu=engine.mtu)
    gruel_puff = gruel_puff_new(
        gruel_schema=gruel_schema,
        mtu=engine.mtu)
    #
    orb = nearcast_orb_new(
        engine=engine,
        nearcast_schema=gs_nearcast_schema_new())
    server_customs_cog = orb.init_cog(
        fn=server_customs_cog_new)
    r = orb.init_cog(
        fn=receiver_cog_fake)
    #
    # start the service
    our_password = '******'
    usr_password = '******'
    r.nc_start_service(
        ip='does not matter',
        port=1234,
        password=our_password)
    #
    # confirm effects: see a 
    assert 0 == clock.now()
    #
    # scenario: rejection is sequenced
    server_customs_cog._to_rejection(
        s='triggered by test')
    #
    # nothing should have changed
    assert 0 == r.count_gruel_send()
    assert 0 == r.count_please_tcp_boot()
    #
    # at three seconds we should see server_bye but no boot
    clock.inc(3)
    orb.cycle()
    assert 1 == r.count_gruel_send()
    d_gruel = gruel_puff.unpack(
        payload=r.last_gruel_send())
    assert d_gruel['message_h'] == 'server_bye'
    assert 0 == r.count_please_tcp_boot()
    #
    # at four seconds we should see the boot
    clock.inc(1)
    orb.cycle()
    assert 1 == r.count_please_tcp_boot()
    #
    return True
Пример #8
0
def should_handle_client_connect_and_then_boot_client():
    engine = engine_fake()
    nearcast_schema = gs_nearcast_schema_new()
    snoop = nearcast_snoop_fake(nearcast_schema=nearcast_schema)
    snoop.disable()
    orb = nearcast_orb_new(engine=engine,
                           nearcast_schema=nearcast_schema,
                           nearcast_snoop=snoop)
    r = orb.init_cog(fn=receiver_cog_fake)
    tcp_server_cog = orb.init_cog(fn=tcp_server_cog_new)
    #
    addr = '127.0.0.1'
    port = 5000
    password = '******'
    #
    # confirm starting state
    assert 0 == r.count_announce_tcp_connect()
    assert tcp_server_cog.server_sid == None
    assert tcp_server_cog.client_sid == None
    #
    # scenario: start the server
    start_service(orb=orb, ip=addr, port=port, password=password)
    #
    # confirm effects
    assert 0 == r.count_announce_tcp_connect()
    assert tcp_server_cog.server_sid != None
    assert tcp_server_cog.client_sid == None
    #
    # scenario: client connects
    client_addr = '203.15.93.150'
    client_port = 6000
    simulate_client_connect(engine=engine,
                            orb=orb,
                            tcp_server_cog=tcp_server_cog,
                            ip=client_addr,
                            port=client_port)
    #
    # confirm effects
    assert 1 == r.count_announce_tcp_connect()
    assert tcp_server_cog.server_sid == None
    assert tcp_server_cog.client_sid != None
    #
    # scenario: stop the service, booting the client in the process
    stop_service(orb=orb)
    #
    # confirm effects
    assert tcp_server_cog.server_sid == None
    assert tcp_server_cog.client_sid == None
    #
    return True
Пример #9
0
def should_boot_client_when_invalid_gruel_is_received():
    activity = activity_new()
    engine = engine_fake()
    nearcast_schema = gs_nearcast_schema_new()
    snoop = nearcast_snoop_fake(nearcast_schema=nearcast_schema)
    snoop.disable()
    orb = nearcast_orb_new(engine=engine,
                           nearcast_schema=nearcast_schema,
                           nearcast_snoop=snoop)
    #
    r = orb.init_cog(fn=receiver_cog_fake)
    tcp_server_cog = orb.init_cog(fn=tcp_server_cog_new)
    #
    addr = '127.0.0.1'
    port = 5000
    password = '******'
    #
    # get to a point where the client is logged in
    start_service(orb=orb, ip=addr, port=port, password=password)
    simulate_client_connect(engine=engine,
                            orb=orb,
                            tcp_server_cog=tcp_server_cog,
                            ip=addr,
                            port=port)
    #
    # confirm starting position
    assert 1 == r.count_announce_tcp_connect()
    assert 0 == r.count_please_tcp_boot()
    assert tcp_server_cog.server_sid == None
    assert tcp_server_cog.client_sid != None
    #
    # scenario: send invalid data as though it is gruel
    cs_tcp_recv = CsTcpRecv()
    cs_tcp_recv.engine = engine
    cs_tcp_recv.client_sid = create_sid()
    cs_tcp_recv.data = bytes('this string is invalid gruel', 'utf8')
    tcp_server_cog._engine_on_tcp_recv(cs_tcp_recv=cs_tcp_recv)
    orb.cycle()
    #
    # confirm effects: nearcast a boot message
    assert 1 == r.count_please_tcp_boot()
    assert tcp_server_cog.server_sid != None
    assert tcp_server_cog.client_sid == None
    #
    return True
Пример #10
0
def should_do_basic_login_reject():
    engine = engine_fake()
    clock = engine.get_clock()
    gruel_schema = gruel_schema_new()
    gruel_press = gruel_press_new(
        gruel_schema=gruel_schema,
        mtu=engine.mtu)
    gruel_puff = gruel_puff_new(
        gruel_schema=gruel_schema,
        mtu=engine.mtu)
    #
    orb = nearcast_orb_new(
        engine=engine,
        nearcast_schema=gs_nearcast_schema_new())
    server_customs_cog = orb.init_cog(
        fn=server_customs_cog_new)
    r = orb.init_cog(
        fn=receiver_cog_fake)
    #
    # start the service
    our_password = '******'
    usr_password = '******'
    r.nc_start_service(
        ip='does not matter',
        port=1234,
        password=our_password)
    #
    # check starting position
    assert 0 == r.count_please_tcp_boot()
    #
    # scenario: user sends an invalid password
    r.nc_gruel_recv(
        d_gruel=gruel_puff.unpack(
            payload=gruel_press.create_client_login_payload(
                password=usr_password,
                heartbeat_interval=3)))
    orb.cycle()
    #
    # check effects: we should boot the client
    assert server_customs_cog.state == ServerCustomsState.reject_stage_a
    assert 0 == r.count_gruel_send()
    #
    return True
Пример #11
0
def should_do_successful_login_accept():
    engine = engine_fake()
    clock = engine.get_clock()
    gruel_schema = gruel_schema_new()
    gruel_press = gruel_press_new(
        gruel_schema=gruel_schema,
        mtu=engine.mtu)
    gruel_puff = gruel_puff_new(
        gruel_schema=gruel_schema,
        mtu=engine.mtu)
    #
    orb = nearcast_orb_new(
        engine=engine,
        nearcast_schema=gs_nearcast_schema_new())
    server_customs_cog = orb.init_cog(
        fn=server_customs_cog_new)
    r = orb.init_cog(
        fn=receiver_cog_fake)
    #
    # start the service
    our_password = '******'
    r.nc_start_service(
        ip='does not matter',
        port=1234,
        password=our_password)
    #
    # scenario: user sends an valid password
    r.nc_gruel_recv(
        d_gruel=gruel_puff.unpack(
            payload=gruel_press.create_client_login_payload(
                password=our_password,
                heartbeat_interval=3)))
    orb.cycle()
    #
    # confirm effects
    assert server_customs_cog.state == ServerCustomsState.authorised
    assert 1 == r.count_announce_login()
    assert 1 == r.count_gruel_send()
    d_grual = gruel_puff.unpack(r.last_gruel_send())
    assert d_grual['message_h'] == 'server_greet'
    #
    return True
Пример #12
0
def should_reject_unauthorised_ip():
    engine = engine_fake()
    nearcast_schema = gs_nearcast_schema_new()
    nearcast_snoop = nearcast_snoop_fake(nearcast_schema=nearcast_schema)
    nearcast_snoop.disable()
    orb = nearcast_orb_new(engine=engine,
                           nearcast_schema=nearcast_schema,
                           nearcast_snoop=nearcast_snoop)
    r = orb.init_cog(fn=receiver_cog_fake)
    ipval_cog = orb.init_cog(fn=ipval_cog_new)
    #
    # scenario: unauth ip connects
    ip = '127.0.0.1'
    port = 5000
    r.nc_announce_tcp_connect(ip=ip, port=port)
    #
    # check effects: we should send a messaage to boot
    assert 1 == r.count_please_tcp_boot()
    #
    return True
Пример #13
0
def should_allow_any_ip():
    engine = engine_fake()
    nearcast_schema = gs_nearcast_schema_new()
    nearcast_snoop = nearcast_snoop_fake(nearcast_schema=nearcast_schema)
    nearcast_snoop.disable()
    orb = nearcast_orb_new(engine=engine,
                           nearcast_schema=nearcast_schema,
                           nearcast_snoop=nearcast_snoop)
    r = orb.init_cog(fn=receiver_cog_fake)
    ipval_cog = orb.init_cog(fn=ipval_cog_new)
    #
    ip = '127.0.0.1'
    port = 5000
    r.nc_ipval_disable()
    r.nc_announce_tcp_connect(ip=ip, port=port)
    #
    assert 0 == r.count_please_tcp_boot()
    assert 1 == r.count_nearnote()
    #
    return True
Пример #14
0
def should_boot_user_on_receipt_of_login_message_when_already_logged_in():
    engine = engine_fake()
    clock = engine.get_clock()
    gruel_schema = gruel_schema_new()
    gruel_press = gruel_press_new(
        gruel_schema=gruel_schema,
        mtu=engine.mtu)
    gruel_puff = gruel_puff_new(
        gruel_schema=gruel_schema,
        mtu=engine.mtu)
    #
    orb = nearcast_orb_new(
        engine=engine,
        nearcast_schema=gs_nearcast_schema_new())
    server_customs_cog = orb.init_cog(
        fn=server_customs_cog_new)
    r = orb.init_cog(
        fn=receiver_cog_fake)
    #
    # start the service
    our_password = '******'
    r.nc_start_service(
        ip='does not matter',
        port=1234,
        password=our_password)
    #
    # hack the starting position
    server_customs_cog.state = ServerCustomsState.authorised
    #
    # scenario: send a message that is client_login
    r.nc_gruel_recv(
        d_gruel=gruel_puff.unpack(
            payload=gruel_press.create_client_login_payload(
                password=our_password,
                heartbeat_interval=3)))
    orb.cycle()
    #
    # check effects
    assert server_customs_cog.state == ServerCustomsState.reject_stage_a
    #
    return True
Пример #15
0
def should_boot_client_if_first_message_is_not_client_login():
    engine = engine_fake()
    clock = engine.get_clock()
    gruel_schema = gruel_schema_new()
    gruel_press = gruel_press_new(
        gruel_schema=gruel_schema,
        mtu=engine.mtu)
    gruel_puff = gruel_puff_new(
        gruel_schema=gruel_schema,
        mtu=engine.mtu)
    #
    orb = nearcast_orb_new(
        engine=engine,
        nearcast_schema=gs_nearcast_schema_new())
    server_customs_cog = orb.init_cog(
        fn=server_customs_cog_new)
    r = orb.init_cog(
        fn=receiver_cog_fake)
    #
    # start the service
    our_password = '******'
    r.nc_start_service(
        ip='does not matter',
        port=1234,
        password=our_password)
    #
    # check starting position
    assert 0 == r.count_please_tcp_boot()
    #
    # scenario: first message is not client_login
    r.nc_gruel_recv(
        d_gruel=gruel_puff.unpack(
            payload=gruel_press.create_heartbeat_payload()))
    orb.cycle()
    #
    # check effects
    assert server_customs_cog.expected_password == our_password
    #
    return True
Пример #16
0
def should_boot_client_when_told_to():
    engine = engine_fake()
    nearcast_schema = gs_nearcast_schema_new()
    snoop = nearcast_snoop_fake(nearcast_schema=nearcast_schema)
    snoop.disable()
    orb = nearcast_orb_new(engine=engine,
                           nearcast_schema=nearcast_schema,
                           nearcast_snoop=snoop)
    #
    r = orb.init_cog(fn=receiver_cog_fake)
    tcp_server_cog = orb.init_cog(fn=tcp_server_cog_new)
    #
    addr = '127.0.0.1'
    port = 5000
    password = '******'
    #
    # get to a point where the client is logged in
    start_service(orb=orb, ip=addr, port=port, password=password)
    simulate_client_connect(engine=engine,
                            orb=orb,
                            tcp_server_cog=tcp_server_cog,
                            ip=addr,
                            port=port)
    #
    # confirm starting position
    assert 1 == r.count_announce_tcp_connect()
    assert tcp_server_cog.server_sid == None
    assert tcp_server_cog.client_sid != None
    #
    # scenario: we receive a boot message
    r.nc_please_tcp_boot()
    #
    # check effects: we want to see that the connection has been dropped and
    # that the server is back up
    assert tcp_server_cog.server_sid != None
    assert tcp_server_cog.client_sid == None
    #
    return True
Пример #17
0
def should_buffer_a_couple_of_docs():
    engine = engine_fake()
    clock = engine.get_clock()
    gruel_schema = gruel_schema_new()
    gruel_press = gruel_press_new(
        gruel_schema=gruel_schema,
        mtu=engine.mtu)
    gruel_puff = gruel_puff_new(
        gruel_schema=gruel_schema,
        mtu=engine.mtu)
    #
    orb = nearcast_orb_new(
        engine=engine,
        nearcast_schema=gs_nearcast_schema_new())
    server_customs_cog = orb.init_cog(
        fn=server_customs_cog_new)
    r = orb.init_cog(
        fn=receiver_cog_fake)
    #
    # start the service
    our_password = '******'
    r.nc_start_service(
        ip='does not matter',
        port=1234,
        password=our_password)
    #
    # preparation: user logs in
    r.nc_announce_tcp_connect(
        ip='also not important',
        port=456)
    r.nc_gruel_recv(
        d_gruel=gruel_puff.unpack(
            payload=gruel_press.create_client_login_payload(
                password=our_password,
                heartbeat_interval=3)))
    orb.cycle()
    #
    # scenario: user sends part of a doc
    r.nc_gruel_recv(
        d_gruel=gruel_puff.unpack(
            payload=gruel_press.create_docdata_payload(
                b_complete=0,
                data='123')))
    orb.cycle()
    #
    # confirm effects (doc is not complete, so should be no effects)
    assert 0 == r.count_doc_recv()
    #
    # scenario: now finish the first doc
    r.nc_gruel_recv(
        d_gruel=gruel_puff.unpack(
            payload=gruel_press.create_docdata_payload(
                b_complete=1,
                data='456')))
    orb.cycle()
    #
    # confirm effects (doc is not complete, so should be no effects)
    assert 1 == r.count_doc_recv()
    assert r.last_doc_recv() == '123456'
    #
    # scenario: now send a second doc, and check it's correct
    r.nc_gruel_recv(
        d_gruel=gruel_puff.unpack(
            payload=gruel_press.create_docdata_payload(
                b_complete=1,
                data='here is a doc')))
    orb.cycle()
    #
    # confirm effects
    assert 2 == r.count_doc_recv()
    assert r.last_doc_recv() == 'here is a doc'
    #
    return True
Пример #18
0
def should_send_a_couple_of_docs():
    engine = engine_fake()
    clock = engine.get_clock()
    gruel_schema = gruel_schema_new()
    gruel_press = gruel_press_new(
        gruel_schema=gruel_schema,
        mtu=engine.mtu)
    gruel_puff = gruel_puff_new(
        gruel_schema=gruel_schema,
        mtu=engine.mtu)
    #
    orb = nearcast_orb_new(
        engine=engine,
        nearcast_schema=gs_nearcast_schema_new())
    server_customs_cog = orb.init_cog(
        fn=server_customs_cog_new)
    r = orb.init_cog(
        fn=receiver_cog_fake)
    #
    # start the service
    our_password = '******'
    r.nc_start_service(
        ip='does not matter',
        port=1234,
        password=our_password)
    #
    # preparation: user logs in
    r.nc_announce_tcp_connect(
        ip='also not important',
        port=456)
    r.nc_gruel_recv(
        d_gruel=gruel_puff.unpack(
            payload=gruel_press.create_client_login_payload(
                password=our_password,
                heartbeat_interval=3)))
    orb.cycle()
    #
    # confirm assumptions
    assert 1 == r.count_gruel_send()
    #
    # scenario: we send a large outbound document towards customs
    doc = '/'.join( ['x', 'y'*2000, 'z'] )
    r.nc_doc_send(
        doc=doc)
    #
    # confirm effects: expect to see the doc broken up into several pieces
    assert 2 <= r.count_gruel_send()
    # examine first doc packet
    d_gruel = gruel_puff.unpack(r.get_gruel_send()[1])
    assert d_gruel['message_h'] == 'docdata'
    assert d_gruel['b_complete'] == 0
    assert d_gruel['data'][0] == 'x'
    # examine next-to-last doc packet
    d_gruel = gruel_puff.unpack(r.get_gruel_send()[-2])
    assert d_gruel['message_h'] == 'docdata'
    assert d_gruel['b_complete'] == 0
    assert d_gruel['data'][-1] == 'y'
    # examine last doc packet
    d_gruel = gruel_puff.unpack(r.get_gruel_send()[-1])
    assert d_gruel['message_h'] == 'docdata'
    assert d_gruel['b_complete'] == 1
    assert d_gruel['data'][-1] == 'z'
    #
    return True
Пример #19
0
def should_broadcast_incoming_message_as_gruel_in():
    engine = engine_fake()
    nearcast_schema = gs_nearcast_schema_new()
    snoop = nearcast_snoop_fake(nearcast_schema=nearcast_schema)
    snoop.disable()
    orb = nearcast_orb_new(engine=engine,
                           nearcast_schema=nearcast_schema,
                           nearcast_snoop=snoop)
    #
    r = orb.init_cog(fn=receiver_cog_fake)
    tcp_server_cog = orb.init_cog(fn=tcp_server_cog_new)
    #
    addr = '127.0.0.1'
    port = 5000
    password = '******'
    #
    # get to a point where the client is logged in
    start_service(orb=orb, ip=addr, port=port, password=password)
    simulate_client_connect(engine=engine,
                            orb=orb,
                            tcp_server_cog=tcp_server_cog,
                            ip=addr,
                            port=port)
    #
    # confirm starting position
    assert 1 == r.count_announce_tcp_connect()
    assert tcp_server_cog.server_sid == None
    assert tcp_server_cog.client_sid != None
    #
    # scenario: client sends a message
    client_send_password = '******'
    client_send_hbint = 3
    simulate_client_send_login(engine=engine,
                               orb=orb,
                               tcp_server_cog=tcp_server_cog,
                               pw=client_send_password,
                               hbint=client_send_hbint)
    #
    # confirm effects
    assert 1 == r.count_gruel_recv()
    d_gruel = r.last_gruel_recv()
    assert d_gruel['message_h'] == 'client_login'
    assert d_gruel['heartbeat_interval'] == client_send_hbint
    assert d_gruel['password'] == client_send_password
    #
    # scenario: client disconnect
    simulate_client_condrop(engine=engine,
                            orb=orb,
                            tcp_server_cog=tcp_server_cog)
    #
    # confirm effects
    assert tcp_server_cog.server_sid != None
    assert tcp_server_cog.client_sid == None
    #
    # scenario: close server
    stop_service(orb=orb)
    #
    # confirm effects
    assert tcp_server_cog.server_sid == None
    assert tcp_server_cog.client_sid == None
    #
    return True