Skip to content

Commit

Permalink
Merge pull request #153 from progaudi/fix/forward-compatibility-error…
Browse files Browse the repository at this point in the history
…-packet
  • Loading branch information
aensidhe authored Mar 21, 2021
2 parents 89c91c6 + a346b54 commit c046b68
Show file tree
Hide file tree
Showing 18 changed files with 86 additions and 72 deletions.
27 changes: 16 additions & 11 deletions src/progaudi.tarantool/Converters/ErrorResponsePacketConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

using ProGaudi.Tarantool.Client.Model.Enums;
using ProGaudi.Tarantool.Client.Model.Responses;
using ProGaudi.Tarantool.Client.Utils;

namespace ProGaudi.Tarantool.Client.Converters
{
Expand All @@ -29,19 +28,25 @@ public ErrorResponse Read(IMsgPackReader reader)
string errorMessage = null;
var length = reader.ReadMapLength();

if (length != 1u)
for (var i = 0; i < length; i++)
{
throw ExceptionHelper.InvalidMapLength(length, 1u);
var errorKey = _keyConverter.Read(reader);

switch (errorKey)
{
case Key.Error24:
errorMessage = _stringConverter.Read(reader);
break;
case Key.Error:
// TODO: add parsing of new error metadata
reader.SkipToken();
break;
default:
reader.SkipToken();
break;
}
}

var errorKey = _keyConverter.Read(reader);
if (errorKey != Key.Error)
{
throw ExceptionHelper.UnexpectedKey(errorKey, Key.Error);
}

errorMessage = _stringConverter.Read(reader);

return new ErrorResponse(errorMessage);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/progaudi.tarantool/Model/Enums/Key.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ public enum Key : uint

// Response keys
Data = 0x30,
Error = 0x31,
Error24 = 0x31,
Metadata = 0x32,
Error = 0x52,

// Sql keys
SqlQueryText = 0x40,
Expand Down
26 changes: 13 additions & 13 deletions tests/progaudi.tarantool.tests/Box/Call_Should.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class Call_Should : TestBase
[Fact]
public async Task call_method()
{
using (var tarantoolClient = await Client.Box.Connect(ConnectionStringFactory.GetReplicationSource_1_7()))
using (var tarantoolClient = await Client.Box.Connect(await ConnectionStringFactory.GetReplicationSource_1_7()))
{
var result = await tarantoolClient.Call_1_6<TarantoolTuple<double>, TarantoolTuple<double>>("math.sqrt", TarantoolTuple.Create(1.3));

Expand All @@ -28,7 +28,7 @@ public async Task call_method()
[Fact]
public async Task return_null_v1_6_should_not_throw()
{
using (var tarantoolClient = await Client.Box.Connect(ConnectionStringFactory.GetReplicationSource_1_7()))
using (var tarantoolClient = await Client.Box.Connect(await ConnectionStringFactory.GetReplicationSource_1_7()))
{
await Should.ThrowAsync<ArgumentException>(async () => await tarantoolClient.Call_1_6<TarantoolTuple<string, int>>("return_null"));
}
Expand All @@ -37,7 +37,7 @@ public async Task return_null_v1_6_should_not_throw()
[Fact]
public async Task return_tuple_v1_6_with_null_should_not_throw()
{
using (var tarantoolClient = await Client.Box.Connect(ConnectionStringFactory.GetReplicationSource_1_7()))
using (var tarantoolClient = await Client.Box.Connect(await ConnectionStringFactory.GetReplicationSource_1_7()))
{
var result = await tarantoolClient.Call_1_6<TarantoolTuple<string>>("return_tuple_with_null");
result.Data.ShouldBe(new[] { TarantoolTuple.Create(default(string)) });
Expand All @@ -47,7 +47,7 @@ public async Task return_tuple_v1_6_with_null_should_not_throw()
[Fact]
public async Task return_tuple_v1_6_should_not_throw()
{
using (var tarantoolClient = await Client.Box.Connect(ConnectionStringFactory.GetReplicationSource_1_7()))
using (var tarantoolClient = await Client.Box.Connect(await ConnectionStringFactory.GetReplicationSource_1_7()))
{
var result = await tarantoolClient.Call_1_6<TarantoolTuple<int, int>>("return_tuple");
result.Data.ShouldBe(new[] {TarantoolTuple.Create(1, 2)});
Expand All @@ -57,7 +57,7 @@ public async Task return_tuple_v1_6_should_not_throw()
[Fact]
public async Task return_int_v1_6_should_not_throw()
{
using (var tarantoolClient = await Client.Box.Connect(ConnectionStringFactory.GetReplicationSource_1_7()))
using (var tarantoolClient = await Client.Box.Connect(await ConnectionStringFactory.GetReplicationSource_1_7()))
{
var result = await tarantoolClient.Call_1_6<TarantoolTuple<int>>("return_scalar");
result.Data.ShouldBe(new[] { TarantoolTuple.Create(1) });
Expand All @@ -67,7 +67,7 @@ public async Task return_int_v1_6_should_not_throw()
[Fact]
public async Task return_nothing_v1_6_should_not_throw()
{
using (var tarantoolClient = await Client.Box.Connect(ConnectionStringFactory.GetReplicationSource_1_7()))
using (var tarantoolClient = await Client.Box.Connect(await ConnectionStringFactory.GetReplicationSource_1_7()))
{
Should.NotThrow(async () => await tarantoolClient.Call_1_6("return_nothing"));
}
Expand All @@ -76,7 +76,7 @@ public async Task return_nothing_v1_6_should_not_throw()
[Fact]
public async Task return_null_should_not_throw()
{
using (var tarantoolClient = await Client.Box.Connect(ConnectionStringFactory.GetReplicationSource_1_7()))
using (var tarantoolClient = await Client.Box.Connect(await ConnectionStringFactory.GetReplicationSource_1_7()))
{
var result = await tarantoolClient.Call<TarantoolTuple<string, int>>("return_null");
result.Data.ShouldBe(new[] { default(TarantoolTuple<string, int>) });
Expand All @@ -86,7 +86,7 @@ public async Task return_null_should_not_throw()
[Fact]
public async Task return_tuple_with_null_should_not_throw()
{
using (var tarantoolClient = await Client.Box.Connect(ConnectionStringFactory.GetReplicationSource_1_7()))
using (var tarantoolClient = await Client.Box.Connect(await ConnectionStringFactory.GetReplicationSource_1_7()))
{
var result = await tarantoolClient.Call<TarantoolTuple<string>>("return_tuple_with_null");
result.Data.ShouldBe(new[] { TarantoolTuple.Create(default(string)) });
Expand All @@ -96,7 +96,7 @@ public async Task return_tuple_with_null_should_not_throw()
[Fact]
public async Task return_tuple_should_not_throw()
{
using (var tarantoolClient = await Client.Box.Connect(ConnectionStringFactory.GetReplicationSource_1_7()))
using (var tarantoolClient = await Client.Box.Connect(await ConnectionStringFactory.GetReplicationSource_1_7()))
{
var result = await tarantoolClient.Call<TarantoolTuple<int, int>>("return_tuple");
result.Data.ShouldBe(new[] { TarantoolTuple.Create(1, 2) });
Expand All @@ -106,7 +106,7 @@ public async Task return_tuple_should_not_throw()
[Fact]
public async Task return_array_should_not_throw()
{
using (var tarantoolClient = await Client.Box.Connect(ConnectionStringFactory.GetReplicationSource_1_7()))
using (var tarantoolClient = await Client.Box.Connect(await ConnectionStringFactory.GetReplicationSource_1_7()))
{
var result = await tarantoolClient.Call<TarantoolTuple<string[]>>("return_array");
result.Data[0].Item1.ShouldBe(new[] {"abc", "def"});
Expand All @@ -116,7 +116,7 @@ public async Task return_array_should_not_throw()
[Fact]
public async Task return_int_should_not_throw()
{
using (var tarantoolClient = await Client.Box.Connect(ConnectionStringFactory.GetReplicationSource_1_7()))
using (var tarantoolClient = await Client.Box.Connect(await ConnectionStringFactory.GetReplicationSource_1_7()))
{
var result = await tarantoolClient.Call<int>("return_scalar");
result.Data.ShouldBe(new[] { 1 });
Expand All @@ -126,7 +126,7 @@ public async Task return_int_should_not_throw()
[Fact]
public async Task return_nothing_should_not_throw()
{
using (var tarantoolClient = await Client.Box.Connect(ConnectionStringFactory.GetReplicationSource_1_7()))
using (var tarantoolClient = await Client.Box.Connect(await ConnectionStringFactory.GetReplicationSource_1_7()))
{
Should.NotThrow(async () => await tarantoolClient.Call("return_nothing"));
}
Expand All @@ -135,7 +135,7 @@ public async Task return_nothing_should_not_throw()
[Fact]
public async Task replace_via_call()
{
using (var tarantoolClient = await Client.Box.Connect(ConnectionStringFactory.GetReplicationSource_1_8()))
using (var tarantoolClient = await Client.Box.Connect(await ConnectionStringFactory.GetReplicationSource_1_8()))
{
var tuple = ("123", new byte[] { 1, 2, 3 });
var result = await tarantoolClient.Call<ValueTuple<string, byte[]>[], ValueTuple<string, byte[]>>(
Expand Down
16 changes: 8 additions & 8 deletions tests/progaudi.tarantool.tests/Box/Connect_Should.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,40 @@ public class Connect_Should : TestBase
[Fact]
public async Task connect_if_UserName_is_null_and_GuestMode()
{
using (await Client.Box.Connect(ConnectionStringFactory.GetReplicationSource_1_7()))
using (await Client.Box.Connect(await ConnectionStringFactory.GetReplicationSource_1_7()))
{ }
}

[Fact]
public async Task throw_exception_if_password_is_wrong()
{
await Client.Box.Connect(ConnectionStringFactory.GetReplicationSource_1_7("operator:wrongPassword")).ShouldThrowAsync<ArgumentException>();
await Client.Box.Connect(await ConnectionStringFactory.GetReplicationSource_1_7("operator:wrongPassword")).ShouldThrowAsync<ArgumentException>();
}

[Fact]
public async Task throw_exception_if_password_is_empty_for_user_with_unset_password()
{
await Client.Box.Connect(ConnectionStringFactory.GetReplicationSource_1_7("notSetPassword:")).ShouldThrowAsync<ArgumentException>();
await Client.Box.Connect(await ConnectionStringFactory.GetReplicationSource_1_7("notSetPassword:")).ShouldThrowAsync<ArgumentException>();
}

[Fact]
public async Task connect_if_password_is_empty_for_user_with_empty_password()
{
using (await Client.Box.Connect(ConnectionStringFactory.GetReplicationSource_1_7("emptyPassword:")))
using (await Client.Box.Connect(await ConnectionStringFactory.GetReplicationSource_1_7("emptyPassword:")))
{ }
}

[Fact]
public async Task connect_with_credentials()
{
using (await Client.Box.Connect(ConnectionStringFactory.GetReplicationSource_1_7("operator:operator")))
using (await Client.Box.Connect(await ConnectionStringFactory.GetReplicationSource_1_7("operator:operator")))
{ }
}

[Fact]
public async Task do_nothing_if_already_connected()
{
using (var box = await Client.Box.Connect(ConnectionStringFactory.GetReplicationSource_1_7("operator:operator")))
using (var box = await Client.Box.Connect(await ConnectionStringFactory.GetReplicationSource_1_7("operator:operator")))
{
var result = await box.Call_1_6<TarantoolTuple<double>, TarantoolTuple<double>>("math.sqrt", TarantoolTuple.Create(1.3));

Expand All @@ -68,7 +68,7 @@ public async Task do_nothing_if_already_connected()
[Fact]
public async Task not_throw_expection_if_used_inside_another_class()
{
using (var box = await Client.Box.Connect(ConnectionStringFactory.GetReplicationSource_1_7("operator:operator")))
using (var box = await Client.Box.Connect(await ConnectionStringFactory.GetReplicationSource_1_7("operator:operator")))
using (var boxUser = new BoxUser(box))
{
var result = await boxUser.TestMethod();
Expand All @@ -81,7 +81,7 @@ public async Task not_throw_expection_if_used_inside_another_class()
[Fact]
public async Task change_IsConnected_state()
{
using (var box = new Client.Box(new ClientOptions(ConnectionStringFactory.GetReplicationSource_1_7("operator:operator"))))
using (var box = new Client.Box(new ClientOptions(await ConnectionStringFactory.GetReplicationSource_1_7("operator:operator"))))
{
box.IsConnected.ShouldBeFalse();
await box.Connect();
Expand Down
8 changes: 4 additions & 4 deletions tests/progaudi.tarantool.tests/Box/Eval_Should.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class Eval_Should : TestBase
[Fact]
public async Task evaluate_expression()
{
using (var tarantoolClient = await Client.Box.Connect(ConnectionStringFactory.GetReplicationSource_1_7()))
using (var tarantoolClient = await Client.Box.Connect(await ConnectionStringFactory.GetReplicationSource_1_7()))
{
var result = await tarantoolClient.Eval<TarantoolTuple<int, int, int>, int>("return ...", TarantoolTuple.Create(1, 2, 3));

Expand All @@ -24,7 +24,7 @@ public async Task evaluate_expression()
[Fact]
public async Task evaluate_scalar()
{
using (var tarantoolClient = await Client.Box.Connect(ConnectionStringFactory.GetReplicationSource_1_7()))
using (var tarantoolClient = await Client.Box.Connect(await ConnectionStringFactory.GetReplicationSource_1_7()))
{
var result = await tarantoolClient.Eval<int>("return 1");

Expand All @@ -35,7 +35,7 @@ public async Task evaluate_scalar()
[Fact]
public async Task evaluate_call_function()
{
using (var tarantoolClient = await Client.Box.Connect(ConnectionStringFactory.GetReplicationSource_1_7()))
using (var tarantoolClient = await Client.Box.Connect(await ConnectionStringFactory.GetReplicationSource_1_7()))
{
var result = await tarantoolClient.Eval<TarantoolTuple<int, int, int>, TarantoolTuple<int, int>>("return return_tuple()", TarantoolTuple.Create(1, 2, 3));

Expand All @@ -46,7 +46,7 @@ public async Task evaluate_call_function()
[Fact]
public async Task evaluate_return_null()
{
using (var tarantoolClient = await Client.Box.Connect(ConnectionStringFactory.GetReplicationSource_1_7()))
using (var tarantoolClient = await Client.Box.Connect(await ConnectionStringFactory.GetReplicationSource_1_7()))
{
var result = await tarantoolClient.Eval<TarantoolTuple, TarantoolTuple<int, int>>("return return_null()", TarantoolTuple.Empty);

Expand Down
8 changes: 4 additions & 4 deletions tests/progaudi.tarantool.tests/Box/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class Options
[Fact]
public async Task ReadSchemaOnConnectEnabled()
{
using (var box = await Client.Box.Connect(ConnectionStringFactory.GetReplicationSource_1_7()))
using (var box = await Client.Box.Connect(await ConnectionStringFactory.GetReplicationSource_1_7()))
{
var utcnow = DateTimeOffset.UtcNow;
box.Schema.LastReloadTime.ShouldBeInRange(utcnow.AddSeconds(-2), utcnow);
Expand All @@ -21,7 +21,7 @@ public async Task ReadSchemaOnConnectEnabled()
[Fact]
public async Task ReadSchemaOnConnectDisabled()
{
var options = new ClientOptions(ConnectionStringFactory.GetReplicationSource_1_7());
var options = new ClientOptions(await ConnectionStringFactory.GetReplicationSource_1_7());
options.ConnectionOptions.ReadSchemaOnConnect = false;
using (var box = new Client.Box(options))
{
Expand All @@ -33,7 +33,7 @@ public async Task ReadSchemaOnConnectDisabled()
[Fact]
public async Task ReadBoxInfoOnConnectEnabled()
{
using (var box = await Client.Box.Connect(ConnectionStringFactory.GetReplicationSource_1_7()))
using (var box = await Client.Box.Connect(await ConnectionStringFactory.GetReplicationSource_1_7()))
{
box.Info.ShouldNotBeNull();
}
Expand All @@ -42,7 +42,7 @@ public async Task ReadBoxInfoOnConnectEnabled()
[Fact]
public async Task ReadBoxInfoOnConnectDisabled()
{
var options = new ClientOptions(ConnectionStringFactory.GetReplicationSource_1_7());
var options = new ClientOptions(await ConnectionStringFactory.GetReplicationSource_1_7());
options.ConnectionOptions.ReadBoxInfoOnConnect = false;
using (var box = new Client.Box(options))
{
Expand Down
2 changes: 1 addition & 1 deletion tests/progaudi.tarantool.tests/Box/Version.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class Version : TestBase
[Fact]
public async Task Smoke()
{
var options = new ClientOptions(ConnectionStringFactory.GetReplicationSource_1_7());
var options = new ClientOptions(await ConnectionStringFactory.GetReplicationSource_1_7());
options.ConnectionOptions.ReadBoxInfoOnConnect = false;
using (var box = new Client.Box(options))
{
Expand Down
26 changes: 17 additions & 9 deletions tests/progaudi.tarantool.tests/ConnectionStringFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,36 @@ namespace ProGaudi.Tarantool.Client.Tests
{
internal class ConnectionStringFactory
{
public static string GetReplicationSource_1_7(string userName = null)
public static async Task<string> GetReplicationSource_1_7(string userName = null)
{
return GetReplicationSource(userName, "localhost:3301", "TARANTOOL_1_7_REPLICATION_SOURCE");
return GetReplicationSource(userName, $"{await ResolveHostname("localhost")}:3301", "TARANTOOL_1_7_REPLICATION_SOURCE");
}

public static string GetReplicationSource_1_8(string userName = null)
public static async Task<string> GetReplicationSource_1_8(string userName = null)
{
return GetReplicationSource(userName, "localhost:3302", "TARANTOOL_1_8_REPLICATION_SOURCE");
return GetReplicationSource(userName, $"{await ResolveHostname("localhost")}:3302", "TARANTOOL_1_8_REPLICATION_SOURCE");
}

public static async Task<string> GetRedisConnectionString()
{
var redisUrl = Environment.GetEnvironmentVariable("REDIS_HOST");

var host = "127.0.0.1";
if (!string.IsNullOrWhiteSpace(redisUrl))
return $"{await ResolveHostname(redisUrl)}:6379";
}

private static async Task<string> ResolveHostname(string host)
{
if (!string.IsNullOrWhiteSpace(host))
{
var resolved = await Dns.GetHostAddressesAsync(redisUrl);
host = resolved.First().ToString();
var resolved = await Dns.GetHostAddressesAsync(host);
var ip = resolved.First().ToString();
if (ip == "::1")
{
return "127.0.0.1";
}
}

return $"{host}:6379";
return "127.0.0.1";
}

private static string GetReplicationSource(string userName, string defaultString, string envName)
Expand Down
4 changes: 2 additions & 2 deletions tests/progaudi.tarantool.tests/Index/Smoke.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public async Task HashIndexMethods()

await ClearDataAsync(spaceName);

using (var tarantoolClient = await Client.Box.Connect(ConnectionStringFactory.GetReplicationSource_1_7()))
using (var tarantoolClient = await Client.Box.Connect(await ConnectionStringFactory.GetReplicationSource_1_7()))
{
var index = tarantoolClient.GetSchema()[spaceName]["primary"];

Expand Down Expand Up @@ -53,7 +53,7 @@ public async Task TreeIndexMethods()

await ClearDataAsync(spaceName);

using (var tarantoolClient = await Client.Box.Connect(ConnectionStringFactory.GetReplicationSource_1_7()))
using (var tarantoolClient = await Client.Box.Connect(await ConnectionStringFactory.GetReplicationSource_1_7()))
{
var schema = tarantoolClient.GetSchema();

Expand Down
Loading

0 comments on commit c046b68

Please sign in to comment.