Files
kopia/tests/robustness/multiclient_test/framework/client.go
Steve Joachim d2b816934c robustness: add tests for kopia server repos (#1029)
* Add error handling for robustness Checker
* robustness engine updates for concurrency
* add client-server utils for multiclient robustness
* create client package for robustness tests
* create multi-client robustness test framework
* multi-client robustness test definitions
* update robustness test runner scripts
* Address unparam lint errors

Co-authored-by: Julio Lopez <julio+gh@kasten.io>
2021-04-29 21:45:13 -07:00

51 lines
1.0 KiB
Go

// +build darwin,amd64 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
}