Skip to content

Commit 8c2231e

Browse files
authored
Merge pull request #894 from nats-io/fix_893
[FIXED] MicroServices: statistics error always incremented
2 parents d4a07ff + a4f3a33 commit 8c2231e

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/micro_endpoint.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,8 @@ _handle_request(natsConnection *nc, natsSubscription *sub, natsMsg *msg, void *c
203203
full_s = stats->ProcessingTimeNanoseconds / 1000000000;
204204
stats->ProcessingTimeSeconds += full_s;
205205
stats->ProcessingTimeNanoseconds -= full_s * 1000000000;
206-
_update_last_error(ep, err);
206+
if (err != NULL)
207+
_update_last_error(ep, err);
207208
micro_unlock_endpoint(ep);
208209

209210
microError_Destroy(err);

test/test.c

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34528,7 +34528,15 @@ static microError *
3452834528
_microHandleRequestNoisy42(microRequest *req)
3452934529
{
3453034530
if ((rand() % 10) == 0)
34531+
{
34532+
struct threadArg *arg = (struct threadArg*) microService_GetState(req->Service);
34533+
34534+
natsMutex_Lock(arg->m);
34535+
arg->sum++;
34536+
natsMutex_Unlock(arg->m);
34537+
3453134538
return micro_Errorf("Unexpected error!");
34539+
}
3453234540

3453334541
// Happy Path.
3453434542
// Random delay between 5-10ms
@@ -35169,6 +35177,7 @@ void test_MicroBasics(void)
3516935177
nats_JSON *md = NULL;
3517035178
int num_requests = 0;
3517135179
int num_errors = 0;
35180+
char *lastErr = NULL;
3517235181
int n;
3517335182
nats_JSON **array;
3517435183
int array_len;
@@ -35260,6 +35269,7 @@ void test_MicroBasics(void)
3526035269
if (s == NATS_TIMEOUT)
3526135270
{
3526235271
testCond(i == NUM_MICRO_SERVICES);
35272+
nats_clearLastError();
3526335273
break;
3526435274
}
3526535275
testCond(NATS_OK == s);
@@ -35338,6 +35348,7 @@ void test_MicroBasics(void)
3533835348
if (s == NATS_TIMEOUT)
3533935349
{
3534035350
testCond(i == NUM_MICRO_SERVICES);
35351+
nats_clearLastError();
3534135352
break;
3534235353
}
3534335354
testCond(NATS_OK == s);
@@ -35372,6 +35383,7 @@ void test_MicroBasics(void)
3537235383
if (s == NATS_TIMEOUT)
3537335384
{
3537435385
testCond(i == NUM_MICRO_SERVICES);
35386+
nats_clearLastError();
3537535387
break;
3537635388
}
3537735389
testCond(NATS_OK == s);
@@ -35399,6 +35411,14 @@ void test_MicroBasics(void)
3539935411
testCond(NATS_OK == s);
3540035412
num_errors += n;
3540135413

35414+
test("Ensure second endpoint last_error is set if num_errors is positive: ");
35415+
lastErr = NULL;
35416+
s = nats_JSONGetStr(array[1], "last_error", &lastErr);
35417+
testCond((s == NATS_OK) && (((n == 0) && nats_IsStringEmpty(lastErr)) ||
35418+
((n > 0) && (strcmp(lastErr, "Unexpected error!") == 0))));
35419+
free(lastErr);
35420+
lastErr = NULL;
35421+
3540235422
test("Ensure second endpoint has average_processing_time as a positive number: ");
3540335423
int64_t avg = 0;
3540435424
s = nats_JSONGetLong(array[1], "average_processing_time", &avg);
@@ -35411,7 +35431,11 @@ void test_MicroBasics(void)
3541135431
test("Check that STATS total request counts add up (50): ");
3541235432
testCond(num_requests == 50);
3541335433
test("Check that STATS total error count is positive, depends on how many instances: ");
35414-
testCond(num_errors > 0);
35434+
natsMutex_Lock(arg.m);
35435+
// The arg.sum value is incremented when the service handler returns an error.
35436+
s = (arg.sum == num_errors ? NATS_OK : NATS_ERR);
35437+
natsMutex_Unlock(arg.m);
35438+
testCond(s == NATS_OK);
3541535439

3541635440
natsSubscription_Destroy(sub);
3541735441
natsInbox_Destroy(inbox);

0 commit comments

Comments
 (0)