Skip to content

Commit

Permalink
Merge pull request #636 from nats-io/fix_635
Browse files Browse the repository at this point in the history
[FIXED] natsConnection_GetConnectedUrl() not working during reconnect
  • Loading branch information
kozlovic committed Feb 10, 2023
2 parents 3741f67 + eebff1c commit 835de29
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/conn.c
Original file line number Diff line number Diff line change
Expand Up @@ -3881,7 +3881,8 @@ natsConnection_GetConnectedUrl(natsConnection *nc, char *buffer, size_t bufferSi

buffer[0] = '\0';

if ((nc->status == NATS_CONN_STATUS_CONNECTED) && (nc->cur->url->fullUrl != NULL))
if (((nc->status == NATS_CONN_STATUS_CONNECTED) || (nc->status == NATS_CONN_STATUS_CONNECTING))
&& (nc->cur->url->fullUrl != NULL))
{
if (strlen(nc->cur->url->fullUrl) >= bufferSize)
s = nats_setDefaultError(NATS_INSUFFICIENT_BUFFER);
Expand All @@ -3907,7 +3908,8 @@ natsConnection_GetConnectedServerId(natsConnection *nc, char *buffer, size_t buf

buffer[0] = '\0';

if ((nc->status == NATS_CONN_STATUS_CONNECTED) && (nc->info.id != NULL))
if (((nc->status == NATS_CONN_STATUS_CONNECTED) || (nc->status == NATS_CONN_STATUS_CONNECTING))
&& (nc->info.id != NULL))
{
if (strlen(nc->info.id) >= bufferSize)
s = nats_setDefaultError(NATS_INSUFFICIENT_BUFFER);
Expand Down
29 changes: 29 additions & 0 deletions test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -14971,6 +14971,25 @@ _reconnectTokenHandler(void* closure)
natsMutex_Lock(args->m);
token = args->tokens[args->tokenCallCount % (sizeof(args->tokens)/sizeof(char*))];
args->tokenCallCount++;
if (args->nc != NULL)
{
char buffer[256] = {'\0'};
natsStatus s;

s = natsConnection_GetConnectedUrl(args->nc, buffer, sizeof(buffer));
if (s == NATS_OK)
{
if (((args->tokenCallCount == 2) && (strcmp(buffer, "nats://127.0.0.1:22223") == 0))
|| ((args->tokenCallCount == 3) && (strcmp(buffer, "nats://127.0.0.1:22224") == 0)))
{
args->results[0]++;
buffer[0] = '\0';
s = natsConnection_GetConnectedServerId(args->nc, buffer, sizeof(buffer));
if ((s == NATS_OK) && (strlen(buffer) > 0))
args->results[0]++;
}
}
}
natsMutex_Unlock(args->m);

return token;
Expand Down Expand Up @@ -15031,6 +15050,10 @@ test_ReconnectWithTokenHandler(void)
s = natsConnection_Connect(&nc, opts);
testCond(s == NATS_OK);

natsMutex_Lock(args.m);
args.nc = nc;
natsMutex_Unlock(args.m);

// Stop the server which will trigger the reconnect
_stopServer(serverPid1);
serverPid1 = NATS_INVALID_PID;
Expand Down Expand Up @@ -15059,6 +15082,12 @@ test_ReconnectWithTokenHandler(void)
testCond((s == NATS_OK) && (buffer[0] != '\0')
&& (strcmp(buffer, servers[2]) == 0));

test("ConnectedURL and ServerID OKs during reconnect process: ");
natsMutex_Lock(args.m);
s = ((args.results[0] == 4) ? NATS_OK : NATS_ERR);
natsMutex_Unlock(args.m);
testCond(s == NATS_OK);

natsOptions_Destroy(opts);
natsConnection_Destroy(nc);

Expand Down

0 comments on commit 835de29

Please sign in to comment.