-
Notifications
You must be signed in to change notification settings - Fork 57
Add retry support #1531
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add retry support #1531
Changes from 3 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f3f79a4
Add retry support for the client
niveathika 155dad5
Add testcases
niveathika e020868
Add documentation
niveathika 42119eb
Validate retry configs
niveathika 69d43b0
Change all interval to decimal
niveathika 8490ad3
Remove unnecessary test
niveathika 2c27a01
Update docs
niveathika 5f148cc
Simplify testcases
niveathika 5831aa6
Fix doc comments
niveathika ebe3428
Streamline function signature
niveathika File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,300 @@ | ||
| // Copyright (c) 2026, WSO2 LLC. (http://www.wso2.com). | ||
| // | ||
| // WSO2 LLC. licenses this file to you under the Apache License, | ||
| // Version 2.0 (the "License"); you may not use this file except | ||
| // in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| import ballerina/test; | ||
| import ballerina/time; | ||
|
|
||
| // Test configuration with retry enabled | ||
| ClientConfiguration retryConfig = { | ||
| protocol: FTP, | ||
| host: "127.0.0.1", | ||
| port: 21212, | ||
| auth: {credentials: {username: "wso2", password: "wso2123"}}, | ||
| userDirIsRoot: false, | ||
| retryConfig: { | ||
| count: 3, | ||
| interval: 0.5, // 500ms initial interval for faster tests | ||
| backOffFactor: 2.0, | ||
| maxWaitInterval: 5.0 | ||
| } | ||
| }; | ||
|
|
||
| // Test configuration with custom retry settings | ||
| ClientConfiguration customRetryConfig = { | ||
| protocol: FTP, | ||
| host: "127.0.0.1", | ||
| port: 21212, | ||
| auth: {credentials: {username: "wso2", password: "wso2123"}}, | ||
| userDirIsRoot: false, | ||
| retryConfig: { | ||
| count: 2, | ||
| interval: 0.2, | ||
| backOffFactor: 1.5, | ||
| maxWaitInterval: 1.0 | ||
| } | ||
| }; | ||
|
|
||
| Client? retryClientEp = (); | ||
| Client? customRetryClientEp = (); | ||
|
|
||
| @test:BeforeSuite | ||
| function initRetryTestEnvironment() returns error? { | ||
| retryClientEp = check new (retryConfig); | ||
| customRetryClientEp = check new (customRetryConfig); | ||
| } | ||
niveathika marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Test: Successful getBytes with retry config (no retry needed) | ||
| @test:Config {} | ||
| function testGetBytesWithRetryConfig_Success() returns error? { | ||
| string testPath = "/home/in/retry/test1.txt"; | ||
|
|
||
| byte[]|Error result = (<Client>retryClientEp)->getBytes(testPath); | ||
|
|
||
| if result is Error { | ||
| test:assertFail(msg = "getBytes with retry config should succeed: " + result.message()); | ||
| } else { | ||
| test:assertTrue(result.length() > 0, msg = "Should return non-empty bytes"); | ||
| } | ||
| } | ||
|
|
||
| // Test: Successful getText with retry config (no retry needed) | ||
| @test:Config { | ||
| dependsOn: [testGetBytesWithRetryConfig_Success] | ||
| } | ||
| function testGetTextWithRetryConfig_Success() returns error? { | ||
| string testPath = "/home/in/retry/test1.txt"; | ||
|
|
||
| string|Error result = (<Client>retryClientEp)->getText(testPath); | ||
|
|
||
| if result is Error { | ||
| test:assertFail(msg = "getText with retry config should succeed: " + result.message()); | ||
| } else { | ||
| test:assertTrue(result.length() > 0, msg = "Should return non-empty text"); | ||
| } | ||
| } | ||
|
|
||
| // Test: Successful getJson with retry config (no retry needed) | ||
| @test:Config { | ||
| dependsOn: [testGetTextWithRetryConfig_Success] | ||
| } | ||
| function testGetJsonWithRetryConfig_Success() returns error? { | ||
| // First, put a JSON file | ||
| string jsonPath = "/home/in/retry/retry-test.json"; | ||
| json testJson = {name: "retry-test", value: 42}; | ||
| check (<Client>retryClientEp)->putJson(jsonPath, testJson); | ||
|
|
||
| json|Error result = (<Client>retryClientEp)->getJson(jsonPath); | ||
|
|
||
| if result is Error { | ||
| test:assertFail(msg = "getJson with retry config should succeed: " + result.message()); | ||
| } else { | ||
| test:assertEquals(result, testJson, msg = "JSON content should match"); | ||
| } | ||
|
|
||
| // Cleanup | ||
| check (<Client>retryClientEp)->delete(jsonPath); | ||
| } | ||
|
|
||
| // Test: Successful getXml with retry config (no retry needed) | ||
| @test:Config { | ||
| dependsOn: [testGetJsonWithRetryConfig_Success] | ||
| } | ||
| function testGetXmlWithRetryConfig_Success() returns error? { | ||
| // First, put an XML file | ||
| string xmlPath = "/home/in/retry/retry-test.xml"; | ||
| xml testXml = xml `<root><item>retry-test</item></root>`; | ||
| check (<Client>retryClientEp)->putXml(xmlPath, testXml); | ||
|
|
||
| xml|Error result = (<Client>retryClientEp)->getXml(xmlPath); | ||
|
|
||
| if result is Error { | ||
| test:assertFail(msg = "getXml with retry config should succeed: " + result.message()); | ||
| } else { | ||
| test:assertEquals(result.toString(), testXml.toString(), msg = "XML content should match"); | ||
| } | ||
niveathika marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Cleanup | ||
| check (<Client>retryClientEp)->delete(xmlPath); | ||
| } | ||
|
|
||
| // Test: Successful getCsv with retry config (no retry needed) | ||
| @test:Config { | ||
| dependsOn: [testGetXmlWithRetryConfig_Success] | ||
| } | ||
| function testGetCsvWithRetryConfig_Success() returns error? { | ||
| // First, put a CSV file | ||
| string csvPath = "/home/in/retry/retry-test.csv"; | ||
| string[][] testCsv = [ | ||
| ["id", "name"], | ||
| ["1", "Alice"], | ||
| ["2", "Bob"] | ||
| ]; | ||
| check (<Client>retryClientEp)->putCsv(csvPath, testCsv); | ||
|
|
||
| string[][]|Error result = (<Client>retryClientEp)->getCsv(csvPath); | ||
|
|
||
| if result is Error { | ||
| test:assertFail(msg = "getCsv with retry config should succeed: " + result.message()); | ||
| } else { | ||
| test:assertEquals(result.length(), 2, msg = "Should return 2 data rows"); | ||
| test:assertEquals(result[0][1], "Alice", msg = "First row name should be Alice"); | ||
| } | ||
|
|
||
| // Cleanup | ||
| check (<Client>retryClientEp)->delete(csvPath); | ||
| } | ||
|
|
||
| // Test: Retry behavior on non-existent file (should fail after retries) | ||
| @test:Config { | ||
| dependsOn: [testGetCsvWithRetryConfig_Success] | ||
| } | ||
| function testGetBytesWithRetry_NonExistentFile() returns error? { | ||
| string nonExistentPath = "/home/in/retry/non-existent-retry-test-file.txt"; | ||
|
|
||
| time:Utc startTime = time:utcNow(); | ||
| byte[]|Error result = (<Client>customRetryClientEp)->getBytes(nonExistentPath); | ||
| time:Utc endTime = time:utcNow(); | ||
|
|
||
| // Should be an error after retries | ||
| test:assertTrue(result is Error, msg = "Should return error for non-existent file"); | ||
|
|
||
| if result is Error { | ||
| // Error message should indicate retry exhaustion | ||
| test:assertTrue(result.message().includes("failed after") || | ||
| result.message().includes("not found"), | ||
| msg = "Error should indicate failure: " + result.message()); | ||
| } | ||
|
|
||
| // With retry config (count=2, interval=0.2, backOffFactor=1.5): | ||
| // Initial attempt + retry 1 (0.2s wait) + retry 2 (0.3s wait) | ||
| // Total minimum: ~0.5s | ||
| decimal elapsedSeconds = <decimal>(endTime[0] - startTime[0]); | ||
| // Allow some tolerance - should take at least some time due to retries | ||
| // Note: The elapsed time check is approximate due to test environment variations | ||
| } | ||
|
|
||
| // Test: Retry behavior on getText with non-existent file | ||
| @test:Config { | ||
| dependsOn: [testGetBytesWithRetry_NonExistentFile] | ||
| } | ||
| function testGetTextWithRetry_NonExistentFile() returns error? { | ||
| string nonExistentPath = "/home/in/retry/non-existent-retry-text.txt"; | ||
|
|
||
| string|Error result = (<Client>customRetryClientEp)->getText(nonExistentPath); | ||
|
|
||
| test:assertTrue(result is Error, msg = "Should return error for non-existent file"); | ||
| } | ||
|
|
||
| // Test: Retry behavior on getJson with non-existent file | ||
| @test:Config { | ||
| dependsOn: [testGetTextWithRetry_NonExistentFile] | ||
| } | ||
| function testGetJsonWithRetry_NonExistentFile() returns error? { | ||
| string nonExistentPath = "/home/in/retry/non-existent-retry.json"; | ||
|
|
||
| json|Error result = (<Client>customRetryClientEp)->getJson(nonExistentPath); | ||
|
|
||
| test:assertTrue(result is Error, msg = "Should return error for non-existent file"); | ||
| } | ||
|
|
||
| // Test: Client without retry config should fail immediately on non-existent file | ||
| @test:Config { | ||
| dependsOn: [testGetJsonWithRetry_NonExistentFile] | ||
| } | ||
| function testGetBytesWithoutRetry_ImmediateFail() returns error? { | ||
| // Use the client without retry config | ||
| string nonExistentPath = "/home/in/retry/non-existent-no-retry.txt"; | ||
|
|
||
| time:Utc startTime = time:utcNow(); | ||
| byte[]|Error result = (<Client>clientEp)->getBytes(nonExistentPath); | ||
| time:Utc endTime = time:utcNow(); | ||
|
|
||
| test:assertTrue(result is Error, msg = "Should return error for non-existent file"); | ||
|
|
||
| // Without retry, should fail quickly (< 1 second) | ||
| decimal elapsedSeconds = <decimal>(endTime[0] - startTime[0]); | ||
| test:assertTrue(elapsedSeconds < 2.0d, | ||
| msg = "Without retry, should fail quickly. Elapsed: " + elapsedSeconds.toString() + "s"); | ||
| } | ||
|
|
||
| // Test: RetryConfig default values | ||
| @test:Config {} | ||
| function testRetryConfigDefaults() { | ||
| RetryConfig defaultConfig = {}; | ||
|
|
||
| test:assertEquals(defaultConfig.count, 3, msg = "Default count should be 3"); | ||
| test:assertEquals(defaultConfig.interval, 1.0d, msg = "Default interval should be 1.0"); | ||
| test:assertEquals(defaultConfig.backOffFactor, 2.0, msg = "Default backOffFactor should be 2.0"); | ||
| test:assertEquals(defaultConfig.maxWaitInterval, 30.0d, msg = "Default maxWaitInterval should be 30.0"); | ||
| } | ||
|
|
||
| // Test: Client creation with minimal retry config | ||
| @test:Config {} | ||
| function testClientWithMinimalRetryConfig() returns error? { | ||
| ClientConfiguration minimalRetryConf = { | ||
| protocol: FTP, | ||
| host: "127.0.0.1", | ||
| port: 21212, | ||
| auth: {credentials: {username: "wso2", password: "wso2123"}}, | ||
| retryConfig: {} // Use all defaults | ||
| }; | ||
|
|
||
| Client minimalClient = check new (minimalRetryConf); | ||
|
|
||
| // Should be able to perform operations | ||
| string testPath = "/home/in/test1.txt"; | ||
| byte[]|Error result = minimalClient->getBytes(testPath); | ||
|
|
||
| if result is Error { | ||
| test:assertFail(msg = "Client with minimal retry config should work: " + result.message()); | ||
| } | ||
|
|
||
| check minimalClient->close(); | ||
| } | ||
|
|
||
| // Test: Verify retry doesn't affect write operations | ||
| @test:Config { | ||
| dependsOn: [testClientWithMinimalRetryConfig] | ||
| } | ||
| function testWriteOperationsWithRetryConfig() returns error? { | ||
| string testPath = "/home/in/retry-write-test.txt"; | ||
| string content = "test content for retry write"; | ||
|
|
||
| // Write operations should work normally with retry config | ||
| Error? putResult = (<Client>retryClientEp)->putText(testPath, content); | ||
| test:assertEquals(putResult, (), msg = "putText should succeed with retry config"); | ||
|
|
||
| // Verify content was written | ||
| string|Error getText = (<Client>retryClientEp)->getText(testPath); | ||
| if getText is string { | ||
| test:assertEquals(getText, content, msg = "Content should match after write"); | ||
| } else { | ||
| test:assertFail(msg = "Failed to read written content: " + getText.message()); | ||
| } | ||
|
|
||
| // Cleanup | ||
| check (<Client>retryClientEp)->delete(testPath); | ||
| } | ||
|
|
||
| @test:AfterSuite | ||
| function cleanupRetryTestEnvironment() returns error? { | ||
| if retryClientEp is Client { | ||
| check (<Client>retryClientEp)->close(); | ||
| } | ||
| if customRetryClientEp is Client { | ||
| check (<Client>customRetryClientEp)->close(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.