Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/Driver/Impl/WebSocketMethods/BaseConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,17 @@ protected BaseConnection(string addr, TimeSpan connectTimeout = default,
}

Task.Run(async () => { await ReceiveLoop().ConfigureAwait(false); });
try
{
var versionResp =
SendJsonBackJson<WSVersionReq, WSVersionResp>(WSAction.Version, new WSVersionReq(), 0);
TDengineVersion.CheckVersionCompatibility(versionResp.Version);
}
catch (Exception)
{
Close();
throw;
}
}

protected static ulong _GetReqId()
Expand Down
1 change: 1 addition & 0 deletions src/Driver/Impl/WebSocketMethods/Protocol/WSActionReq.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace TDengine.Driver.Impl.WebSocketMethods.Protocol
{
public static class WSAction
{
public const string Version = "version";
public const string Conn = "conn";
public const string Query = "query";
public const string Fetch = "fetch";
Expand Down
6 changes: 6 additions & 0 deletions src/Driver/Impl/WebSocketMethods/Protocol/WSVersionReq.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace TDengine.Driver.Impl.WebSocketMethods.Protocol
{
public class WSVersionReq
{
}
}
19 changes: 19 additions & 0 deletions src/Driver/Impl/WebSocketMethods/Protocol/WSVersionResp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Newtonsoft.Json;

namespace TDengine.Driver.Impl.WebSocketMethods.Protocol
{
public class WSVersionResp : IWSBaseResp
{
[JsonProperty("code")] public int Code { get; set; }

[JsonProperty("message")] public string Message { get; set; }

[JsonProperty("action")] public string Action { get; set; }

[JsonProperty("req_id")] public ulong ReqId { get; set; }

[JsonProperty("timing")] public long Timing { get; set; }

[JsonProperty("version")] public string Version { get; set; }
}
}
66 changes: 66 additions & 0 deletions src/Driver/TDengineVersion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System;

namespace TDengine.Driver
{
public class TDengineVersion
{
private static Version MinimumVersion { get; set; } = new Version(3, 3, 2, 0);

public static Version ParseVersion(string version)
{
if (string.IsNullOrEmpty(version))
{
throw new UnknownVersionException(version);
}

var parts = version.Split('.');
if (parts.Length < 4)
{
throw new UnknownVersionException(version);
}

version = string.Join(".", parts[0], parts[1], parts[2], parts[3]);
if (!Version.TryParse(version, out var parsedVersion))
{
throw new UnknownVersionException(version);
}

return parsedVersion;
}

public static void CheckVersionCompatibility(string ver)
{
var currentVersion = ParseVersion(ver);
if (currentVersion < MinimumVersion)
{
throw new VersionMismatchException(
currentVersion.ToString(),
MinimumVersion.ToString());
}
}
}

public class UnknownVersionException : Exception
{
public string Version { get; }

public UnknownVersionException(string version)
: base($"Unknown TDengine version: {version}")
{
Version = version;
}
}

public class VersionMismatchException : Exception
{
public string CurrentVersion { get; }
public string MinimumVersion { get; }

public VersionMismatchException(string currentVersion, string minimumVersion)
: base($"Version mismatch. The minimum required TDengine version is {minimumVersion}.")
{
CurrentVersion = currentVersion;
MinimumVersion = minimumVersion;
}
}
}
44 changes: 44 additions & 0 deletions test/Driver.Test/Client/Version/TDengineVersionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using TDengine.Driver;
using Xunit;

namespace Driver.Test.Client.Version
{
public class TDengineVersionTests
{
[Theory]
[InlineData("3.3.6.3", 3, 3, 6, 3)]
[InlineData("3.3.2.0", 3, 3, 2, 0)]
[InlineData("3.3.6.3.alpha", 3, 3, 6, 3)]
public void ParseVersion_ValidVersion_ReturnsVersion(string input, int major, int minor, int build,
int revision)
{
var version = TDengineVersion.ParseVersion(input);
Assert.Equal(new System.Version(major, minor, build, revision), version);
}

[Theory]
[InlineData("")]
[InlineData(null)]
[InlineData("3.3")]
[InlineData("3.3.6")]
[InlineData("abc.def.ghi.jkl")]
public void ParseVersion_InvalidVersion_ThrowsUnknownVersionException(string input)
{
Assert.Throws<UnknownVersionException>(() => TDengineVersion.ParseVersion(input));
}

[Fact]
public void CheckVersionCompatibility_VersionTooLow_ThrowsVersionMismatchException()
{
Assert.Throws<VersionMismatchException>(() => TDengineVersion.CheckVersionCompatibility("3.3.1.0"));
}

[Fact]
public void CheckVersionCompatibility_VersionEqualOrHigher_NoException()
{
TDengineVersion.CheckVersionCompatibility("3.3.2.0");
TDengineVersion.CheckVersionCompatibility("3.3.6.3");
TDengineVersion.CheckVersionCompatibility("3.3.10.0");
}
}
}