utils

package
v0.0.0-...-020e20f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 1, 2022 License: Apache-2.0 Imports: 25 Imported by: 0

Documentation

Overview

Package utils provides useful standalone function for test and network, etc.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotEnoughPorts defines error indicating random port allocation failure.
	ErrNotEnoughPorts = errors.New("not enough ports in port range")
)

FJ is short for filepath.Join.

Functions

func ConcatAll

func ConcatAll(args ...[]byte) []byte

ConcatAll concatenate several bytes slice into one.

func CopyFile

func CopyFile(src, dst string) (int64, error)

CopyFile copies from src to dst until either EOF is reached on src or an error occurs. It verifies src exists and removes the dst if it exists.

func DecodeMsgPack

func DecodeMsgPack(buf []byte, out interface{}) error

DecodeMsgPack reverses the encode operation on a byte slice input.

func DecodeMsgPackPlain

func DecodeMsgPackPlain(buf []byte, out interface{}) error

DecodeMsgPackPlain reverses the encode operation on a byte slice input without RawToString setting.

func DupConf

func DupConf(confFile string, newConfFile string) (err error)

DupConf duplicate conf file using random new listen addr to avoid failure on concurrent test cases.

func EncodeMsgPack

func EncodeMsgPack(in interface{}) (*bytes.Buffer, error)

EncodeMsgPack writes an encoded object to a new bytes buffer.

func Exist

func Exist(path string) bool

Exist return if file or path is exist.

func GetMsgPackClientCodec

func GetMsgPackClientCodec(c io.ReadWriteCloser) rpc.ClientCodec

GetMsgPackClientCodec returns msgpack client codec for connection.

func GetMsgPackServerCodec

func GetMsgPackServerCodec(c io.ReadWriteCloser) rpc.ServerCodec

GetMsgPackServerCodec returns msgpack server codec for connection.

func GetProjectSrcDir

func GetProjectSrcDir() string

GetProjectSrcDir gets the src code root.

func GetRandomPorts

func GetRandomPorts(bindAddr string, minPort, maxPort, count int) (ports []int, err error)

GetRandomPorts returns available random ports, previously allocated ports will be ignored.

func HomeDirExpand

func HomeDirExpand(path string) string

HomeDirExpand tries to expand the tilde (~) in the front of a path to a fullpath directory.

func PaddedBigBytes

func PaddedBigBytes(bigint *big.Int, n int) []byte

PaddedBigBytes encodes a big integer as a big-endian byte slice. The length of the slice is at least n bytes.

func RegisterInterfaceToMsgPack

func RegisterInterfaceToMsgPack(intf, impl reflect.Type) (err error)

RegisterInterfaceToMsgPack binds interface decode/encode to specified implementation.

func RemoveAll

func RemoveAll(pattern string)

RemoveAll removes files using glob.

func RemoveDuplicatesUnordered

func RemoveDuplicatesUnordered(elements []string) []string

RemoveDuplicatesUnordered de-duplicate elements in string slice.

func RunCommand

func RunCommand(bin string, args []string, processName string, workingDir string, logDir string, toStd bool) (err error)

RunCommand runs a command and capture its output to a log file,

if toStd is true also output to stdout and stderr.

func StartProfile

func StartProfile(cpuprofile, memprofile string) error

StartProfile initializes the CPU and memory profile, if specified.

func StopProfile

func StopProfile()

StopProfile closes the CPU and memory profiles if they are running.

func WaitForExit

func WaitForExit() <-chan os.Signal

WaitForExit waits for user cancellation signals: SIGINT/SIGTERM and ignore SIGHUP/SIGTTIN/SIGTTOU.

func WaitForPorts

func WaitForPorts(ctx context.Context, bindAddr string, ports []int, interval time.Duration) (err error)

WaitForPorts returns only when port is ready to listen or canceled by context.

func WaitToConnect

func WaitToConnect(ctx context.Context, bindAddr string, ports []int, interval time.Duration) (err error)

WaitToConnect returns only when port is ready to connect or canceled by context.

Types

type CMD

type CMD struct {
	Cmd     *exec.Cmd
	LogPath string
	LogFD   *os.File
}

CMD is the struct holding exec.Cmd and log path.

func RunCommandNB

func RunCommandNB(bin string, args []string, processName string, workingDir string, logDir string, toStd bool) (cmd *CMD, err error)

RunCommandNB starts a non-blocking command.

type Once

type Once struct {
	// contains filtered or unexported fields
}

Once refactors sync.Once and add Reset support.

func (*Once) Do

func (o *Once) Do(f func())

Do handles once logic.

func (*Once) Reset

func (o *Once) Reset()

Reset make the once object to be initialized once again.

Directories

Path Synopsis
log

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL