Jest Load Balancing Test Suite
Load balancing is a crucial technique for distributing workload across multiple servers or resources to prevent overload and ensure high availability. This challenge asks you to implement a Jest test suite that verifies the correct behavior of a simple load balancing function. The goal is to ensure your load balancer distributes requests evenly and handles potential server failures gracefully.
Problem Description
You are provided with a loadBalancer function (described below) and a set of mock server functions. Your task is to create a Jest test suite that thoroughly tests the loadBalancer function under various scenarios, including normal operation, server failures, and edge cases. The test suite should verify that the load balancer distributes requests across available servers in a round-robin fashion and handles server unavailability without crashing.
The loadBalancer function takes an array of server functions as input. It distributes incoming requests to these servers in a round-robin fashion. If a server fails (throws an error), the load balancer should skip that server and continue distributing requests to the remaining healthy servers. The load balancer should also handle the case where no servers are available.
loadBalancer Function Signature:
async function loadBalancer(servers: (() => Promise<string>)[]): Promise<string>
Key Requirements:
- Round-Robin Distribution: The load balancer should distribute requests to servers in a cyclical order.
- Server Failure Handling: The load balancer should gracefully handle server failures (errors thrown by server functions) and continue distributing requests to available servers.
- No Servers Available: The load balancer should handle the case where the input array of servers is empty or all servers fail.
- Asynchronous Operations: The server functions are asynchronous (return Promises). The load balancer must correctly handle these asynchronous operations.
- Test Coverage: The test suite should cover all the key requirements and edge cases.
Expected Behavior:
- When all servers are healthy, the load balancer should distribute requests evenly across them.
- When a server fails, the load balancer should skip that server and continue distributing requests to the remaining healthy servers.
- When no servers are available, the load balancer should either return a specific error message or throw an appropriate error.
Examples
Example 1:
Input: [() => Promise.resolve("Server 1"), () => Promise.resolve("Server 2"), () => Promise.resolve("Server 3")]
Output: ["Server 1", "Server 2", "Server 3", "Server 1", "Server 2", "Server 3"]
Explanation: The load balancer distributes requests in a round-robin fashion across the three servers.
Example 2:
Input: [() => Promise.resolve("Server 1"), () => () => Promise.reject("Server 2 failed"), () => Promise.resolve("Server 3")]
Output: ["Server 1", "Server 3", "Server 1", "Server 3", "Server 1", "Server 3"]
Explanation: Server 2 fails, so the load balancer only distributes requests to Server 1 and Server 3.
Example 3:
Input: []
Output: "No servers available"
Explanation: The input array is empty, so the load balancer returns an error message.
Constraints
- The
loadBalancerfunction should be implemented in a way that avoids unnecessary complexity. - The test suite should be well-structured and easy to understand.
- The test suite should aim for high test coverage.
- The
loadBalancerfunction should not take more than 10ms to execute in normal conditions. - Server functions are expected to return a string via a Promise.
Notes
- You are free to use any Jest features and mocking techniques to create your test suite.
- Consider using
async/awaitfor cleaner asynchronous testing. - Think about how to effectively mock server failures to test the error handling logic.
- The
loadBalancerfunction is not provided; you will need to implement it as part of your solution. Your test suite should test your implementation ofloadBalancer. - The expected output in the examples is a sequence of strings returned by the servers. Your tests should verify this sequence.