// test encode/decode. static void encode_decode_test(void) { char buffer[BUF_SIZE]; struct census_tag_set *cts = census_tag_set_create(NULL, basic_tags, BASIC_TAG_COUNT, NULL); size_t print_bsize; size_t bin_bsize; // Test with too small a buffer GPR_ASSERT(census_tag_set_encode(cts, buffer, 2, &print_bsize, &bin_bsize) == NULL); char *b_buffer = census_tag_set_encode(cts, buffer, BUF_SIZE, &print_bsize, &bin_bsize); GPR_ASSERT(b_buffer != NULL && print_bsize > 0 && bin_bsize > 0 && print_bsize + bin_bsize <= BUF_SIZE && b_buffer == buffer + print_bsize); census_tag_set *cts2 = census_tag_set_decode(buffer, print_bsize, b_buffer, bin_bsize); GPR_ASSERT(cts2 != NULL); const census_tag_set_create_status *status = census_tag_set_get_create_status(cts2); census_tag_set_create_status expected = {2, 2, 0, 0, 0, 0, 0, 0}; GPR_ASSERT(memcmp(status, &expected, sizeof(expected)) == 0); for (int i = 0; i < BASIC_TAG_COUNT; i++) { census_tag tag; if (CENSUS_TAG_IS_PROPAGATED(basic_tags[i].flags)) { GPR_ASSERT(census_tag_set_get_tag_by_key(cts2, basic_tags[i].key, &tag) == 1); GPR_ASSERT(compare_tag(&tag, &basic_tags[i])); } else { GPR_ASSERT(census_tag_set_get_tag_by_key(cts2, basic_tags[i].key, &tag) == 0); } } census_tag_set_destroy(cts2); census_tag_set_destroy(cts); }
// Add/modify/delete a tag to/in a context. Caller must validate that tag key // etc. are valid. static void context_modify_tag(census_context *context, const census_tag *tag, size_t key_len, size_t value_len) { // First delete the tag if it is already present. bool deleted = context_delete_tag(context, tag, key_len); bool added = false; if (CENSUS_TAG_IS_PROPAGATED(tag->flags)) { added = tag_set_add_tag(&context->tags[PROPAGATED_TAGS], tag, key_len, value_len); } else { added = tag_set_add_tag(&context->tags[LOCAL_TAGS], tag, key_len, value_len); } if (deleted) { context->status.n_modified_tags++; } else { if (added) { context->status.n_added_tags++; } else { context->status.n_ignored_tags++; } } }