go-sqlmock icon indicating copy to clipboard operation
go-sqlmock copied to clipboard

[Feature] Add a way to test cancellation where the query still completes

Open cleroux opened this issue 3 years ago • 0 comments
trafficstars

Hi! Thank you for this useful library!

With regard to cancelling queries in Postgres, through context cancellation or other mechanism like pg_cancel_backend(), Postgres provides no guarantee that the query will actually be cancelled, and therefore might still complete normally.

From Postgres docs:

The cancellation signal might or might not have any effect — for example, if it arrives after the backend has finished processing the query, then it will have no effect. If the cancellation is effective, it results in the current command being terminated early with an error message.

At the moment, sqlmock will always produce an error if we cancel the context passed into it.

In a highly concurrent system that supports query cancellation, we have observed this edge case happening often enough that we would like to test for it. So it would be nice if we could set up this scenario where the context is cancelled but sqlmock does not produce an error.

I hope to hear your opinion on this. If this sounds reasonable, I may be able to submit a PR. Though I admit I have not dived into the implementation of sqlmock to know how complex this might be.

Proposal

Perhaps something like .WillCompleteOnCancel().WillReturnRows(rows)

Use-cases

I would like to be able to test the case where the query might complete without error when the context is cancelled.

cleroux avatar Aug 25 '22 16:08 cleroux