Files
kopia/tests/robustness/multiclient_test/framework/client.go
Jarek Kowalski 8a4ac4dec3 Upgraded linter to 1.43.0 (#1505)
* fixed new gocritic violations
* fixed new 'contextcheck' violations
* fixed 'gosec' warnings
* suppressed ireturn and varnamelen linters
* fixed tenv violations, enabled building robustness tests on arm64
* fixed remaining linux failures
* makefile: fixed 'lint-all' target when running on arm64
* linter: increase deadline
* disable nilnil linter - to be enabled in separate PR
2021-11-11 17:03:11 -08:00

52 lines
1.1 KiB
Go

//go:build darwin || (linux && amd64)
// +build darwin linux,amd64
package framework
import (
"context"
petname "github.com/dustinkirkland/golang-petname"
"github.com/google/uuid"
)
const nameLen int = 2
var clientKey = struct{}{}
// Client is a unique client for use in multiclient robustness tests.
type Client struct {
ID string
}
func init() {
petname.NonDeterministicMode()
}
func newClient() *Client {
return &Client{
ID: petname.Generate(nameLen, "-") + "-" + uuid.NewString(),
}
}
// NewClientContext returns a copy of ctx with a new client.
func NewClientContext(ctx context.Context) context.Context {
return context.WithValue(ctx, clientKey, newClient())
}
// NewClientContexts returns copies of ctx with n new clients.
func NewClientContexts(ctx context.Context, n int) []context.Context {
ctxs := make([]context.Context, n)
for i := range ctxs {
ctxs[i] = NewClientContext(ctx)
}
return ctxs
}
// UnwrapContext returns a client from the given context.
func UnwrapContext(ctx context.Context) *Client {
c, _ := ctx.Value(clientKey).(*Client)
return c
}