@cryptotaxi247 / netdata-1 / commits / 2a6d0a35f

Improve unittests (#16329)

* Update flags during label copy Update unittests * Return proper value * Improve double check in unittests

Stelios Fragkakis committed Nov 6, 2023 at 21:39 UTC 2a6d0a35f208e90e306b31857e0c037bfc165796
1 file changed +72 -73
database/rrdlabels.c
+72 -73
@@ -1040,14 +1040,13 @@ void rrdlabels_copy(RRDLABELS *dst, RRDLABELS *src)
1040 lfe_start_nolock(src, label, ls)
1041 {
1042 RRDLABEL *old_label_with_key = rrdlabels_find_label_with_key_unsafe(dst, label);
1043 -
1043 Pvoid_t *PValue = JudyLIns(&dst->JudyL, (Word_t)label, PJE0);
1044 if(unlikely(!PValue || PValue == PJERR))
1045 fatal("RRDLABELS: corrupted labels array");
1046
1047 if (!*PValue) {
1048 dup_label(label);
1050 - *((RRDLABEL_SRC *)PValue) = ls;
1049 + ls = (ls & ~(RRDLABEL_FLAG_OLD)) | RRDLABEL_FLAG_NEW;
1050 dst->version++;
1051 update_statistics = true;
1052 if (old_label_with_key) {
@@ -1055,6 +1054,10 @@ void rrdlabels_copy(RRDLABELS *dst, RRDLABELS *src)
1054 delete_label((RRDLABEL *)old_label_with_key);
1055 }
1056 }
1057 + else
1058 + ls = (ls & ~(RRDLABEL_FLAG_NEW)) | RRDLABEL_FLAG_OLD;
1059 +
1060 + *((RRDLABEL_SRC *)PValue) = ls;
1061 }
1062 lfe_done_nolock();
1063 if (update_statistics) {
@@ -1318,7 +1321,30 @@ struct rrdlabels_unittest_add_a_pair {
1321 int errors;
1322 };
1323
1321 -int rrdlabels_unittest_add_a_pair_callback(const char *name, const char *value, RRDLABEL_SRC ls __maybe_unused, void *data) {
1324 +RRDLABEL *rrdlabels_find_label_with_key(RRDLABELS *labels, const char *key, RRDLABEL_SRC *source)
1325 +{
1326 + if (!labels || !key)
1327 + return NULL;
1328 +
1329 + STRING *this_key = string_strdupz(key);
1330 +
1331 + RRDLABEL *lb = NULL;
1332 + RRDLABEL_SRC ls;
1333 +
1334 + lfe_start_read(labels, lb, ls)
1335 + {
1336 + if (lb->index.key == this_key) {
1337 + if (source)
1338 + *source = ls;
1339 + break;
1340 + }
1341 + }
1342 + lfe_done(labels);
1343 + string_freez(this_key);
1344 + return lb;
1345 +}
1346 +
1347 +static int rrdlabels_unittest_add_a_pair_callback(const char *name, const char *value, RRDLABEL_SRC ls __maybe_unused, void *data) {
1348 struct rrdlabels_unittest_add_a_pair *t = (struct rrdlabels_unittest_add_a_pair *)data;
1349
1350 t->name = name;
@@ -1344,7 +1370,7 @@ int rrdlabels_unittest_add_a_pair_callback(const char *name, const char *value,
1370 return 1;
1371 }
1372
1347 -int rrdlabels_unittest_add_a_pair(const char *pair, const char *name, const char *value) {
1373 +static int rrdlabels_unittest_add_a_pair(const char *pair, const char *name, const char *value) {
1374 RRDLABELS *labels = rrdlabels_create();
1375 int errors;
1376
@@ -1374,7 +1400,7 @@ int rrdlabels_unittest_add_a_pair(const char *pair, const char *name, const char
1400 return errors;
1401 }
1402
1377 -int rrdlabels_unittest_add_pairs() {
1403 +static int rrdlabels_unittest_add_pairs() {
1404 fprintf(stderr, "\n%s() tests\n", __FUNCTION__);
1405
1406 int errors = 0;
@@ -1422,66 +1448,33 @@ int rrdlabels_unittest_add_pairs() {
1448 return errors;
1449 }
1450
1425 -int rrdlabels_unittest_double_check() {
1451 +static int rrdlabels_unittest_expect_value(RRDLABELS *labels, const char *key, const char *value, RRDLABEL_SRC required_source)
1452 +{
1453 + RRDLABEL_SRC source;
1454 + RRDLABEL *label = rrdlabels_find_label_with_key(labels, key, &source);
1455 + return (!label || strcmp(string2str(label->index.value), value) != 0 || !(source & required_source));
1456 +}
1457 +
1458 +static int rrdlabels_unittest_double_check()
1459 +{
1460 fprintf(stderr, "\n%s() tests\n", __FUNCTION__);
1461
1428 - int errors = 1;
1462 int ret = 0;
1463 RRDLABELS *labels = rrdlabels_create();
1464
1432 - const char *pair = "key1=value1";
1433 -
1434 - struct rrdlabels_unittest_add_a_pair tmp = {
1435 - .pair = pair,
1436 - .expected_name = "key1",
1437 - .expected_value = NULL,
1438 - .errors = 0
1439 - };
1465 + rrdlabels_add(labels, "key1", "value1", RRDLABEL_SRC_CONFIG);
1466 + ret += rrdlabels_unittest_expect_value(labels, "key1", "value1", RRDLABEL_FLAG_NEW);
1467
1441 - fprintf(stderr, "rrdlabels_add_pair(labels, %s) ...\n ", pair);
1442 -
1443 - rrdlabels_add_pair(labels, pair, RRDLABEL_SRC_CONFIG);
1444 - size_t count = rrdlabels_entries(labels);
1445 - fprintf(stderr, "Added one key with \"value1\", entries found %zu\n", count);
1446 - tmp.expected_value = "value1";
1447 - ret = rrdlabels_walkthrough_read(labels, rrdlabels_unittest_add_a_pair_callback, &tmp);
1448 -
1449 - fprintf(stderr, "Adding key with same value \"value1\" (collision check)\n");
1450 - rrdlabels_add_pair(labels, pair, RRDLABEL_SRC_CONFIG);
1451 - count = rrdlabels_entries(labels);
1452 - fprintf(stderr, "Added same key again \"value1\", entries found %zu\n", count);
1468 + rrdlabels_add(labels, "key1", "value2", RRDLABEL_SRC_CONFIG);
1469 + ret += !rrdlabels_unittest_expect_value(labels, "key1", "value2", RRDLABEL_FLAG_OLD);
1470
1454 - ret = rrdlabels_walkthrough_read(labels, rrdlabels_unittest_add_a_pair_callback, &tmp);
1455 -
1456 - // Add same key with different value
1457 - pair = "key1=value2";
1458 - rrdlabels_add_pair(labels, pair, RRDLABEL_SRC_CONFIG);
1459 - count = rrdlabels_entries(labels);
1460 - fprintf(stderr, "Added same key again with \"value2\", entries found %zu\n", count);
1461 -
1462 - tmp.expected_value = "value2";
1463 - ret = rrdlabels_walkthrough_read(labels, rrdlabels_unittest_add_a_pair_callback, &tmp);
1464 -
1465 - fprintf(stderr, "Adding key with same value \"value2\" (collision check)\n");
1466 - rrdlabels_add_pair(labels, pair, RRDLABEL_SRC_CONFIG);
1467 - count = rrdlabels_entries(labels);
1468 - fprintf(stderr, "Added same key again with \"value2\", entries found %zu\n", count);
1469 -
1470 - ret = rrdlabels_walkthrough_read(labels, rrdlabels_unittest_add_a_pair_callback, &tmp);
1471 - errors = tmp.errors;
1472 - if(ret != 1) {
1473 - fprintf(stderr, "failed to get \"%s\" label", "key1");
1474 - errors++;
1475 - }
1476 -
1477 - if(!errors)
1478 - fprintf(stderr, " OK, name='%s' and value='%s'\n", tmp.name, tmp.value?tmp.value:"(null)");
1479 - else
1480 - fprintf(stderr, " FAILED\n");
1471 + ret += (rrdlabels_entries(labels) != 1);
1472
1473 rrdlabels_destroy(labels);
1474
1484 - return errors;
1475 + if (ret)
1476 + fprintf(stderr, "\n%s() tests failed\n", __FUNCTION__);
1477 + return ret;
1478 }
1479
1480 static int rrdlabels_walkthrough_index_read(RRDLABELS *labels, int (*callback)(const char *name, const char *value, RRDLABEL_SRC ls, size_t index, void *data), void *data)
@@ -1514,7 +1507,8 @@ static int unittest_dump_labels(const char *name, const char *value, RRDLABEL_SR
1507 return 1;
1508 }
1509
1517 -int rrdlabels_unittest_migrate_check() {
1510 +static int rrdlabels_unittest_migrate_check()
1511 +{
1512 fprintf(stderr, "\n%s() tests\n", __FUNCTION__);
1513
1514 RRDLABELS *labels1 = NULL;
@@ -1553,38 +1547,43 @@ int rrdlabels_unittest_migrate_check() {
1547 rrdlabels_add(labels1, "key4", "value4", RRDLABEL_SRC_CONFIG); // 4 keys
1548 rrdlabels_walkthrough_index_read(labels1, unittest_dump_labels, "\nlabels1");
1549
1556 - rrdlabels_add(labels2, "key1", "value1_new", RRDLABEL_SRC_CONFIG);
1557 - rrdlabels_add(labels2, "key2", "value2", RRDLABEL_SRC_CONFIG);
1550 rrdlabels_add(labels2, "key0", "value0", RRDLABEL_SRC_CONFIG);
1551 + rrdlabels_add(labels2, "key1", "value1", RRDLABEL_SRC_CONFIG);
1552 + rrdlabels_add(labels2, "key2", "value2", RRDLABEL_SRC_CONFIG);
1553 +
1554 + int rc = 0;
1555 + rc = rrdlabels_unittest_expect_value(labels1, "key1", "value1", RRDLABEL_FLAG_NEW);
1556 + if (rc)
1557 + return rc;
1558 +
1559 rrdlabels_walkthrough_index_read(labels2, unittest_dump_labels, "\nlabels2");
1560
1561 rrdlabels_copy(labels1, labels2); // labels1 should have 5 keys
1562 - rrdlabels_walkthrough_index_read(labels1, unittest_dump_labels, "\nlabels1 after copy from labels2");
1562 + rc = rrdlabels_unittest_expect_value(labels1, "key1", "value1", RRDLABEL_FLAG_OLD);
1563 + if (rc)
1564 + return rc;
1565 +
1566 + rc = rrdlabels_unittest_expect_value(labels1, "key0", "value0", RRDLABEL_FLAG_NEW);
1567 + if (rc)
1568 + return rc;
1569
1570 + rrdlabels_walkthrough_index_read(labels1, unittest_dump_labels, "\nlabels1 after copy from labels2");
1571 entries = rrdlabels_entries(labels1);
1572 +
1573 fprintf(stderr, "labels1 (copied) entries found %zu (should be 5)\n", rrdlabels_entries(labels1));
1574 if (entries != 5)
1575 return 1;
1576
1569 - rrdlabels_add(labels1, "key100", "value100", RRDLABEL_SRC_CONFIG);
1570 - rrdlabels_walkthrough_index_read(labels1, unittest_dump_labels, "\nlabels1 now after key100");
1571 -
1572 - rrdlabels_copy(labels2, labels1); // labels2 should have 6 keys
1573 - entries = rrdlabels_entries(labels1);
1574 -
1575 - fprintf(stderr, "labels2 (copied) entries found %zu (should be 6)\n", rrdlabels_entries(labels1));
1576 -
1577 - rrdlabels_walkthrough_index_read(labels1, unittest_dump_labels, "\nlabels2 after labels1 copy");
1578 -
1579 - rrdlabels_walkthrough_index_read(labels1, unittest_dump_labels, "\nfinal labels1");
1577 + rrdlabels_add(labels1, "key0", "value0", RRDLABEL_SRC_CONFIG);
1578 + rc = rrdlabels_unittest_expect_value(labels1, "key0", "value0", RRDLABEL_FLAG_OLD);
1579
1580 rrdlabels_destroy(labels1);
1581 rrdlabels_destroy(labels2);
1582
1584 - return entries != 6;
1583 + return rc;
1584 }
1585
1587 -int rrdlabels_unittest_check_simple_pattern(RRDLABELS *labels, const char *pattern, bool expected) {
1586 +static int rrdlabels_unittest_check_simple_pattern(RRDLABELS *labels, const char *pattern, bool expected) {
1587 fprintf(stderr, "rrdlabels_match_simple_pattern(labels, \"%s\") ... ", pattern);
1588
1589 bool ret = rrdlabels_match_simple_pattern(labels, pattern);
@@ -1593,7 +1592,7 @@ int rrdlabels_unittest_check_simple_pattern(RRDLABELS *labels, const char *patte
1592 return (ret == expected)?0:1;
1593 }
1594
1596 -int rrdlabels_unittest_simple_pattern() {
1595 +static int rrdlabels_unittest_simple_pattern() {
1596 fprintf(stderr, "\n%s() tests\n", __FUNCTION__);
1597
1598 int errors = 0;