Skip to content

Commit

Permalink
Some more fixes, test redisxGetInfo(), cure overzealous error propaga…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
attipaci committed Dec 11, 2024
1 parent c13a13b commit ae01f8d
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 16 deletions.
8 changes: 5 additions & 3 deletions include/redisx.h
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,7 @@ int redisxSetUser(Redis *redis, const char *username);
int redisxSetPassword(Redis *redis, const char *passwd);
int redisxSelectDB(Redis *redis, int idx);
int redisxSetProtocol(Redis *redis, enum redisx_protocol protocol);
enum redisx_protocol redisxGetProtocol(Redis *redis);
XLookupTable *redisxGetInfo(Redis *redis, const char *parameter);
RESP *redisxGetHelloData(Redis *redis);


Redis *redisxInit(const char *server);
Redis *redisxInitSentinel(const char *serviceName, const RedisServer *serverList, int nServers);
Expand All @@ -369,7 +367,11 @@ void redisxDestroy(Redis *redis);
int redisxConnect(Redis *redis, boolean usePipeline);
void redisxDisconnect(Redis *redis);
int redisxReconnect(Redis *redis, boolean usePipeline);

int redisxPing(Redis *redis, const char *message);
enum redisx_protocol redisxGetProtocol(Redis *redis);
XLookupTable *redisxGetInfo(Redis *redis, const char *parameter);
RESP *redisxGetHelloData(Redis *redis);

boolean redisxIsConnected(Redis *redis);
boolean redisxHasPipeline(Redis *redis);
Expand Down
6 changes: 4 additions & 2 deletions src/redisx-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ static int rReadToken(ClientPrivate *cp, char *buf, int length) {
int status = rReadChunkAsync(cp);
if(status) {
pthread_mutex_unlock(&cp->readLock);
return x_trace(fn, NULL, status);
if(cp->isEnabled) return x_trace(fn, NULL, status);
return status;
}
}

Expand All @@ -161,7 +162,8 @@ static int rReadToken(ClientPrivate *cp, char *buf, int length) {
// If client was disabled before we got everything, then simply return X_NO_SERVICE
if(!cp->isEnabled) {
*buf = '\0';
return x_trace(fn, NULL, X_NO_SERVICE);
if(L > 0) return x_trace(fn, NULL, X_NO_SERVICE);
return X_NO_SERVICE;
}

// From here on L is the number of characters actually buffered.
Expand Down
13 changes: 9 additions & 4 deletions src/redisx-net.c
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,9 @@ int rConnectClient(Redis *redis, enum redisx_channel channel) {
/**
* Initializes the Redis client library, and sets the hostname or IP address for the Redis server.
*
* \param server Server host name or numeric IP address, e.g. "127.0.0.1"
* \param server Server host name or numeric IP address, e.g. "127.0.0.1". The string will
* be copied, not referenced, for the internal configuration, such that the
* string passed may be destroyed freely after the call.
*
* \return X_SUCCESS or
* X_FAILURE if the IP address is invalid.
Expand Down Expand Up @@ -872,9 +874,12 @@ int redisxValidateSentinel(const char *serviceName, const RedisServer *serverLis
* sentinel node connection timeout.
*
* @param serviceName The service name as registered in the Sentinel server configuration.
* @param serverList An set of Sentinel servers to use to dynamically find the current master. A
* copy of the supplied name will be used, so the argument passed can be
* freely destroyed after the call.
* The supplied name will be copied, not referenced, so that the value
* passed may be freely destroyed after the call.
* @param serverList An set of Sentinel servers to use to dynamically find the current master.
* The list itself and its contents are not referenced. Instead a deep copy
* will be made of it, so the list that was pased can be freely destroyed
* after the call.
* @param nServers The number of servers in the list
* @return X_SUCCESS (0) if successful, or else an error code <0.
*
Expand Down
16 changes: 10 additions & 6 deletions src/redisx.c
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,7 @@ RESP *redisxGetHelloData(Redis *redis) {
* @param parameter Optional parameter to pass with INFO, or NULL.
* @return a newly created lookup table with the string key/value pairs of the
* response from the Redis server, or NULL if there was an error.
* The caller should destroy the lookup table after using it.
*
* @sa redisxGetHelloData()
*/
Expand All @@ -741,7 +742,7 @@ XLookupTable *redisxGetInfo(Redis *redis, const char *parameter) {
XStructure *s;
XLookupTable *lookup;
RESP *reply;
char *str;
char *line;
int status;

reply = redisxRequest(redis, "INFO", parameter, NULL, NULL, &status);
Expand All @@ -752,13 +753,16 @@ XLookupTable *redisxGetInfo(Redis *redis, const char *parameter) {
s = xCreateStruct();

// Go line by line...
str = strtok((char *) reply->value, "\n");
line = strtok((char *) reply->value, "\n");

// Parse key:value lines into a structure.
while(str) {
const char *tok = strtok(str, ":");
if(tok) xSetField(s, xCreateStringField(tok, xStringCopyOf(strtok(NULL, "\n"))));
str = strtok(NULL, "\n");
while(line) {
char *sep = strchr(line, ':');
if(sep) {
*sep = '\0';
xSetField(s, xCreateStringField(line, xStringCopyOf(sep + 1)));
}
line = strtok(NULL, "\n");
}

lookup = xCreateLookup(s, FALSE);
Expand Down
3 changes: 2 additions & 1 deletion test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ include ../config.mk
all: tests run

.PHONY: tests
tests: $(BIN)/test-ping $(BIN)/test-hello $(BIN)/test-tab
tests: $(BIN)/test-ping $(BIN)/test-info $(BIN)/test-hello $(BIN)/test-tab

.PHONY: run
run: tests
$(BIN)/test-info
$(BIN)/test-ping
$(BIN)/test-hello
$(BIN)/test-tab
Expand Down
49 changes: 49 additions & 0 deletions test/src/test-info.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* @file
*
* @date Created on Dec 8, 2024
* @author Attila Kovacs
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "redisx.h"
#include "xchange.h"

int main() {
Redis *redis = redisxInit("localhost");
XLookupTable *info;
XField *role;

xSetDebug(TRUE);
//redisxSetVerbose(TRUE);
//redisxDebugTraffic(TRUE);

if(redisxConnect(redis, FALSE) < 0) {
perror("ERROR! connect");
return 1;
}

info = redisxGetInfo(redis, "replication");
if(!info) {
perror("ERROR! NULL info");
return 1;
}

role = xLookupField(info, "role");
if(!role) {
fprintf(stderr, "ERROR! role not found (count = %ld)", xLookupCount(info));
return 1;
}

xDestroyLookup(info);
redisxDisconnect(redis);
redisxDestroy(redis);

fprintf(stderr, "OK\n");

return 0;

}

0 comments on commit ae01f8d

Please sign in to comment.