Skip to content

Commit

Permalink
Modified tests to dynamically pull the replicas number, and added mor…
Browse files Browse the repository at this point in the history
…e replicas to cluster config

Signed-off-by: Muhammad Awawdi <[email protected]>

updated number of replicas for the cluster, dropped the part for CMD as its irrelevant

Signed-off-by: Muhammad Awawdi <[email protected]>

prettier

Signed-off-by: Muhammad Awawdi <[email protected]>
  • Loading branch information
Muhammad-awawdi-amazon committed Nov 17, 2024
1 parent c482ea9 commit 5469747
Showing 1 changed file with 75 additions and 7 deletions.
82 changes: 75 additions & 7 deletions node/tests/GlideClusterClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ describe("GlideClusterClient", () => {
getServerVersion,
)
: // setting replicaCount to 1 to facilitate tests routed to replicas
await ValkeyCluster.createCluster(true, 3, 1, getServerVersion);
await ValkeyCluster.createCluster(true, 3, 2, getServerVersion);
}, 40000);

afterEach(async () => {
Expand Down Expand Up @@ -1968,13 +1968,59 @@ describe("GlideClusterClient", () => {
},
);
describe("GlideClusterClient - AZAffinity Read Strategy Test", () => {
async function getNumberOfReplicas(
client: GlideClusterClient,
): Promise<number> {
const replicationInfo = await client.customCommand([
"INFO",
"REPLICATION",
]);

if (Array.isArray(replicationInfo)) {
// Handle array response from cluster (CME Mode)
let totalReplicas = 0;
for (const node of replicationInfo) {
const nodeInfo = node as {
key: string;
value: string | string[] | null;
};

if (typeof nodeInfo.value === "string") {
const lines = nodeInfo.value.split(/\r?\n/);
const connectedReplicasLine = lines.find(
(line) =>
line.startsWith("connected_slaves:") ||
line.startsWith("connected_replicas:"),
);

if (connectedReplicasLine) {
const parts = connectedReplicasLine.split(":");
const numReplicas = parseInt(parts[1], 10);
if (!isNaN(numReplicas)) {
// Sum up replicas from each primary node
totalReplicas += numReplicas;
}
}
}
}

if (totalReplicas > 0) {
return totalReplicas;
}
throw new Error(
"Could not find replica information in any node's response",
);
}
throw new Error(
"Unexpected response format from INFO REPLICATION command",
);
}

it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
"should route GET commands to all replicas with the same AZ using protocol %p",
async (protocol) => {
const az = "us-east-1a";
const n_replicas = 6;
const GET_CALLS = 3 * n_replicas;
const get_cmdstat = `calls=${GET_CALLS / n_replicas}`;
const GET_CALLS_PER_REPLICA = 3;

let client_for_config_set;
let client_for_testing_az;
Expand Down Expand Up @@ -2006,6 +2052,19 @@ describe("GlideClusterClient", () => {
{ route: "allNodes" },
);

// Retrieve the number of replicas dynamically
const n_replicas = await getNumberOfReplicas(
client_for_config_set,
);
console.log(n_replicas);
if (n_replicas === 0) {
throw new Error(
"No replicas found in the cluster. Test requires at least one replica.",
);
}
const GET_CALLS = GET_CALLS_PER_REPLICA * n_replicas;
const get_cmdstat = `calls=${GET_CALLS_PER_REPLICA}`;

// Stage 2: Create AZ affinity client and verify configuration
client_for_testing_az =
await GlideClusterClient.createClient(
Expand Down Expand Up @@ -2064,7 +2123,7 @@ describe("GlideClusterClient", () => {
// Stage 4: Verify GET commands were routed correctly
const info_result =
await client_for_testing_az.customCommand(
["INFO", "COMMANDSTATS"],
["INFO", "ALL"], // Get both replication and commandstats info
{ route: "allNodes" },
);

Expand All @@ -2077,11 +2136,20 @@ describe("GlideClusterClient", () => {
};
const infoStr =
nodeInfo.value?.toString() || "";
return infoStr.includes(get_cmdstat);

// Check if this is a replica node AND it has the expected number of GET calls
const isReplicaNode =
infoStr.includes("role:slave") ||
infoStr.includes("role:replica");

return (
isReplicaNode &&
infoStr.includes(get_cmdstat)
);
},
).length;

expect(matching_entries_count).toBe(n_replicas);
expect(matching_entries_count).toBe(n_replicas); // Should expect 6 as the cluster was created with 3 primary and 2 replicas, totalling 6 replica nodes
} else {
throw new Error(
"Unexpected response format from INFO command",
Expand Down

0 comments on commit 5469747

Please sign in to comment.