util

package
v0.23.2 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2024 License: Apache-2.0 Imports: 29 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 CombineUnique

func CombineUnique[T constraints.Ordered](a, b []T) []T

CombineUnique merges two ordered slices. If both slices have unique elements then so does the resulting slice. More generally, each element is present max(timesInA, timesInB) times.

Takes ownership of both slices, and uses the longer one to store the result.

This function is used to combine slices where one of the slices is small or has mostly the same elements as the other. If the two slices are large and don't have many duplicates, this function should be avoided, because of the usage of `copy` that can increase CPU.

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 ConstantWithMetamorphicTestBoolWithoutLogging

func ConstantWithMetamorphicTestBoolWithoutLogging(name string, defaultValue bool) bool

ConstantWithMetamorphicTestBoolWithoutLogging is like ConstantWithMetamorphicTestBool except it does not log the value. This is necessary to work around this issue: https://github.com/cockroachdb/cockroachdb-parser/issues/106667 TODO(test-eng): Remove this variant when the issue above is addressed.

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 ExpandTabsInRedactableBytes

func ExpandTabsInRedactableBytes(s redact.RedactableBytes) (redact.RedactableBytes, error)

ExpandTabsInRedactableBytes expands tabs in the redactable byte slice, so that columns are aligned. The correctness of this function depends on the assumption that the `tabwriter` does not replace characters.

func Filter

func Filter[T any](collection []T, predicate func(T) bool) []T

Filter returns a new slice that only contains elements from collection that satisfy predicate.

// Filter in place
numbers = Filter(numbers, isEven)
// Filter into a new slice
odds := Filter(numbers, isEven)

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) redact.RedactableString

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

func IsMetamorphicBuild

func IsMetamorphicBuild() 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 Map

func Map[T, K any](collection []T, fn func(T) K) []K

Map returns a new slice containing the results of fn for each element within collection. Usage:

Map([]int{1, 2, 3}, func(i int) int {
	return i
})

func MapFrom

func MapFrom[T any, K comparable, V any](collection []T, fn func(T) (K, V)) map[K]V

MapFrom returns a map populated with keys and values returned by fn. Usage:

// Construct a set.
MapFrom(numbers, func(i int) (int, struct{}) {
	return i, struct{}{}
})

// Construct a map of numbers to their square.
MapFrom(numbers, func(i int) (int, int) {
	return i, i * i
})

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 RandString

func RandString(rng *rand.Rand, length int, alphabet string) string

RandString generates a random string of the desired length from the input alphabet.

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 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 MakeUnresolvedAddrWithDefaults

func MakeUnresolvedAddrWithDefaults(network, addr, defaultPort string) UnresolvedAddr

MakeUnresolvedAddrWithDefaults creates a new UnresolvedAddr from a network and raw address string, using the following defaults if not given:

- Network: tcp - Host: local hostname or 127.0.0.1 - Port: given default port

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
admission
admissionpb
Package admissionpb contains the base types for the admission package.
Package admissionpb contains the base types for the admission package.
Package buildutil provides a constant CrdbTestBuild.
Package buildutil provides a constant CrdbTestBuild.
Package encoding exposes some utilities for encoding data as bytes.
Package encoding exposes some utilities for encoding data as bytes.
Package grunning is a library that's able to retrieve on-CPU running time for individual goroutines.
Package grunning is a library that's able to retrieve on-CPU running time for individual goroutines.
Package interval provides two implementations for an interval tree.
Package interval provides two implementations for an interval tree.
log
netutil
Package pretty prints documents based on a target line width.
Package pretty prints documents based on a target line width.
gen
This binary takes the tzdata from Go's source and extracts all timezones names from them.
This binary takes the tzdata from Go's source and extracts all timezones names from them.
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