I’m working on replacing mgo with the official MongoDB driver for Go, and one of my tests is randomly breaking in an unexpected way. Here what the test does:
- Create a proxy (with toxiproxy) to a local MongoDB instance.
- Disable the proxy, so the database seems to be down.
- Run a function that does an update that increments a field. If it fails, it keeps retrying until the command eventually succeeds.
- Enable the proxy.
- Wait for the function to complete and assert that the field has been incremented correctly.
This test randomly breaks because sometimes that field gets incremented twice. I noticed that it happens when an update is retried just as the proxy gets enabled: the client code receives an incomplete read of message header: context deadline exceeded
error, which makes it retry the command, but the previous one indeed succeeded because the field ends up being incremented twice.
Is there anything that I can do on my side to prevent this from happening? I tried to find a specific error to catch, but I couldn’t find any. Or is this something the driver itself should handle? Any help is appreciated.