Your best bet if using Great Stuff type spray foam is to apply it in thin layers. Fill up your parts gradually allowing each application to expand and set, usually set enough within a few hours. If the spray foam is enclosed and has little air it will take longer to set. Using a 2 part spray foam is the ideal way to go, because the foam sets up instantly and you can fill up parts completely in one application. As far as the problem that you had with the foam slipping out of the pipe I would say that it would have been caused by a couple of things, the first being the pipe is non porous and the gloss on it makes for a slippers surface, lastly the foam was probably not filled thorouly to create max compression. This should not be a problem typically if these factors are not replicated.