コード例 #1
0
def check_enforced_non_communication(log: Stream, ctx: FullContext,
                                     typename: str) -> bool:
    datatype_regular = ctx.get_datatype(typename)
    if datatype_regular.__idl__.keyless:
        return True

    narrow_ctx = ctx.narrow_context_of(typename)
    new_scope = deepcopy(narrow_ctx.scope)
    non_valid_mutation(new_scope, typename)
    mutated_ctx = FullContext(new_scope)
    mutated_datatype = mutated_ctx.get_datatype(typename)

    normal_without_header_idl = "\n".join(narrow_ctx.idl_file.splitlines()[1:])
    mutated_without_header_idl = "\n".join(
        mutated_ctx.idl_file.splitlines()[1:])

    if normal_without_header_idl == mutated_without_header_idl:
        # No mutation took place (only unions) just assume it is good
        return True

    dp = DomainParticipant()

    try:
        tp = Topic(dp, typename, mutated_datatype)
    except DDSException:
        # Sometimes the type gets so mangled (like empty structs/unions)
        # that it is not a valid topic type anymore. We'll consider this a
        # successful test.
        return True

    dw = DataWriter(
        dp,
        tp,
        qos=Qos(Policy.DataRepresentation(use_xcdrv2_representation=True),
                Policy.Reliability.Reliable(duration(seconds=2)),
                Policy.DestinationOrder.BySourceTimestamp))
    dw.set_status_mask(DDSStatus.PublicationMatched)
    dw.take_status()

    ctx.c_app.run(typename, 1)

    now = time.time()
    while (dw.take_status() & DDSStatus.PublicationMatched) == 0:
        if time.time() - now > 0.5:
            ctx.c_app.process.kill()
            return True
        time.sleep(0.001)

    ctx.c_app.process.kill()

    log << f"C-app agreed to communicate with non-valid mutation" << log.endl << log.indent
    log << log.dedent << "[Mutated IDL]:" << log.indent << log.endl
    log << mutated_ctx.idl_file << log.endl
    log << log.dedent
    return False
コード例 #2
0
def check_py_c_key_equivalence(log: Stream, ctx: FullContext, typename: str,
                               num_samples: int) -> bool:
    datatype = ctx.get_datatype(typename)
    if datatype.__idl__.keyless:
        return True

    samples = [
        generate_random_instance(datatype, seed=i) for i in range(num_samples)
    ]

    # We need to make sure the samples all have unique keys to make sure that we agree
    # on sample ordering with C
    keysamples = {}
    for s in samples:
        keysamples[datatype.__idl__.key(s)] = s
    samples = list(keysamples.values())

    dp = DomainParticipant()
    tp = Topic(dp, typename, datatype)
    dw = DataWriter(
        dp,
        tp,
        qos=Qos(Policy.DataRepresentation(use_xcdrv2_representation=True),
                Policy.History.KeepLast(len(samples)),
                Policy.Reliability.Reliable(duration(seconds=2)),
                Policy.DestinationOrder.BySourceTimestamp))
    dw.set_status_mask(DDSStatus.PublicationMatched)
    dw.take_status()

    ctx.c_app.run(typename, len(samples))

    now = time.time()
    while (dw.take_status() & DDSStatus.PublicationMatched) == 0:
        if time.time() - now > 4:
            # timeout if C app did not start up within 4 seconds
            ctx.c_app.result()
            log << f"C-app did not communicate with Python:" << log.endl << log.indent
            log << ctx.c_app.last_error << log.endl
            log << log.dedent
            return False
        time.sleep(0.001)

    time.sleep(0.5)

    for sample in samples:
        dw.write(sample)
        time.sleep(0.002)

    hashes = ctx.c_app.result()
    success = True

    if not hashes:
        log << f"C-app did not return output, stderr:" << log.endl << log.indent
        log << ctx.c_app.last_error << log.endl
        log << f"stdout:" << log.endl
        log << ctx.c_app.last_out << log.endl
        log << log.dedent << "Example sample sent:" << log.endl << log.indent
        log << samples[0] << log.endl << samples[0].serialize()
        log << log.dedent
        return False

    if len(hashes) != len(samples):
        log << f"C-app did not return as many samples as were sent, stderr:" << log.endl << log.indent
        log << ctx.c_app.last_error << log.endl
        log << f"stdout:" << log.endl
        log << ctx.c_app.last_out << log.endl << log.dedent
        log << log.dedent << "Example sample sent:" << log.endl << log.indent
        log << samples[0] << log.endl << samples[0].serialize()
        success = False

    for i in range(min(len(hashes), len(samples))):
        c_key = hashes[i]
        py_key = datatype.__idl__.key(samples[i], use_version_2=True)

        if not py_key == c_key:
            log << "PY-C Keys do not match!" << log.endl << log.indent
            log << "Instance: " << samples[i] << log.endl
            log << "Serialized Instance:" << log.endl << samples[i].serialize()
            log << "Python key:" << log.endl << py_key
            log << "C key:" << log.endl << c_key
            log << log.dedent
            return False

    return success