Exemplo n.º 1
0
// test many changes at once.
static void replace_add_delete_test(void) {
  struct census_context *context =
      census_context_create(NULL, basic_tags, BASIC_TAG_COUNT, NULL);
  const census_context_status *status;
  struct census_context *context2 =
      census_context_create(context, modify_tags, MODIFY_TAG_COUNT, &status);
  census_context_status expected = {2, 1, 6, 2, 3, 4, 0, 2};
  GPR_ASSERT(memcmp(status, &expected, sizeof(expected)) == 0);
  // validate context contents. Use specific indices into the two arrays
  // holding tag values.
  GPR_ASSERT(validate_tag(context2, &basic_tags[3]));
  GPR_ASSERT(validate_tag(context2, &basic_tags[4]));
  GPR_ASSERT(validate_tag(context2, &modify_tags[0]));
  GPR_ASSERT(validate_tag(context2, &modify_tags[1]));
  GPR_ASSERT(validate_tag(context2, &modify_tags[6]));
  GPR_ASSERT(validate_tag(context2, &modify_tags[7]));
  GPR_ASSERT(validate_tag(context2, &modify_tags[8]));
  GPR_ASSERT(validate_tag(context2, &modify_tags[9]));
  GPR_ASSERT(validate_tag(context2, &modify_tags[10]));
  GPR_ASSERT(!validate_tag(context2, &basic_tags[0]));
  GPR_ASSERT(!validate_tag(context2, &basic_tags[1]));
  GPR_ASSERT(!validate_tag(context2, &basic_tags[2]));
  GPR_ASSERT(!validate_tag(context2, &basic_tags[5]));
  GPR_ASSERT(!validate_tag(context2, &basic_tags[6]));
  GPR_ASSERT(!validate_tag(context2, &basic_tags[7]));
  census_context_destroy(context);
  census_context_destroy(context2);
}
Exemplo n.º 2
0
// test encode/decode.
static void encode_decode_test(void) {
  char buffer[BUF_SIZE];
  struct census_context *context =
      census_context_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_context_encode(context, buffer, 2, &print_bsize,
                                   &bin_bsize) == NULL);
  char *b_buffer = census_context_encode(context, 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_context *context2 =
      census_context_decode(buffer, print_bsize, b_buffer, bin_bsize);
  GPR_ASSERT(context2 != NULL);
  const census_context_status *status = census_context_get_status(context2);
  census_context_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_context_get_tag(context2, basic_tags[i].key, &tag) ==
                 1);
      GPR_ASSERT(compare_tag(&tag, &basic_tags[i]));
    } else {
      GPR_ASSERT(census_context_get_tag(context2, basic_tags[i].key, &tag) ==
                 0);
    }
  }
  census_context_destroy(context2);
  census_context_destroy(context);
}
Exemplo n.º 3
0
// delete a single tag.
static void delete_tag_test(void) {
  struct census_context *context =
      census_context_create(NULL, basic_tags, BASIC_TAG_COUNT, NULL);
  const census_context_status *status;
  struct census_context *context2 = census_context_create(
      context, modify_tags + DELETE_TAG_OFFSET, 1, &status);
  census_context_status expected = {2, 1, 4, 1, 0, 0, 0, 0};
  GPR_ASSERT(memcmp(status, &expected, sizeof(expected)) == 0);
  census_tag tag;
  GPR_ASSERT(census_context_get_tag(
                 context2, modify_tags[DELETE_TAG_OFFSET].key, &tag) == 0);
  census_context_destroy(context);
  census_context_destroy(context2);
}
Exemplo n.º 4
0
// replace a single tags flags
static void replace_flags_test(void) {
  struct census_context *context =
      census_context_create(NULL, basic_tags, BASIC_TAG_COUNT, NULL);
  const census_context_status *status;
  struct census_context *context2 = census_context_create(
      context, modify_tags + REPLACE_FLAG_OFFSET, 1, &status);
  census_context_status expected = {1, 2, 5, 0, 0, 1, 0, 0};
  GPR_ASSERT(memcmp(status, &expected, sizeof(expected)) == 0);
  census_tag tag;
  GPR_ASSERT(census_context_get_tag(
                 context2, modify_tags[REPLACE_FLAG_OFFSET].key, &tag) == 1);
  GPR_ASSERT(compare_tag(&tag, &modify_tags[REPLACE_FLAG_OFFSET]));
  census_context_destroy(context);
  census_context_destroy(context2);
}
Exemplo n.º 5
0
// Make a copy of a context
static void copy_test(void) {
  struct census_context *context =
      census_context_create(NULL, basic_tags, BASIC_TAG_COUNT, NULL);
  const census_context_status *status;
  struct census_context *context2 =
      census_context_create(context, NULL, 0, &status);
  census_context_status expected = {2, 2, 4, 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;
    GPR_ASSERT(census_context_get_tag(context2, basic_tags[i].key, &tag) == 1);
    GPR_ASSERT(compare_tag(&tag, &basic_tags[i]));
  }
  census_context_destroy(context);
  census_context_destroy(context2);
}
Exemplo n.º 6
0
// Create an empty context.
static void empty_test(void) {
  struct census_context *context = census_context_create(NULL, NULL, 0, NULL);
  GPR_ASSERT(context != NULL);
  const census_context_status *status = census_context_get_status(context);
  census_context_status expected = {0, 0, 0, 0, 0, 0, 0, 0};
  GPR_ASSERT(memcmp(status, &expected, sizeof(expected)) == 0);
  census_context_destroy(context);
}
Exemplo n.º 7
0
// Test census_context_get_tag().
static void lookup_by_key_test(void) {
  struct census_context *context =
      census_context_create(NULL, basic_tags, BASIC_TAG_COUNT, NULL);
  census_tag tag;
  for (int i = 0; i < BASIC_TAG_COUNT; i++) {
    GPR_ASSERT(census_context_get_tag(context, basic_tags[i].key, &tag) == 1);
    GPR_ASSERT(compare_tag(&tag, &basic_tags[i]));
  }
  // non-existent keys
  GPR_ASSERT(census_context_get_tag(context, "key", &tag) == 0);
  GPR_ASSERT(census_context_get_tag(context, "key01", &tag) == 0);
  GPR_ASSERT(census_context_get_tag(context, "k9", &tag) == 0);
  GPR_ASSERT(census_context_get_tag(context, "random", &tag) == 0);
  GPR_ASSERT(census_context_get_tag(context, "", &tag) == 0);
  census_context_destroy(context);
}
Exemplo n.º 8
0
// Try creating context with invalid entries.
static void invalid_test(void) {
  char key[300];
  memset(key, 'k', 299);
  key[299] = 0;
  char value[300];
  memset(value, 'v', 300);
  census_tag tag = {key, value, 3, CENSUS_TAG_BINARY};
  // long keys, short value. Key lengths (including terminator) should be
  // <= 255 (CENSUS_MAX_TAG_KV_LEN)
  GPR_ASSERT(strlen(key) == 299);
  const census_context_status *status;
  struct census_context *context =
      census_context_create(NULL, &tag, 1, &status);
  census_context_status expected = {0, 0, 0, 0, 0, 0, 1, 0};
  GPR_ASSERT(memcmp(status, &expected, sizeof(expected)) == 0);
  census_context_destroy(context);
  key[CENSUS_MAX_TAG_KV_LEN] = 0;
  GPR_ASSERT(strlen(key) == CENSUS_MAX_TAG_KV_LEN);
  context = census_context_create(NULL, &tag, 1, &status);
  GPR_ASSERT(memcmp(status, &expected, sizeof(expected)) == 0);
  census_context_destroy(context);
  key[CENSUS_MAX_TAG_KV_LEN - 1] = 0;
  GPR_ASSERT(strlen(key) == CENSUS_MAX_TAG_KV_LEN - 1);
  context = census_context_create(NULL, &tag, 1, &status);
  census_context_status expected2 = {0, 0, 1, 0, 1, 0, 0, 0};
  GPR_ASSERT(memcmp(status, &expected2, sizeof(expected2)) == 0);
  census_context_destroy(context);
  // now try with long values
  tag.value_len = 300;
  context = census_context_create(NULL, &tag, 1, &status);
  GPR_ASSERT(memcmp(status, &expected, sizeof(expected)) == 0);
  census_context_destroy(context);
  tag.value_len = CENSUS_MAX_TAG_KV_LEN + 1;
  context = census_context_create(NULL, &tag, 1, &status);
  GPR_ASSERT(memcmp(status, &expected, sizeof(expected)) == 0);
  census_context_destroy(context);
  tag.value_len = CENSUS_MAX_TAG_KV_LEN;
  context = census_context_create(NULL, &tag, 1, &status);
  GPR_ASSERT(memcmp(status, &expected2, sizeof(expected2)) == 0);
  census_context_destroy(context);
  // 0 length key.
  key[0] = 0;
  context = census_context_create(NULL, &tag, 1, &status);
  GPR_ASSERT(memcmp(status, &expected, sizeof(expected)) == 0);
  census_context_destroy(context);
}
Exemplo n.º 9
0
// Test create and iteration over basic context.
static void basic_test(void) {
  const census_context_status *status;
  struct census_context *context =
      census_context_create(NULL, basic_tags, BASIC_TAG_COUNT, &status);
  census_context_status expected = {2, 2, 4, 0, 8, 0, 0, 0};
  GPR_ASSERT(memcmp(status, &expected, sizeof(expected)) == 0);
  census_context_iterator it;
  census_context_initialize_iterator(context, &it);
  census_tag tag;
  while (census_context_next_tag(&it, &tag)) {
    // can't rely on tag return order: make sure it matches exactly one.
    int matches = 0;
    for (int i = 0; i < BASIC_TAG_COUNT; i++) {
      if (compare_tag(&tag, &basic_tags[i])) matches++;
    }
    GPR_ASSERT(matches == 1);
  }
  census_context_destroy(context);
}
Exemplo n.º 10
0
void grpc_census_context_destroy(void *context) {
  census_context_destroy((census_context *)context);
}