Skip to content

Commit 8f32788

Browse files
committed
Proper handling of command timeout
1 parent c1ac6c3 commit 8f32788

File tree

4 files changed

+135
-32
lines changed

4 files changed

+135
-32
lines changed

src/FirebirdSql.Data.FirebirdClient.Tests/FbCommandTests.cs

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,4 +831,117 @@ public async Task CommandTimeoutTest()
831831
Assert.ThrowsAsync<OperationCanceledException>(async () => await cmd.ExecuteNonQueryAsync());
832832
}
833833
}
834+
835+
[Test]
836+
public async Task DefaultTimeoutValueTest()
837+
{
838+
var csb = BuildConnectionStringBuilder(ServerType, Compression, WireCrypt);
839+
await using (var conn = new FbConnection(csb.ToString()))
840+
{
841+
await using (var cmd = conn.CreateCommand())
842+
{
843+
Assert.AreEqual(0, cmd.CommandTimeout);
844+
}
845+
}
846+
}
847+
848+
[Test]
849+
public async Task TimeoutConnectionStringTest()
850+
{
851+
var csb = BuildConnectionStringBuilder(ServerType, Compression, WireCrypt);
852+
csb.CommandTimeout = 20;
853+
await using (var conn = new FbConnection(csb.ToString()))
854+
{
855+
await using (var cmd = conn.CreateCommand())
856+
{
857+
Assert.AreEqual(20, cmd.CommandTimeout);
858+
}
859+
}
860+
}
861+
862+
[Test]
863+
public async Task TimeoutNegativeConnectionStringTest()
864+
{
865+
var csb = BuildConnectionStringBuilder(ServerType, Compression, WireCrypt);
866+
csb.CommandTimeout = -1;
867+
await using (var conn = new FbConnection(csb.ToString()))
868+
{
869+
await using (var cmd = conn.CreateCommand())
870+
{
871+
Assert.AreEqual(0, cmd.CommandTimeout);
872+
}
873+
}
874+
}
875+
876+
[Test]
877+
public async Task Timeout0ConnectionStringTest()
878+
{
879+
var csb = BuildConnectionStringBuilder(ServerType, Compression, WireCrypt);
880+
csb.CommandTimeout = 0;
881+
await using (var conn = new FbConnection(csb.ToString()))
882+
{
883+
await using (var cmd = conn.CreateCommand())
884+
{
885+
Assert.AreEqual(0, cmd.CommandTimeout);
886+
}
887+
}
888+
}
889+
890+
[Test]
891+
public async Task SetTimeoutTest()
892+
{
893+
var csb = BuildConnectionStringBuilder(ServerType, Compression, WireCrypt);
894+
await using (var conn = new FbConnection(csb.ToString()))
895+
{
896+
await using (var cmd = conn.CreateCommand())
897+
{
898+
cmd.CommandTimeout = 6;
899+
Assert.AreEqual(6, cmd.CommandTimeout);
900+
}
901+
}
902+
}
903+
904+
[Test]
905+
public async Task SetTimeout0Test()
906+
{
907+
var csb = BuildConnectionStringBuilder(ServerType, Compression, WireCrypt);
908+
await using (var conn = new FbConnection(csb.ToString()))
909+
{
910+
await using (var cmd = conn.CreateCommand())
911+
{
912+
cmd.CommandTimeout = 0;
913+
Assert.AreEqual(0, cmd.CommandTimeout);
914+
}
915+
}
916+
}
917+
918+
[Test]
919+
public async Task TimeoutConnectionStringOverrideTest()
920+
{
921+
var csb = BuildConnectionStringBuilder(ServerType, Compression, WireCrypt);
922+
csb.CommandTimeout = 20;
923+
await using (var conn = new FbConnection(csb.ToString()))
924+
{
925+
await using (var cmd = conn.CreateCommand())
926+
{
927+
cmd.CommandTimeout = 2;
928+
Assert.AreEqual(2, cmd.CommandTimeout);
929+
}
930+
}
931+
}
932+
933+
[Test]
934+
public async Task TimeoutConnectionStringOverride0Test()
935+
{
936+
var csb = BuildConnectionStringBuilder(ServerType, Compression, WireCrypt);
937+
csb.CommandTimeout = 20;
938+
await using (var conn = new FbConnection(csb.ToString()))
939+
{
940+
await using (var cmd = conn.CreateCommand())
941+
{
942+
cmd.CommandTimeout = 0;
943+
Assert.AreEqual(0, cmd.CommandTimeout);
944+
}
945+
}
946+
}
834947
}

src/FirebirdSql.Data.FirebirdClient.Tests/FbConnectionTests.cs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -668,21 +668,6 @@ public async Task ApplicationNameCorrectlyPassedToServer()
668668

669669
}
670670

671-
[Test]
672-
public async Task CommandTimeoutProperlyPassed()
673-
{
674-
var csb = BuildConnectionStringBuilder(ServerType, Compression, WireCrypt);
675-
csb.CommandTimeout = 23;
676-
await using (var conn = new FbConnection(csb.ToString()))
677-
{
678-
await using (var command = conn.CreateCommand())
679-
{
680-
Assert.AreEqual(csb.CommandTimeout, command.CommandTimeout);
681-
}
682-
}
683-
684-
}
685-
686671
private async Task BeginTransactionILTestsHelper(IsolationLevel level)
687672
{
688673
await using (var conn = new FbConnection(BuildConnectionString(ServerType, Compression, WireCrypt)))

src/FirebirdSql.Data.FirebirdClient/FirebirdClient/FbBatchCommand.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public sealed class FbBatchCommand : IFbPreparedCommand, IDescriptorFiller, IDis
4646
private string _commandText;
4747
private bool _disposed;
4848
private bool _implicitTransaction;
49-
//private int _commandTimeout;
49+
//private int? _commandTimeout;
5050
//private int _fetchSize;
5151
private bool _multiError;
5252

@@ -68,15 +68,17 @@ public string CommandText
6868
}
6969
}
7070

71+
//[Category("Behavior")]
72+
//[DefaultValue(ConnectionString.DefaultValueCommandTimeout)]
7173
//public int CommandTimeout
7274
//{
7375
// get
7476
// {
75-
// if (_commandTimeout > 0)
76-
// return _commandTimeout;
77-
// if (_connection?.ConnectionOptions.CommandTimeout > 0)
78-
// return (int)_connection?.ConnectionOptions.CommandTimeout;
79-
// return 30;
77+
// if (_commandTimeout != null)
78+
// return (int)_commandTimeout;
79+
// if (_connection?.CommandTimeout >= 0)
80+
// return (int)_connection?.CommandTimeout;
81+
// return ConnectionString.DefaultValueCommandTimeout;
8082
// }
8183
// set
8284
// {
@@ -228,7 +230,7 @@ public FbBatchCommand(string cmdText, FbConnection connection)
228230
public FbBatchCommand(string cmdText, FbConnection connection, FbTransaction transaction)
229231
{
230232
_namedParameters = Array.Empty<string>();
231-
//_commandTimeout = 0;
233+
//_commandTimeout = null;
232234
//_fetchSize = 200;
233235
_multiError = false;
234236
_commandText = string.Empty;
@@ -265,7 +267,7 @@ public void Dispose()
265267
throw FbException.Create(ex);
266268
}
267269
_multiError = false;
268-
//_commandTimeout = 0;
270+
//_commandTimeout = null;
269271
//_fetchSize = 0;
270272
_implicitTransaction = false;
271273
_commandText = null;

src/FirebirdSql.Data.FirebirdClient/FirebirdClient/FbCommand.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public sealed class FbCommand : DbCommand, IFbPreparedCommand, IDescriptorFiller
4646
private bool _disposed;
4747
private bool _designTimeVisible;
4848
private bool _implicitTransaction;
49-
private int _commandTimeout;
49+
private int? _commandTimeout;
5050
private int _fetchSize;
5151
private Type[] _expectedColumnTypes;
5252

@@ -72,22 +72,25 @@ public override string CommandText
7272
}
7373

7474
[Category("Data")]
75-
[DefaultValue(CommandType.Text), RefreshProperties(RefreshProperties.All)]
75+
[DefaultValue(CommandType.Text)]
76+
[RefreshProperties(RefreshProperties.All)]
7677
public override CommandType CommandType
7778
{
7879
get { return _commandType; }
7980
set { _commandType = value; }
8081
}
8182

83+
[Category("Behavior")]
84+
[DefaultValue(ConnectionString.DefaultValueCommandTimeout)]
8285
public override int CommandTimeout
8386
{
8487
get
8588
{
86-
if (_commandTimeout > 0)
87-
return _commandTimeout;
88-
if (_connection?.ConnectionOptions.CommandTimeout > 0)
89-
return (int)_connection?.ConnectionOptions.CommandTimeout;
90-
return 30;
89+
if (_commandTimeout != null)
90+
return (int)_commandTimeout;
91+
if (_connection?.CommandTimeout >= 0)
92+
return (int)_connection?.CommandTimeout;
93+
return ConnectionString.DefaultValueCommandTimeout;
9194
}
9295
set
9396
{
@@ -312,7 +315,7 @@ public FbCommand(string cmdText, FbConnection connection, FbTransaction transact
312315
_updatedRowSource = UpdateRowSource.Both;
313316
_commandType = CommandType.Text;
314317
_designTimeVisible = true;
315-
_commandTimeout = 0;
318+
_commandTimeout = null;
316319
_fetchSize = 200;
317320
_commandText = string.Empty;
318321

@@ -356,7 +359,7 @@ protected override void Dispose(bool disposing)
356359
{
357360
throw FbException.Create(ex);
358361
}
359-
_commandTimeout = 0;
362+
_commandTimeout = null;
360363
_fetchSize = 0;
361364
_implicitTransaction = false;
362365
_commandText = null;

0 commit comments

Comments
 (0)