The text T4 of the Tale of Years (the full version Tolkien wanted to publish but couldn't for space reasons, leading to the very compressed Appendix B text) has this:
3001 About this time Gollum was captured and taken to Mordor and there held in prison.
Therefore Gollum had first met Shelob some two decades before he was captured. It's clear that whether or not Sauron expected him to die, Gollum knew he could exit via Cirith Ungol without becoming spider-meat. However, it's very clear from Shagrat and Gorbag's conversation that Gollum was deliberately allowed to escape, likely because Sauron felt he might lead him to the Ring.