mirror of
https://github.com/hanzoai/tasks.git
synced 2026-10-30 14:56:19 +00:00
## What changed?
<!-- Describe what has changed in this PR -->
Remove license header from every file. Because it is really hard to
follow in this PR here is the summary:
1. License header is removed from all `.go` and `.proto` files
:fireworks::fireworks:🎆.
2. `LICENSE` file in the root directory has only Temporal and Uber
copyrights.
3. 5 other `LICENSE` files added to the packages which have copyrights
different from Temporal and Uber: Datadog, Xargin, "Mat Ryer, Tyler
Bunnell and contributors".
4. `license_file` flag is removed from all code generation tools.
5. `copyright_file` flag is removed from `go:generate mockgen`
directive.
6. All copyright related targets are removed from `Makefile`.
7. Updated Temporal copyright year to 2025 everywhere.
## Why?
<!-- Tell your future self why have you made these changes -->
I double checked with legal department that it is not needed to have
license header in every file. One file per repo is enough. I put all
copyrights to the root `LICENSE` file and removed header from all other
files. Also updated tools and `Makefile`.
173 lines
5.1 KiB
Go
173 lines
5.1 KiB
Go
// Package temporaltest provides utilities for end to end Temporal server testing.
|
|
package temporaltest
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"math/rand"
|
|
"testing"
|
|
"time"
|
|
|
|
"go.temporal.io/sdk/client"
|
|
"go.temporal.io/sdk/worker"
|
|
"go.temporal.io/server/common/dynamicconfig"
|
|
"go.temporal.io/server/common/log"
|
|
"go.temporal.io/server/temporal"
|
|
temporalite "go.temporal.io/server/temporaltest/internal"
|
|
)
|
|
|
|
// A TestServer is a Temporal server listening on a system-chosen port on the
|
|
// local loopback interface, for use in end-to-end tests.
|
|
//
|
|
// Methods on TestServer are not safe for concurrent use.
|
|
type TestServer struct {
|
|
server *temporalite.LiteServer
|
|
defaultTestNamespace string
|
|
defaultClient client.Client
|
|
clients []client.Client
|
|
workers []worker.Worker
|
|
t *testing.T
|
|
defaultClientOptions client.Options
|
|
defaultWorkerOptions worker.Options
|
|
serverOptions []temporal.ServerOption
|
|
}
|
|
|
|
func (ts *TestServer) fatal(err error) {
|
|
if ts.t == nil {
|
|
panic(err)
|
|
}
|
|
ts.t.Fatal(err)
|
|
}
|
|
|
|
// NewWorker registers and starts a Temporal worker on the specified task queue.
|
|
func (ts *TestServer) NewWorker(taskQueue string, registerFunc func(registry worker.Registry)) worker.Worker {
|
|
return ts.NewWorkerWithOptions(taskQueue, registerFunc, ts.defaultWorkerOptions)
|
|
}
|
|
|
|
// NewWorkerWithOptions returns a Temporal worker on the specified task queue.
|
|
//
|
|
// WorkflowPanicPolicy is always set to worker.FailWorkflow so that workflow executions
|
|
// fail fast when workflow code panics or detects non-determinism.
|
|
func (ts *TestServer) NewWorkerWithOptions(taskQueue string, registerFunc func(registry worker.Registry), opts worker.Options) worker.Worker {
|
|
opts.WorkflowPanicPolicy = worker.FailWorkflow
|
|
|
|
w := worker.New(ts.GetDefaultClient(), taskQueue, opts)
|
|
registerFunc(w)
|
|
ts.workers = append(ts.workers, w)
|
|
|
|
if err := w.Start(); err != nil {
|
|
ts.fatal(err)
|
|
}
|
|
|
|
return w
|
|
}
|
|
|
|
// GetDefaultClient returns the default Temporal client configured for making requests to the server.
|
|
//
|
|
// It is configured to use a pre-registered test namespace and will be closed on TestServer.Stop.
|
|
func (ts *TestServer) GetDefaultClient() client.Client {
|
|
if ts.defaultClient == nil {
|
|
ts.defaultClient = ts.NewClientWithOptions(ts.defaultClientOptions)
|
|
}
|
|
return ts.defaultClient
|
|
}
|
|
|
|
// GetDefaultNamespace returns the randomly generated namespace which has been pre-registered with the test server.
|
|
func (ts *TestServer) GetDefaultNamespace() string {
|
|
return ts.defaultTestNamespace
|
|
}
|
|
|
|
// GetFrontendHostPort returns the host:port for this server.
|
|
//
|
|
// When constructing a Temporal client from within the same process,
|
|
// GetDefaultClient or NewClientWithOptions should be used instead.
|
|
func (ts *TestServer) GetFrontendHostPort() string {
|
|
return ts.server.FrontendHostPort()
|
|
}
|
|
|
|
// NewClientWithOptions returns a new Temporal client configured for making requests to the server.
|
|
//
|
|
// If no namespace option is set it will use a pre-registered test namespace.
|
|
// The returned client will be closed on TestServer.Stop.
|
|
func (ts *TestServer) NewClientWithOptions(opts client.Options) client.Client {
|
|
if opts.Namespace == "" {
|
|
opts.Namespace = ts.defaultTestNamespace
|
|
}
|
|
if opts.Logger == nil {
|
|
opts.Logger = &testLogger{ts.t}
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
|
|
c, err := ts.server.NewClientWithOptions(ctx, opts)
|
|
if err != nil {
|
|
ts.fatal(fmt.Errorf("error creating client: %w", err))
|
|
}
|
|
|
|
ts.clients = append(ts.clients, c)
|
|
|
|
return c
|
|
}
|
|
|
|
// Stop closes test clients and shuts down the server.
|
|
func (ts *TestServer) Stop() {
|
|
for _, w := range ts.workers {
|
|
w.Stop()
|
|
}
|
|
for _, c := range ts.clients {
|
|
c.Close()
|
|
}
|
|
if err := ts.server.Stop(); err != nil {
|
|
// Log instead of throwing error because there's no need to fail the test
|
|
// if it already succeeded.
|
|
ts.t.Logf("error shutting down Temporal server: %s", err)
|
|
}
|
|
}
|
|
|
|
// NewServer starts and returns a new TestServer.
|
|
//
|
|
// If not specifying the WithT option, the caller should execute Stop when finished to close
|
|
// the server and release resources.
|
|
func NewServer(opts ...TestServerOption) *TestServer {
|
|
testNamespace := fmt.Sprintf("temporaltest-%d", rand.Intn(1e6))
|
|
|
|
ts := TestServer{
|
|
defaultTestNamespace: testNamespace,
|
|
}
|
|
|
|
// Apply options
|
|
for _, opt := range opts {
|
|
opt.apply(&ts)
|
|
}
|
|
|
|
if ts.t != nil {
|
|
ts.t.Cleanup(ts.Stop)
|
|
}
|
|
|
|
s, err := temporalite.NewLiteServer(&temporalite.LiteServerConfig{
|
|
Namespaces: []string{ts.defaultTestNamespace},
|
|
Ephemeral: true,
|
|
Logger: log.NewNoopLogger(),
|
|
DynamicConfig: dynamicconfig.StaticClient{
|
|
dynamicconfig.ForceSearchAttributesCacheRefreshOnRead.Key(): []dynamicconfig.ConstrainedValue{{Value: true}},
|
|
},
|
|
// Disable "accept incoming network connections?" prompt on macOS
|
|
FrontendIP: "127.0.0.1",
|
|
}, ts.serverOptions...)
|
|
if err != nil {
|
|
ts.fatal(fmt.Errorf("error creating server: %w", err))
|
|
}
|
|
ts.server = s
|
|
|
|
// Start does not block as long as InterruptOn is unset.
|
|
if err := s.Start(); err != nil {
|
|
ts.fatal(err)
|
|
}
|
|
|
|
// This sleep helps avoid a panic in github.com/temporalio/[email protected]/swim/labels.go:175
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
return &ts
|
|
}
|