Closed cleroux closed 5 months ago
Hi @cleroux! Thank you for your request. Just read and then re-read your submission, and I suspect you may be confusing a couple of things, but make sure to correct me if I'm wrong. Here are some pointers:
sqlmock
is a valid /database/sql/driver
that emulates different conditions from a database driver standpoint.To put it another way, try execute a query with a cancelled context using the real pq or pgx drivers. Also try sending a non-cancelled context but somehow make sure that you cancel it before it returns. Both cases using either driver should return the same context cancellation error, otherwise it's a bug in the driver.
To learn more what the Go context
package can and can't do for you make sure to read:
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:
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.