-
Notifications
You must be signed in to change notification settings - Fork 77
Check if server's and client's versions are compatible #206
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
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7b6bac8
Check versions compatibility during init
tellet-q d1c5141
Add tests
tellet-q f734d58
Fetch server version using futures
tellet-q 3b53ff7
Do async health check in sync context
timvisee 1d1c10e
Check compatibility in temporary thread
timvisee c89c142
create dedicated client to avoid 'Service was not ready'
agourlay a42ba77
fmt
agourlay a7c4cbb
Use semver crate
tellet-q 6460e0f
Fix linter
tellet-q 6033def
Merge branch 'dev' into check-version-on-init
tellet-q 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| use std::error::Error; | ||
| use std::fmt; | ||
|
|
||
| #[derive(Debug, Clone)] | ||
| pub struct Version { | ||
| pub major: u32, | ||
| pub minor: u32, | ||
| } | ||
|
|
||
| impl Version { | ||
| pub fn parse(version: &str) -> Result<Version, VersionParseError> { | ||
| if version.is_empty() { | ||
| return Err(VersionParseError::EmptyVersion); | ||
| } | ||
| let parts: Vec<&str> = version.split('.').collect(); | ||
| if parts.len() < 2 { | ||
| return Err(VersionParseError::InvalidFormat(version.to_string())); | ||
| } | ||
|
|
||
| let major = parts[0] | ||
| .parse::<u32>() | ||
| .map_err(|_| VersionParseError::InvalidFormat(version.to_string()))?; | ||
| let minor = parts[1] | ||
| .parse::<u32>() | ||
| .map_err(|_| VersionParseError::InvalidFormat(version.to_string()))?; | ||
|
|
||
| Ok(Version { major, minor }) | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
| pub enum VersionParseError { | ||
| EmptyVersion, | ||
| InvalidFormat(String), | ||
| } | ||
|
|
||
| impl fmt::Display for VersionParseError { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| match self { | ||
| VersionParseError::EmptyVersion => write!(f, "Version is empty"), | ||
| VersionParseError::InvalidFormat(version) => { | ||
| write!( | ||
| f, | ||
| "Unable to parse version, expected format: x.y[.z], found: {}", | ||
tellet-q marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| version | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Error for VersionParseError {} | ||
|
|
||
| pub fn is_compatible(client_version: Option<&str>, server_version: Option<&str>) -> bool { | ||
| if client_version.is_none() || server_version.is_none() { | ||
| println!( | ||
| "Unable to compare versions, client_version: {:?}, server_version: {:?}", | ||
| client_version, server_version | ||
| ); | ||
| return false; | ||
| } | ||
|
|
||
| let client_version = client_version.unwrap(); | ||
| let server_version = server_version.unwrap(); | ||
|
|
||
| if client_version == server_version { | ||
| return true; | ||
| } | ||
|
|
||
| match ( | ||
| Version::parse(client_version), | ||
| Version::parse(server_version), | ||
| ) { | ||
| (Ok(client), Ok(server)) => { | ||
| let major_dif = (client.major as i32 - server.major as i32).abs(); | ||
| if major_dif >= 1 { | ||
| return false; | ||
| } | ||
| (client.minor as i32 - server.minor as i32).abs() <= 1 | ||
| } | ||
| (Err(e), _) | (_, Err(e)) => { | ||
| println!("Unable to compare versions: {}", e); | ||
| false | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn test_is_compatible() { | ||
| let test_cases = vec![ | ||
| (Some("1.9.3.dev0"), Some("2.8.1.dev12-something"), false), | ||
| (Some("1.9"), Some("2.8"), false), | ||
| (Some("1"), Some("2"), false), | ||
| (Some("1.9.0"), Some("2.9.0"), false), | ||
| (Some("1.1.0"), Some("1.2.9"), true), | ||
| (Some("1.2.7"), Some("1.1.8.dev0"), true), | ||
| (Some("1.2.1"), Some("1.2.29"), true), | ||
| (Some("1.2.0"), Some("1.2.0"), true), | ||
| (Some("1.2.0"), Some("1.4.0"), false), | ||
| (Some("1.4.0"), Some("1.2.0"), false), | ||
| (Some("1.9.0"), Some("3.7.0"), false), | ||
| (Some("3.0.0"), Some("1.0.0"), false), | ||
| (None, Some("1.0.0"), false), | ||
| (Some("1.0.0"), None, false), | ||
| (None, None, false), | ||
| ]; | ||
|
|
||
| for (client_version, server_version, expected_result) in test_cases { | ||
| let result = is_compatible(client_version, server_version); | ||
| assert_eq!( | ||
| result, expected_result, | ||
| "Failed for client: {:?}, server: {:?}", | ||
| client_version, server_version | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_version_parse_errors() { | ||
| let test_cases = vec![ | ||
| ("1", VersionParseError::InvalidFormat("1".to_string())), | ||
| ("1.", VersionParseError::InvalidFormat("1.".to_string())), | ||
| (".1", VersionParseError::InvalidFormat(".1".to_string())), | ||
| (".1.", VersionParseError::InvalidFormat(".1.".to_string())), | ||
| ( | ||
| "1.a.1", | ||
| VersionParseError::InvalidFormat("1.a.1".to_string()), | ||
| ), | ||
| ( | ||
| "a.1.1", | ||
| VersionParseError::InvalidFormat("a.1.1".to_string()), | ||
| ), | ||
| ("", VersionParseError::EmptyVersion), | ||
| ]; | ||
|
|
||
| for (input, expected_error) in test_cases { | ||
| let result = Version::parse(input); | ||
| assert!(result.is_err()); | ||
| assert_eq!(result.unwrap_err().to_string(), expected_error.to_string()); | ||
| } | ||
| } | ||
| } | ||
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.