summaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
authorAnton Titov <[email protected]>2019-12-23 15:53:51 +0300
committerGitHub <[email protected]>2019-12-23 15:53:51 +0300
commita11a3a5511a7b986f06c5921932e3438a0a543d7 (patch)
treece930d93d3f887da0a74c7bf0ce2b2c5173d1ca3 /util
parent7f2909ffb746880a380dbc8b9d7f4257a5abca6c (diff)
parentb0c299387222c9a32b92a11abb30c85e5c7f4741 (diff)
Merge pull request #224 from spiral/feature/reuse-ports-and-test-improvements
Test improvements
Diffstat (limited to 'util')
-rw-r--r--util/network.go13
1 files changed, 12 insertions, 1 deletions
diff --git a/util/network.go b/util/network.go
index 3f533023..b9066de7 100644
--- a/util/network.go
+++ b/util/network.go
@@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"net"
+ "os"
"strings"
"syscall"
)
@@ -19,7 +20,7 @@ func CreateListener(address string) (net.Listener, error) {
return nil, errors.New("invalid Protocol (tcp://:6001, unix://file.sock)")
}
- if dsn[0] == "unix" {
+ if dsn[0] == "unix" && fileExists(dsn[1]) {
err := syscall.Unlink(dsn[1])
if err != nil {
return nil, fmt.Errorf("error during the unlink syscall: error %v", err)
@@ -28,3 +29,13 @@ func CreateListener(address string) (net.Listener, error) {
return net.Listen(dsn[0], dsn[1])
}
+
+// fileExists checks if a file exists and is not a directory before we
+// try using it to prevent further errors.
+func fileExists(filename string) bool {
+ info, err := os.Stat(filename)
+ if os.IsNotExist(err) {
+ return false
+ }
+ return !info.IsDir()
+}