util

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2022 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const DisableMetamorphicEnvVar = "COCKROACH_INTERNAL_DISABLE_METAMORPHIC_TESTING"

DisableMetamorphicEnvVar can be used to disable metamorphic tests for sub-processes. If it exists and is set to something truthy as defined by strconv.ParseBool then metamorphic testing will not be enabled.

View Source
const RaceEnabled = false

RaceEnabled is true if CockroachDB was built with the race build tag.

Variables

View Source
var (
	ErrInvalidLengthUnresolvedAddr        = fmt.Errorf("proto: negative length found during unmarshaling")
	ErrIntOverflowUnresolvedAddr          = fmt.Errorf("proto: integer overflow")
	ErrUnexpectedEndOfGroupUnresolvedAddr = fmt.Errorf("proto: unexpected end of group")
)
View Source
var TestAddr = NewUnresolvedAddr("tcp", "127.0.0.1:0")

TestAddr is an address to use for test servers. Listening on port 0 causes the kernel to allocate an unused port.

Functions

func CRC32

func CRC32(data []byte) uint32

CRC32 computes the Castagnoli CRC32 of the given data.

func CombineUniqueInt64

func CombineUniqueInt64(a []int64, b []int64) []int64

CombineUniqueInt64 combines two ordered int64 slices and returns the result without duplicates.

func CombineUniqueString

func CombineUniqueString(a []string, b []string) []string

CombineUniqueString combines two ordered string slices and returns the result without duplicates.

func ConstantWithMetamorphicTestBool

func ConstantWithMetamorphicTestBool(name string, defaultValue bool) bool

ConstantWithMetamorphicTestBool is like ConstantWithMetamorphicTestValue except it returns the non-default value half of the time (if running a metamorphic build).

The given name is used for logging.

func ConstantWithMetamorphicTestChoice

func ConstantWithMetamorphicTestChoice(
	name string, defaultValue interface{}, otherValues ...interface{},
) interface{}

ConstantWithMetamorphicTestChoice is like ConstantWithMetamorphicTestValue except it returns a random choice (equally weighted) of the given values. The default value is included in the random choice.

The given name is used for logging.

func ConstantWithMetamorphicTestRange

func ConstantWithMetamorphicTestRange(name string, defaultValue, min, max int) int

ConstantWithMetamorphicTestRange is like ConstantWithMetamorphicTestValue except instead of returning a single metamorphic test value, it returns a random test value in the semi-open range [min, max).

The given name is used for logging.

func ConstantWithMetamorphicTestValue

func ConstantWithMetamorphicTestValue(name string, defaultValue, metamorphicValue int) int

ConstantWithMetamorphicTestValue should be used to initialize "magic constants" that should be varied during test scenarios to check for bugs at boundary conditions. When metamorphicBuild is true, the test value will be used with metamorphicValueProbability probability. In all other cases, the production ("default") value will be used. The constant must be a "metamorphic variable": changing it cannot affect the output of any SQL DMLs. It can only affect the way in which the data is retrieved or processed, because otherwise the main test corpus would fail if this flag were enabled.

An example of a "magic constant" that behaves this way is a batch size. Batch sizes tend to present testing problems, because often the logic that deals with what to do when a batch is finished is less likely to be exercised by simple unit tests that don't use enough data to fill up a batch.

For example, instead of writing:

const batchSize = 64

you should write:

var batchSize = util.ConstantWithMetamorphicTestValue("batch-size", 64, 1)

This will often give your code a batch size of 1 in the crdb_test build configuration, increasing the amount of exercise the edge conditions get.

The given name is used for logging.

func EnableRacePreemptionPoints

func EnableRacePreemptionPoints() func()

EnableRacePreemptionPoints enables goroutine preemption points declared with RacePreempt for builds using the race build tag.

func EqualPtrFields

func EqualPtrFields(src, dst reflect.Value, prefix string) []string

EqualPtrFields uses reflection to check two "mirror" structures for matching pointer fields that point to the same object. Used to verify cloning/deep copy functions.

Returns the names of equal pointer fields.

func GetSingleRune

func GetSingleRune(s string) (rune, error)

GetSingleRune decodes the string s as a single rune if possible.

func GetSmallTrace

func GetSmallTrace(skip int) string

GetSmallTrace returns a comma-separated string containing the top 5 callers from a given skip level.

func IsMetamo1rphicBuild added in v0.0.2

func IsMetamo1rphicBuild() bool

IsMetamorphicBuild returns whether this build is metamorphic. By build being "metamorphic" we mean that some magic constants in the codebase might get initialized to non-default value. A build will become metamorphic with metamorphicBuildProbability probability if 'crdb_test' build flag is specified (this is the case for all test targets).

func MoveTopKToFront

func MoveTopKToFront(data sort.Interface, k int)

MoveTopKToFront moves the top K elements to the front. It makes O(n) calls to data.Less and data.Swap (with very high probability). It uses Hoare's selection algorithm (aka quickselect).

func Pluralize

func Pluralize(n int64) string

Pluralize returns a single character 's' unless n == 1.

func RacePreempt

func RacePreempt()

RacePreempt adds a goroutine preemption point if CockroachDB was built with the race build tag and preemption points have been enabled. The function is a no-op (and should be optimized out through dead code elimination) if the race build tag was not used.

func RemoveTrailingSpaces

func RemoveTrailingSpaces(input string) string

RemoveTrailingSpaces splits the input string into lines, trims any trailing spaces from each line, then puts the lines back together.

Any newlines at the end of the input string are ignored.

The output string always ends in a newline.

func ToLowerSingleByte

func ToLowerSingleByte(b byte) byte

ToLowerSingleByte returns the lowercase of a given single ASCII byte. A non ASCII byte is returned unchanged.

func TruncateString

func TruncateString(s string, maxRunes int) string

TruncateString truncates a string to a given number of runes.

Types

type EveryN

type EveryN struct {
	// N is the minimum duration of time between log messages.
	N time.Duration

	syncutil.Mutex
	// contains filtered or unexported fields
}

EveryN provides a way to rate limit spammy events. It tracks how recently a given event has occurred so that it can determine whether it's worth handling again.

The zero value for EveryN is usable and is equivalent to Every(0), meaning that all calls to ShouldProcess will return true.

NOTE: If you specifically care about log messages, you should use the version of this in the log package, as it integrates with the verbosity flags.

func Every

func Every(n time.Duration) EveryN

Every is a convenience constructor for an EveryN object that allows a log message every n duration.

func (*EveryN) ShouldProcess

func (e *EveryN) ShouldProcess(now time.Time) bool

ShouldProcess returns whether it's been more than N time since the last event.

type FNV64

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

FNV64 encapsulates the hash state.

func MakeFNV64

func MakeFNV64() FNV64

MakeFNV64 initializes a new FNV64 hash state.

func (*FNV64) Add

func (f *FNV64) Add(c uint64)

Add modifies the underlying FNV64 state by accumulating the given integer hash to the existing state.

func (*FNV64) Init

func (f *FNV64) Init()

Init initializes FNV64 to starting value.

func (*FNV64) IsInitialized

func (f *FNV64) IsInitialized() bool

IsInitialized returns true if the hash struct was initialized, which happens automatically when created through MakeFNV64 above.

func (*FNV64) Sum

func (f *FNV64) Sum() uint64

Sum returns the hash value accumulated till now.

type FastIntMap

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

FastIntMap is a replacement for map[int]int which is more efficient when both keys and values are small. It can be passed by value (but Copy must be used for independent modification of copies).

func (FastIntMap) ContentsIntoBuffer

func (m FastIntMap) ContentsIntoBuffer(buf *bytes.Buffer)

ContentsIntoBuffer writes the contents of the map into the provided buffer in the following format:

key1:val1 key2:val2 ...

The keys are in ascending order.

func (FastIntMap) Copy

func (m FastIntMap) Copy() FastIntMap

Copy returns a FastIntMap that can be independently modified.

func (FastIntMap) Empty

func (m FastIntMap) Empty() bool

Empty returns true if the map is empty.

func (FastIntMap) ForEach

func (m FastIntMap) ForEach(fn func(key, val int))

ForEach calls the given function for each key/value pair in the map (in arbitrary order).

func (FastIntMap) Get

func (m FastIntMap) Get(key int) (value int, ok bool)

Get returns the current value mapped to key, or (-1, false) if the key is unmapped.

func (FastIntMap) GetDefault

func (m FastIntMap) GetDefault(key int) (value int)

GetDefault returns the current value mapped to key, or 0 if the key is unmapped.

func (FastIntMap) Len

func (m FastIntMap) Len() int

Len returns the number of keys in the map.

func (FastIntMap) MaxKey

func (m FastIntMap) MaxKey() (_ int, ok bool)

MaxKey returns the maximum key that is in the map. If the map is empty, returns ok=false.

func (FastIntMap) MaxValue

func (m FastIntMap) MaxValue() (_ int, ok bool)

MaxValue returns the maximum value that is in the map. If the map is empty, returns (0, false).

func (*FastIntMap) Set

func (m *FastIntMap) Set(key, val int)

Set maps a key to the given value.

func (FastIntMap) String

func (m FastIntMap) String() string

String prints out the contents of the map in the following format:

map[key1:val1 key2:val2 ...]

The keys are in ascending order.

func (*FastIntMap) Unset

func (m *FastIntMap) Unset(key int)

Unset unmaps the given key.

type FastIntSet

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

FastIntSet keeps track of a set of integers. It does not perform any allocations when the values are small. It is not thread-safe.

func MakeFastIntSet

func MakeFastIntSet(vals ...int) FastIntSet

MakeFastIntSet returns a set initialized with the given values.

func (*FastIntSet) Add

func (s *FastIntSet) Add(i int)

Add adds a value to the set. No-op if the value is already in the set. If the large set is not nil and the value is within the range [0, 63], the value is added to both the large and small sets.

func (*FastIntSet) AddRange

func (s *FastIntSet) AddRange(from, to int)

AddRange adds values 'from' up to 'to' (inclusively) to the set. E.g. AddRange(1,5) adds the values 1, 2, 3, 4, 5 to the set. 'to' must be >= 'from'. AddRange is always more efficient than individual Adds.

func (FastIntSet) Contains

func (s FastIntSet) Contains(i int) bool

Contains returns true if the set contains the value.

func (FastIntSet) Copy

func (s FastIntSet) Copy() FastIntSet

Copy returns a copy of s which can be modified independently.

func (*FastIntSet) CopyFrom

func (s *FastIntSet) CopyFrom(other FastIntSet)

CopyFrom sets the receiver to a copy of other, which can then be modified independently.

func (*FastIntSet) Decode

func (s *FastIntSet) Decode(br io.ByteReader) error

Decode does the opposite of Encode. The contents of the receiver are overwritten.

func (FastIntSet) Difference

func (s FastIntSet) Difference(rhs FastIntSet) FastIntSet

Difference returns the elements of s that are not in rhs as a new set.

func (*FastIntSet) DifferenceWith

func (s *FastIntSet) DifferenceWith(rhs FastIntSet)

DifferenceWith removes any elements in rhs from this set.

func (FastIntSet) Empty

func (s FastIntSet) Empty() bool

Empty returns true if the set is empty.

func (*FastIntSet) Encode

func (s *FastIntSet) Encode(buf *bytes.Buffer) error

Encode the set and write it to a bytes.Buffer using binary.varint byte encoding.

This method cannot be used if the set contains negative elements.

If the set has only elements in the range [0, 63], we encode a 0 followed by a 64-bit bitmap. Otherwise, we encode a length followed by each element.

WARNING: this is used by plan gists, so if this encoding changes, explain.gistVersion needs to be bumped.

func (FastIntSet) Equals

func (s FastIntSet) Equals(rhs FastIntSet) bool

Equals returns true if the two sets are identical.

func (FastIntSet) ForEach

func (s FastIntSet) ForEach(f func(i int))

ForEach calls a function for each value in the set (in increasing order).

func (FastIntSet) Intersection

func (s FastIntSet) Intersection(rhs FastIntSet) FastIntSet

Intersection returns the intersection of s and rhs as a new set.

func (*FastIntSet) IntersectionWith

func (s *FastIntSet) IntersectionWith(rhs FastIntSet)

IntersectionWith removes any elements not in rhs from this set.

func (FastIntSet) Intersects

func (s FastIntSet) Intersects(rhs FastIntSet) bool

Intersects returns true if s has any elements in common with rhs.

func (FastIntSet) Len

func (s FastIntSet) Len() int

Len returns the number of the elements in the set.

func (FastIntSet) Next

func (s FastIntSet) Next(startVal int) (int, bool)

Next returns the first value in the set which is >= startVal. If there is no value, the second return value is false.

func (FastIntSet) Ordered

func (s FastIntSet) Ordered() []int

Ordered returns a slice with all the integers in the set, in increasing order.

func (*FastIntSet) Remove

func (s *FastIntSet) Remove(i int)

Remove removes a value from the set. No-op if the value is not in the set.

func (FastIntSet) String

func (s FastIntSet) String() string

String returns a list representation of elements. Sequential runs of positive numbers are shown as ranges. For example, for the set {0, 1, 2, 5, 6, 10}, the output is "(0-2,5,6,10)".

func (FastIntSet) SubsetOf

func (s FastIntSet) SubsetOf(rhs FastIntSet) bool

SubsetOf returns true if rhs contains all the elements in s.

func (FastIntSet) Union

func (s FastIntSet) Union(rhs FastIntSet) FastIntSet

Union returns the union of s and rhs as a new set.

func (*FastIntSet) UnionWith

func (s *FastIntSet) UnionWith(rhs FastIntSet)

UnionWith adds all the elements from rhs to this set.

type NoCopy

type NoCopy struct{}

NoCopy may be embedded into structs which must not be copied after the first use.

See https://github.com/golang/go/issues/8005#issuecomment-190753527 for details.

func (*NoCopy) Lock

func (*NoCopy) Lock()

Lock is a no-op used by -copylocks checker from `go vet`.

func (*NoCopy) Unlock

func (*NoCopy) Unlock()

Unlock is a no-op used by -copylocks checker from `go vet`.

type StringListBuilder

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

StringListBuilder helps printing out lists of items. See MakeStringListBuilder.

func MakeStringListBuilder

func MakeStringListBuilder(begin, separator, end string) StringListBuilder

MakeStringListBuilder creates a StringListBuilder, which is used to print out lists of items. Sample usage:

b := MakeStringListBuilder("(", ", ", ")")
b.Add(&buf, "x")
b.Add(&buf, "y")
b.Finish(&buf) // By now, we wrote "(x, y)".

If Add is not called, nothing is written.

func (*StringListBuilder) Add

func (b *StringListBuilder) Add(w io.Writer, val string)

Add an item to the list.

func (*StringListBuilder) Addf

func (b *StringListBuilder) Addf(w io.Writer, format string, args ...interface{})

Addf is a format variant of Add.

func (*StringListBuilder) Finish

func (b *StringListBuilder) Finish(w io.Writer)

Finish must be called after all the elements have been added.

type UnresolvedAddr

type UnresolvedAddr struct {
	NetworkField string `protobuf:"bytes,1,opt,name=network_field,json=networkField" json:"network_field"`
	AddressField string `protobuf:"bytes,2,opt,name=address_field,json=addressField" json:"address_field"`
}

UnresolvedAddr is an unresolved version of net.Addr.

var IsolatedTestAddr *UnresolvedAddr

IsolatedTestAddr is an address to use for tests that need extra isolation by using more addresses than 127.0.0.1 (support for this is platform-specific and only enabled on Linux). Both TestAddr and IsolatedTestAddr guarantee that the chosen port is not in use when allocated, but IsolatedTestAddr draws from a larger pool of addresses so that when tests are run in a tight loop the system is less likely to run out of available ports or give a port to one test immediately after it was closed by another.

IsolatedTestAddr should be used for tests that open and close a large number of sockets, or tests which stop a server and rely on seeing a "connection refused" error afterwards. It cannot be used with tests that operate in secure mode since our test certificates are only valid for 127.0.0.1.

func MakeUnresolvedAddr

func MakeUnresolvedAddr(network, addr string) UnresolvedAddr

MakeUnresolvedAddr populates an UnresolvedAddr from a network and raw address string.

func NewUnresolvedAddr

func NewUnresolvedAddr(network, addr string) *UnresolvedAddr

NewUnresolvedAddr creates a new UnresolvedAddr from a network and raw address string.

func (*UnresolvedAddr) Descriptor

func (*UnresolvedAddr) Descriptor() ([]byte, []int)

func (*UnresolvedAddr) Equal

func (this *UnresolvedAddr) Equal(that interface{}) bool

func (UnresolvedAddr) IsEmpty

func (a UnresolvedAddr) IsEmpty() bool

IsEmpty returns true if the address has no network or address specified.

func (*UnresolvedAddr) Marshal

func (m *UnresolvedAddr) Marshal() (dAtA []byte, err error)

func (*UnresolvedAddr) MarshalTo

func (m *UnresolvedAddr) MarshalTo(dAtA []byte) (int, error)

func (*UnresolvedAddr) MarshalToSizedBuffer

func (m *UnresolvedAddr) MarshalToSizedBuffer(dAtA []byte) (int, error)

func (*UnresolvedAddr) Network

func (a *UnresolvedAddr) Network() string

Network returns the address's network name.

func (*UnresolvedAddr) ProtoMessage

func (*UnresolvedAddr) ProtoMessage()

func (*UnresolvedAddr) Reset

func (m *UnresolvedAddr) Reset()

func (UnresolvedAddr) Resolve

func (a UnresolvedAddr) Resolve() (net.Addr, error)

Resolve attempts to resolve a into a net.Addr.

func (*UnresolvedAddr) Size

func (m *UnresolvedAddr) Size() (n int)

func (UnresolvedAddr) String

func (a UnresolvedAddr) String() string

String returns the address's string form.

func (*UnresolvedAddr) Unmarshal

func (m *UnresolvedAddr) Unmarshal(dAtA []byte) error

func (*UnresolvedAddr) XXX_DiscardUnknown

func (m *UnresolvedAddr) XXX_DiscardUnknown()

func (*UnresolvedAddr) XXX_Marshal

func (m *UnresolvedAddr) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*UnresolvedAddr) XXX_Merge

func (m *UnresolvedAddr) XXX_Merge(src proto.Message)

func (*UnresolvedAddr) XXX_Size

func (m *UnresolvedAddr) XXX_Size() int

func (*UnresolvedAddr) XXX_Unmarshal

func (m *UnresolvedAddr) XXX_Unmarshal(b []byte) error

Directories

Path Synopsis
csv
Package csv reads and writes comma-separated values (CSV) files.
Package csv reads and writes comma-separated values (CSV) files.
Package pretty prints documents based on a target line width.
Package pretty prints documents based on a target line width.
singleflight
Package singleflight provides a duplicate function call suppression mechanism.
Package singleflight provides a duplicate function call suppression mechanism.
pgdate
Package pgdate contains parsing functions and types for dates and times in a manner that is compatible with PostgreSQL.
Package pgdate contains parsing functions and types for dates and times in a manner that is compatible with PostgreSQL.

Jump to

Keyboard shortcuts

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