Skip to content

Commit

Permalink
fixed DB consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
OlenaKostash committed Sep 17, 2024
1 parent 6750369 commit 36f9c5e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/NBomber.Sinks.Timescale/NBomber.Sinks.Timescale.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

<ItemGroup>
<PackageReference Include="NBomber.Contracts" Version="5.5.0" />
<PackageReference Include="Npgsql" Version="8.0.3" />
<PackageReference Include="Npgsql" Version="8.0.4" />
<PackageReference Include="RepoDb.PostgreSql" Version="1.13.1" />
<PackageReference Include="RepoDb.PostgreSql.BulkOperations" Version="1.13.1" />
</ItemGroup>
Expand Down
30 changes: 27 additions & 3 deletions src/NBomber.Sinks.Timescale/TimescaleDbSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class TimescaleDbSink : IReportingSink
private IBaseContext _context;
private NpgsqlConnection _mainConnection;
private string _connectionString = "";
private bool _dbError = false;

public string SinkName => "NBomber.Sinks.TimescaleDb";

Expand Down Expand Up @@ -83,8 +84,31 @@ public async Task Start()
TestName = testInfo.TestName,
NodeInfo = Json.serialize(nodeInfo)
};

await _mainConnection.BinaryBulkInsertAsync(SqlQueries.SessionsTable, new [] { record });

var text = @$"INSERT INTO {SqlQueries.SessionsTable}
(""{ColumnNames.Time}"",
""{ColumnNames.SessionId}"",
""{ColumnNames.CurrentOperation}"",
""{ColumnNames.TestSuite}"",
""{ColumnNames.TestName}"",
""{ColumnNames.NodeInfo}"")
VALUES
('{record.Time}',
'{record.SessionId}',
'{record.CurrentOperation}',
'{record.TestSuite}',
'{record.TestName}',
'{record.NodeInfo}'::jsonb)";
try
{
await _mainConnection.ExecuteNonQueryAsync(text);
}
catch (Exception ex)
{
_dbError = true;
_logger.Error(ex, ex.Message);
throw ex;
}
}
}

Expand All @@ -102,7 +126,7 @@ public void Dispose()

private async Task SaveScenarioStats(ScenarioStats[] stats, bool isFinal = false)
{
if (_mainConnection != null)
if (_mainConnection != null && !_dbError)
{
var currentTime = DateTime.UtcNow;

Expand Down

0 comments on commit 36f9c5e

Please sign in to comment.