Remove PortFinder, use AddressManager
Nothing uses the PortFinder anymore. We can now remove the PortFinder, and rename the files to make clear AddressManager is now the thing to use.
This commit is contained in:
		
							parent
							
								
									0683d7977a
								
							
						
					
					
						commit
						a49601db63
					
				|  | @ -61,33 +61,3 @@ func (d *DefaultAddressManager) Host() (string, error) { | |||
| 	} | ||||
| 	return d.host, nil | ||||
| } | ||||
| 
 | ||||
| // PortFinder is the signature of a function returning a free port and a resolved
 | ||||
| // address to listen/bind on, and an erro in case there were some problems finding
 | ||||
| // a free pair of address & port
 | ||||
| type PortFinder func(host string) (port int, resolvedAddress string, err error) | ||||
| 
 | ||||
| //go:generate counterfeiter . PortFinder
 | ||||
| 
 | ||||
| // DefaultPortFinder is the default implementation of PortFinder: It resolves that
 | ||||
| // hostname or IP handed in and asks the kernel for a random port. To make that a bit
 | ||||
| // safer, it also tries to bind to that port to make sure it is not in use. If all goes
 | ||||
| // well the port and the resolved address are returned, otherwise the error is forwarded.
 | ||||
| func DefaultPortFinder(host string) (port int, resolvedAddress string, err error) { | ||||
| 	var addr *net.TCPAddr | ||||
| 	addr, err = net.ResolveTCPAddr("tcp", fmt.Sprintf("%s:0", host)) | ||||
| 	if err != nil { | ||||
| 		return | ||||
| 	} | ||||
| 	resolvedAddress = addr.IP.String() | ||||
| 	var l *net.TCPListener | ||||
| 	l, err = net.ListenTCP("tcp", addr) | ||||
| 	if err != nil { | ||||
| 		return | ||||
| 	} | ||||
| 	defer func() { | ||||
| 		err = l.Close() | ||||
| 	}() | ||||
| 
 | ||||
| 	return l.Addr().(*net.TCPAddr).Port, resolvedAddress, nil | ||||
| } | ||||
|  | @ -86,31 +86,3 @@ var _ = Describe("DefaultAddressManager", func() { | |||
| 
 | ||||
| 	}) | ||||
| }) | ||||
| 
 | ||||
| var _ = Describe("DefaultPortFinder", func() { | ||||
| 	It("returns a free port and an address to bind to", func() { | ||||
| 		port, addr, err := DefaultPortFinder("127.0.0.1") | ||||
| 
 | ||||
| 		Expect(err).NotTo(HaveOccurred()) | ||||
| 		Expect(addr).To(Equal("127.0.0.1")) | ||||
| 		Expect(port).To(BeNumerically(">=", 1)) | ||||
| 		Expect(port).To(BeNumerically("<=", 65535)) | ||||
| 	}) | ||||
| 
 | ||||
| 	It("errrors on invalid host", func() { | ||||
| 		_, _, err := DefaultPortFinder("this is not a hostname") | ||||
| 
 | ||||
| 		Expect(err).To(MatchError(ContainSubstring("no such host"))) | ||||
| 	}) | ||||
| 
 | ||||
| 	Context("when using a DNS name", func() { | ||||
| 		It("returns a free port and an address to bind to", func() { | ||||
| 			port, addr, err := DefaultPortFinder("localhost") | ||||
| 
 | ||||
| 			Expect(err).NotTo(HaveOccurred()) | ||||
| 			Expect(addr).To(Equal("127.0.0.1")) | ||||
| 			Expect(port).To(BeNumerically(">=", 1)) | ||||
| 			Expect(port).To(BeNumerically("<=", 65535)) | ||||
| 		}) | ||||
| 	}) | ||||
| }) | ||||
		Loading…
	
		Reference in New Issue