Skip to content

Instantly share code, notes, and snippets.

/gistit Secret

Created September 15, 2016 22:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/7595f6db11d7202cb3fa12e64a862c61 to your computer and use it in GitHub Desktop.
Save anonymous/7595f6db11d7202cb3fa12e64a862c61 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"io/ioutil"
"net"
"os"
"path/filepath"
)
func test(tmp string) error {
sockFile := filepath.Join(tmp, "unix.sock")
l, err := net.Listen("unix", sockFile)
if err != nil {
return err
}
// check that there is sock file after listen
if _, err := os.Stat(sockFile); err != nil {
return fmt.Errorf("file %s is not found", sockFile)
}
if err := l.Close(); err != nil {
return fmt.Errorf("first close %v", err)
}
// check that there is no sock file after close
if _, err := os.Stat(sockFile); err == nil {
return fmt.Errorf("file %s is found", sockFile)
}
l2, err := net.Listen("unix", sockFile)
if err != nil {
return err
}
defer l2.Close()
// close old listener again
l.Close()
// new unix-socket deleted
if _, err := os.Stat(sockFile); err != nil {
return fmt.Errorf("file %s is not found", sockFile)
}
return nil
}
func main() {
tmp, err := ioutil.TempDir("", "test-unix-sock-")
if err != nil {
panic(err)
}
defer os.RemoveAll(tmp)
if err := test(tmp); err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment