Skip to content

Commit

Permalink
fix tx test
Browse files Browse the repository at this point in the history
  • Loading branch information
isoos committed Sep 9, 2024
1 parent f1fbfba commit 2b25090
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
27 changes: 19 additions & 8 deletions lib/src/v3/connection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -229,15 +229,16 @@ class PgConnectionImplementation extends _PgSessionBase implements Connection {
);

if (_debugLog) {
final hash = channel.hashCode;
channel = channel.transform(StreamChannelTransformer(
StreamTransformer.fromHandlers(
handleData: (msg, sink) {
print('[in] $msg');
print('[$hash in] $msg');
sink.add(msg);
},
),
async.StreamSinkTransformer.fromHandlers(handleData: (msg, sink) {
print('[out] $msg');
print('[$hash out] $msg');
sink.add(msg);
}),
));
Expand Down Expand Up @@ -493,12 +494,22 @@ class PgConnectionImplementation extends _PgSessionBase implements Connection {
timeout ??= _settings.queryTimeout;
if (timeout.isNegative) return;
if (timeout == _lastStatementTimeout) return;
await _doExecuteSimpleQuery(
InternalQueryDescription.direct(
'SET statement_timeout TO ${timeout.inMilliseconds};'),
true,
);
_lastStatementTimeout = timeout;

try {
await _doExecuteSimpleQuery(
InternalQueryDescription.direct(
'SET statement_timeout TO ${timeout.inMilliseconds};'),
true,
);
_lastStatementTimeout = timeout;
} on ServerException catch (e) {
// we ignore error messages if they happen inside a failed transaction block
if (e.code == '25P02') {
return;
}
// rethrow otherwise
rethrow;
}
}

@override
Expand Down
14 changes: 9 additions & 5 deletions test/timeout_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,17 @@ void main() {

await c2.execute('BEGIN');
await expectLater(
() => c2.execute('SELECT * FROM t WHERE id=1 FOR UPDATE;',
timeout: Duration(seconds: 1)),
throwsA(isA<ServerException>()));
() => c2.execute(
'SELECT * FROM t WHERE id=1 FOR UPDATE;',
timeout: Duration(seconds: 1),
),
throwsA(isA<ServerException>()),
);

await c1.execute('COMMIT');
// This line fails:
// await c2.execute('ROLLBACK');
await c2.execute('COMMIT');

await c2.execute('SELECT * FROM t;');
});
});
}

0 comments on commit 2b25090

Please sign in to comment.