Fix 16550 "not authorized", TypeError, and others

This set of commits fixes a number of outstanding errors, including 16550: "not authorized for query" and TypeError: no implicit conversion of nil into String.

I believe this addresses #288, #278, #252, #247, #275, #290, and others.

Nearly everything here is triggered by unstable network environments, hence the difficulty in testing them. Thanks to MongoLab for their flipflop server which was a great help. I also injected random intentional DNS failures. The combination of the two, along with the helpful script in #323, were the basis for discovery. On to the issues:

DNS address resolution did not handle the case of DNS failure on the initial call to Address#resolve. This caused node.address.ip and node.address.resolved to be invalid which had a variety of cascading effects, including a memory leak in the seeds array and the dreaded "TypeError: no implicit conversion of nil into String" error.

There are multiple codepaths to trigger Connection#connect. Only some of them actually call apply_credentials(). Further, it was possible to call #connect without resetting @credentials, causing apply_credentials() to no-op. Each of these could individually trigger the 16550 errors. All of this has been addressed.

Using the flipflop server + random DNS failures and the script noted above, the only remaining error I can cause is '13435: "not master and slaveOk=false"'. The patches in #320 fix those as well and I recommend that set of patches. I have tested the below set of patches against both moped/master as well as the tree from #320.


This was brought up in my pull https://github.com/mongoid/moped/pull/286 which was rejected due to it causing some issues on 2.0. @zarqman hits the nail on the head on the problem.