-
Notifications
You must be signed in to change notification settings - Fork 11
/
utils.go
100 lines (86 loc) · 2 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
* Copyright (c) 2020-present unTill Pro, Ltd.
* @author Denis Gribanov
*/
package coreutils
import (
"bytes"
"errors"
"fmt"
"os"
"strings"
"syscall"
"time"
"github.com/voedger/voedger/pkg/istructs"
)
func IsBlank(str string) bool {
return len(strings.TrimSpace(str)) == 0
}
// https://github.com/golang/go/issues/27169
func ResetTimer(t *time.Timer, timeout time.Duration) {
if !t.Stop() {
select {
case <-t.C:
default:
}
}
t.Reset(timeout)
}
func IsTest() bool {
return strings.Contains(os.Args[0], ".test") || IsDebug()
}
func IsDebug() bool {
return strings.Contains(os.Args[0], "__debug_bin")
}
func IsCassandraStorage() bool {
_, ok := os.LookupEnv("CASSANDRA_TESTS_ENABLED")
return ok
}
func IsDynamoDBStorage() bool {
_, ok := os.LookupEnv("DYNAMODB_TESTS_ENABLED")
return ok
}
func ServerAddress(port int) string {
addr := ""
if IsTest() {
addr = "127.0.0.1"
}
return fmt.Sprintf("%s:%d", addr, port)
}
func SplitErrors(joinedError error) (errs []error) {
if joinedError != nil {
var pErr IErrUnwrapper
if errors.As(joinedError, &pErr) {
return pErr.Unwrap()
}
return []error{joinedError}
}
return
}
func IsWSAEError(err error, errno syscall.Errno) bool {
var sysCallErr *os.SyscallError
if errors.As(err, &sysCallErr) {
var syscallErrno syscall.Errno
if errors.As(sysCallErr.Err, &syscallErrno) {
return syscallErrno == errno
}
}
return false
}
// used in BuildAppWorkspaces() only because there are no apps in IAppPartitions on that moment
func AppPartitionID(wsid istructs.WSID, numAppPartitions istructs.NumAppPartitions) istructs.PartitionID {
return istructs.PartitionID(int(wsid) % int(numAppPartitions))
}
func NilAdminPortGetter() int { panic("to be tested") }
func ScanSSE(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
}
if i := bytes.Index(data, []byte("\n\n")); i >= 0 {
return i + 2, data[0:i], nil
}
if atEOF {
return len(data), data, nil
}
return 0, nil, nil
}