Skip to content

Commit

Permalink
test: add concurrency test
Browse files Browse the repository at this point in the history
  • Loading branch information
huskar-t committed Jan 23, 2025
1 parent 6c90a36 commit 842dfb1
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
68 changes: 68 additions & 0 deletions test/Driver.Test/Client/Query/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -669,5 +669,73 @@ private void AssertValue(IRows rows, object?[][] data)
Assert.Equal(Encoding.UTF8.GetBytes("{\"a\":\"b\"}"), rows.GetValue(data[i].Length));
}
}

private void QueryConcurrencyTest(string connectString, string db)
{
var precision = TDenginePrecision.TSDB_TIME_PRECISION_MILLI;
var builder = new ConnectionStringBuilder(connectString);
var client = DbDriver.Open(builder);
try
{
client.Exec($"drop database if exists {db}");
client.Exec($"create database {db} precision '{PrecisionString(precision)}'");
client.Exec($"use {db}");
client.Exec("create table t1 (ts timestamp, a int, b float, c binary(10))");
var ts = new long[10];
var dateTime = DateTime.Now;
var tsv = new DateTime[10];
for (int i = 0; i < 10; i++)
{
ts[i] = (dateTime.Add(TimeSpan.FromSeconds(i)).ToUniversalTime().Ticks -
TDengineConstant.TimeZero.Ticks) / 10000;
tsv[i] = TDengineConstant.ConvertTimeToDatetime(ts[i], precision);
}

var valuesStr = "";
for (int i = 0; i < 10; i++)
{
valuesStr += $"({ts[i]}, {i}, {i}, '中文')";
}

client.Exec($"insert into t1 values {valuesStr}");
var tasks = new System.Collections.Generic.List<System.Threading.Tasks.Task>();
for (var i = 0; i < 10; i++)
{
int localI = i;
string query = "select * from t1 where ts = " + ts[localI];
tasks.Add(System.Threading.Tasks.Task.Run(() =>
{
using (var rows = client.Query(query))
{
Assert.Equal(1, rows.GetOrdinal("a"));
var fieldCount = rows.FieldCount;
Assert.Equal(4, fieldCount);
Assert.Equal("ts", rows.GetName(0));
Assert.Equal("a", rows.GetName(1));
Assert.Equal("b", rows.GetName(2));
Assert.Equal("c", rows.GetName(3));
var haveNext = rows.Read();
Assert.True(haveNext);
Assert.Equal(tsv[localI], rows.GetValue(0));
Assert.Equal(localI, rows.GetValue(1));
Assert.Equal((float)localI, rows.GetValue(2));
Assert.Equal(Encoding.UTF8.GetBytes("中文"), rows.GetValue(3));
}
}));
}

System.Threading.Tasks.Task.WaitAll(tasks.ToArray());
}
catch (Exception e)
{
_output.WriteLine(e.ToString());
throw;
}
finally
{
client.Exec($"drop database if exists {db}");
client.Dispose();
}
}
}
}
7 changes: 7 additions & 0 deletions test/Driver.Test/Client/Query/Native.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,5 +137,12 @@ public void NativeSMLJsonTest()
var db = "sml_json_test";
this.SMLJsonTest(this._nativeConnectString, db);
}

[Fact]
public void NativeQueryConcurrencyTest()
{
var db = "query_concurrency_test";
this.QueryConcurrencyTest(this._nativeConnectString, db);
}
}
}
7 changes: 7 additions & 0 deletions test/Driver.Test/Client/Query/WS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,5 +137,12 @@ public void WebSocketSMLJsonTest()
var db = "ws_sml_json_test";
this.SMLJsonTest(this._wsConnectString, db);
}

[Fact]
public void WebSocketQueryConcurrencyTest()
{
var db = "ws_query_concurrency_test";
this.QueryConcurrencyTest(this._wsConnectString, db);
}
}
}

0 comments on commit 842dfb1

Please sign in to comment.