Skip to content

Commit

Permalink
Don't throw error when creating dotmember metric with CounterIncremen…
Browse files Browse the repository at this point in the history
…t kind
  • Loading branch information
SanderMertens committed Oct 3, 2023
1 parent fd9fd1e commit 07f82bc
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 8 deletions.
6 changes: 3 additions & 3 deletions flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -29276,13 +29276,13 @@ ecs_entity_t ecs_metric_init(
goto error;
}

if (kind == EcsCounterIncrement && !desc->member) {
if (kind == EcsCounterIncrement && !desc->member && !desc->dotmember) {
ecs_err("CounterIncrement can only be used in combination with member");
goto error;
}

if (kind == EcsCounterId && desc->member) {
ecs_err("CounterIncrement cannot be used in combination with member");
if (kind == EcsCounterId && (desc->member || desc->dotmember)) {
ecs_err("CounterId cannot be used in combination with member");
goto error;
}

Expand Down
6 changes: 3 additions & 3 deletions src/addons/metrics.c
Original file line number Diff line number Diff line change
Expand Up @@ -728,13 +728,13 @@ ecs_entity_t ecs_metric_init(
goto error;
}

if (kind == EcsCounterIncrement && !desc->member) {
if (kind == EcsCounterIncrement && !desc->member && !desc->dotmember) {
ecs_err("CounterIncrement can only be used in combination with member");
goto error;
}

if (kind == EcsCounterId && desc->member) {
ecs_err("CounterIncrement cannot be used in combination with member");
if (kind == EcsCounterId && (desc->member || desc->dotmember)) {
ecs_err("CounterId cannot be used in combination with member");
goto error;
}

Expand Down
4 changes: 3 additions & 1 deletion test/addons/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -1577,7 +1577,9 @@
"id_count",
"id_target_count",
"metric_instance_has_doc_name",
"metric_nested_member"
"metric_nested_member",
"metric_nested_member_counter",
"metric_nested_member_counter_increment"
]
}, {
"id": "Alerts",
Expand Down
158 changes: 158 additions & 0 deletions test/addons/src/Metrics.c
Original file line number Diff line number Diff line change
Expand Up @@ -1889,3 +1889,161 @@ void Metrics_metric_nested_member(void) {

ecs_fini(world);
}

void Metrics_metric_nested_member_counter(void) {
typedef struct {
float x, y;
} Point;

typedef struct {
float dummy;
Point position;
} Position;

ecs_world_t *world = ecs_init();
ECS_IMPORT(world, FlecsMetrics);

ECS_COMPONENT(world, Point);
ECS_COMPONENT(world, Position);

ecs_struct(world, {
.entity = ecs_id(Point),
.members = {
{ "x", ecs_id(ecs_f32_t) },
{ "y", ecs_id(ecs_f32_t) },
}
});

ecs_struct(world, {
.entity = ecs_id(Position),
.members = {
{ "dummy", ecs_id(ecs_f32_t) },
{ "position", ecs_id(Point) },
}
});

ecs_entity_t m = ecs_metric(world, {
.entity = ecs_entity(world, { .name = "metrics.position_y" }),
.id = ecs_id(Position),
.dotmember = "position.y",
.kind = EcsCounter
});

test_assert(m != 0);

ecs_entity_t e1 = ecs_new_entity(world, "e1");
ecs_set(world, e1, Position, {10, {20, 30}});

ecs_progress(world, 0);

ecs_iter_t it = ecs_children(world, m);
test_bool(true, ecs_children_next(&it));
test_uint(it.count, 1);
{
ecs_entity_t e = it.entities[0];
test_assert(ecs_has_pair(world, e, EcsMetric, EcsCounter));
const EcsMetricSource *src = ecs_get(world, e, EcsMetricSource);
const EcsMetricValue *inst = ecs_get(world, e, EcsMetricValue);
test_assert(src != NULL);
test_assert(inst != NULL);
test_uint(src->entity, e1);
test_int(inst->value, 30);

test_str(ecs_doc_get_name(world, e), "e1");
}

test_bool(false, ecs_children_next(&it));

ecs_fini(world);
}

void Metrics_metric_nested_member_counter_increment(void) {
typedef struct {
float x, y;
} Point;

typedef struct {
float dummy;
Point position;
} Position;

ecs_world_t *world = ecs_init();
ECS_IMPORT(world, FlecsMetrics);

ECS_COMPONENT(world, Point);
ECS_COMPONENT(world, Position);

ecs_struct(world, {
.entity = ecs_id(Point),
.members = {
{ "x", ecs_id(ecs_f32_t) },
{ "y", ecs_id(ecs_f32_t) },
}
});

ecs_struct(world, {
.entity = ecs_id(Position),
.members = {
{ "dummy", ecs_id(ecs_f32_t) },
{ "position", ecs_id(Point) },
}
});

ecs_entity_t m = ecs_metric(world, {
.entity = ecs_entity(world, { .name = "metrics.position_y" }),
.id = ecs_id(Position),
.dotmember = "position.y",
.kind = EcsCounterIncrement
});

test_assert(m != 0);

ecs_entity_t e1 = ecs_new_entity(world, "e1");
ecs_set(world, e1, Position, {10, {20, 30}});

ecs_progress(world, 1);

{
ecs_iter_t it = ecs_children(world, m);
test_bool(true, ecs_children_next(&it));
test_uint(it.count, 1);
{
ecs_entity_t e = it.entities[0];
test_assert(ecs_has_pair(world, e, EcsMetric, EcsCounterIncrement));
const EcsMetricSource *src = ecs_get(world, e, EcsMetricSource);
const EcsMetricValue *inst = ecs_get(world, e, EcsMetricValue);
test_assert(src != NULL);
test_assert(inst != NULL);
test_uint(src->entity, e1);
test_int(inst->value, 30);

test_str(ecs_doc_get_name(world, e), "e1");
}

test_bool(false, ecs_children_next(&it));
}

ecs_progress(world, 1);

{
ecs_iter_t it = ecs_children(world, m);
test_bool(true, ecs_children_next(&it));
test_uint(it.count, 1);
{
ecs_entity_t e = it.entities[0];
test_assert(ecs_has_pair(world, e, EcsMetric, EcsCounterIncrement));
const EcsMetricSource *src = ecs_get(world, e, EcsMetricSource);
const EcsMetricValue *inst = ecs_get(world, e, EcsMetricValue);
test_assert(src != NULL);
test_assert(inst != NULL);
test_uint(src->entity, e1);
test_int(inst->value, 60);

test_str(ecs_doc_get_name(world, e), "e1");
}

test_bool(false, ecs_children_next(&it));
}

ecs_fini(world);
}
12 changes: 11 additions & 1 deletion test/addons/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1507,6 +1507,8 @@ void Metrics_id_count(void);
void Metrics_id_target_count(void);
void Metrics_metric_instance_has_doc_name(void);
void Metrics_metric_nested_member(void);
void Metrics_metric_nested_member_counter(void);
void Metrics_metric_nested_member_counter_increment(void);

// Testsuite 'Alerts'
void Alerts_one_active_alert(void);
Expand Down Expand Up @@ -7341,6 +7343,14 @@ bake_test_case Metrics_testcases[] = {
{
"metric_nested_member",
Metrics_metric_nested_member
},
{
"metric_nested_member_counter",
Metrics_metric_nested_member_counter
},
{
"metric_nested_member_counter_increment",
Metrics_metric_nested_member_counter_increment
}
};

Expand Down Expand Up @@ -7727,7 +7737,7 @@ static bake_test_suite suites[] = {
"Metrics",
NULL,
NULL,
28,
30,
Metrics_testcases
},
{
Expand Down

0 comments on commit 07f82bc

Please sign in to comment.