go.temporal.io/server/temporaltest/server.go

172 LOC · 75 covered · 97 uncovered · 17 ranges · 24 concepts · 6 introducers · 7 tests

File neighbourhood

The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the filego.temporal.io/server/api/matchingservice/v1/service_grpc.pb.go · 1911 LOCv1/service_grpc.pb.gogo.temporal.io/server/common/client_cache.go · 146 LOCcommon/client_cache.gogo.temporal.io/server/common/dynamicconfig/static_client.go · 31 LOCdynamicconfig/static_cli…go.temporal.io/server/common/namespace/namespace.go · 377 LOCnamespace/namespace.gogo.temporal.io/server/common/namespace/nsregistry/registry.go · 898 LOCnsregistry/registry.gogo.temporal.io/server/common/persistence/sql/sqlplugin/sqlite/visibility.go · 207 LOCsqlite/visibility.gogo.temporal.io/server/common/persistence/sql/sqlplugin/visibility.go · 336 LOCsqlplugin/visibility.gogo.temporal.io/server/common/persistence/visibility/store/sql/query_converter_legacy.go · 677 LOCsql/query_converter_lega…go.temporal.io/server/common/persistence/visibility/store/sql/query_converter_legacy_factory.go · 32 LOCsql/query_converter_lega…go.temporal.io/server/common/persistence/visibility/store/sql/query_converter_legacy_sqlite.go · 334 LOCsql/query_converter_lega…go.temporal.io/server/common/persistence/visibility/store/sql/visibility_store.go · 968 LOCsql/visibility_store.gogo.temporal.io/server/common/persistence/visibility/visibility_manager_impl.go · 619 LOCvisibility/visibility_ma…go.temporal.io/server/common/primitives/timestamp/duration.go · 88 LOCtimestamp/duration.gogo.temporal.io/server/common/searchattribute/mapper.go · 243 LOCsearchattribute/mapper.g…go.temporal.io/server/schema/sqlite/setup.go · 200 LOCsqlite/setup.gogo.temporal.io/server/service/frontend/fx.go · 1057 LOCfrontend/fx.gogo.temporal.io/server/service/frontend/service.go · 568 LOCfrontend/service.gogo.temporal.io/server/service/history/api/respondworkflowtaskcompleted/workflow_task_completed_handler.go · 1625 LOCrespondworkflowtaskcompl…go.temporal.io/server/service/matching/handler.go · 726 LOCmatching/handler.gogo.temporal.io/server/service/matching/pri_matcher.go · 702 LOCmatching/pri_matcher.gogo.temporal.io/server/service/matching/task.go · 394 LOCmatching/task.gogo.temporal.io/server/temporal/server_option.go · 227 LOCtemporal/server_option.g…go.temporal.io/server/temporaltest/internal/lite_server.go · 375 LOCinternal/lite_server.gogo.temporal.io/server/temporaltest/logger.go · 41 LOCtemporaltest/logger.gogo.temporal.io/server/temporaltest/options.go · 52 LOCtemporaltest/options.goworkflow_handler.go ×11 · 117 introduced LOCworkflow_handler.go ×11common.go ×1 · 11 introduced LOCcommon.go ×1request_response.pb.go ×6 · 246 introduced LOCrequest_response.pb.go ×…visibility_store.go ×17 · 171 introduced LOCvisibility_store.go ×17telemetry.go ×2 · 15 introduced LOCtelemetry.go ×2TestNewServer · 0 introduced LOCTestNewServerpri_matcher.go ×1 · 2 introduced LOCpri_matcher.go ×1timer_queue_active_task_executor.go ×1 · 3 introduced LOCtimer_queue_active_task_…workflow_task_completed_handler.go ×9 · 75 introduced LOCworkflow_task_completed_…pri_forwarder.go ×2 · 4 introduced LOCpri_forwarder.go ×2request_response.pb.go ×1 · 7 introduced LOCrequest_response.pb.go ×…logger.go ×1 · 5 introduced LOClogger.go ×1logger.go ×1 · 2 introduced LOClogger.go ×1pri_matcher.go ×1 · 4 introduced LOCpri_matcher.go ×1workflow_handler.go ×8 · 144 introduced LOCworkflow_handler.go ×8pri_matcher.go ×1 · 2 introduced LOCpri_matcher.go ×1handler.go ×1 · 20 introduced LOChandler.go ×1pri_matcher.go ×8 · 55 introduced LOCpri_matcher.go ×8matching_engine.go ×1 · 8 introduced LOCmatching_engine.go ×1server.go ×1 · 3 introduced LOCserver.go ×1logger.go ×2 · 21 introduced LOClogger.go ×2endpoint_registry.go ×2 · 26 introduced LOCendpoint_registry.go ×2server.go ×3 · 13 introduced LOCserver.go ×3lite_server.go ×25 · 303 introduced LOClite_server.go ×25ExampleNewServer · introduced test · go.temporal.io/server/temporaltest/ExampleNewServerExampleNewServerTestBaseServerOptions · introduced test · go.temporal.io/server/temporaltest/TestBaseServerOptionsTestBaseServerOptionsTestClientWithCustomInterceptor · introduced test · go.temporal.io/server/temporaltest/TestClientWithCustomInterceptorTestClientWithCustomInte…TestDefaultWorkerOptions · introduced test · go.temporal.io/server/temporaltest/TestDefaultWorkerOptionsTestDefaultWorkerOptionsTestNewServer · introduced test · go.temporal.io/server/temporaltest/TestNewServerTestNewServerTestNewWorkerWithOptions · introduced test · go.temporal.io/server/temporaltest/TestNewWorkerWithOptionsTestNewWorkerWithOptionsTestSearchAttributeRegistration · introduced test · go.temporal.io/server/temporaltest/TestSearchAttributeRegistrationTestSearchAttributeRegis…Focused file · go.temporal.io/server/temporaltest/server.go · 172 LOCtemporaltest/server.go

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.

1 // Package temporaltest provides utilities for end to end Temporal server testing.
2 package temporaltest
3
4 import (
5 "context"
6 "fmt"
7 "math/rand"
8 "testing"
9 "time"
10
11 "go.temporal.io/sdk/client"
12 "go.temporal.io/sdk/worker"
13 "go.temporal.io/server/common/dynamicconfig"
14 "go.temporal.io/server/common/log"
15 "go.temporal.io/server/temporal"
16 temporalite "go.temporal.io/server/temporaltest/internal"
17 )
18
19 // A TestServer is a Temporal server listening on a system-chosen port on the
20 // local loopback interface, for use in end-to-end tests.
21 //
22 // Methods on TestServer are not safe for concurrent use.
23 type TestServer struct {
24 server *temporalite.LiteServer
25 defaultTestNamespace string
26 defaultClient client.Client
27 clients []client.Client
28 workers []worker.Worker
29 t *testing.T
30 defaultClientOptions client.Options
31 defaultWorkerOptions worker.Options
32 serverOptions []temporal.ServerOption
33 }
34
35 func (ts *TestServer) fatal(err error) {
36 if ts.t == nil {
37 panic(err)
38 }
39 ts.t.Fatal(err)
40 }
41
42 // NewWorker registers and starts a Temporal worker on the specified task queue.
43 > func (ts *TestServer) NewWorker(taskQueue string, registerFunc func(registry worker.Registry)) worker.Worker { server.go ×1
44 > return ts.NewWorkerWithOptions(taskQueue, registerFunc, ts.defaultWorkerOptions)
45 > }
46
47 // NewWorkerWithOptions returns a Temporal worker on the specified task queue.
48 //
49 // WorkflowPanicPolicy is always set to worker.FailWorkflow so that workflow executions
50 // fail fast when workflow code panics or detects non-determinism.
51 > func (ts *TestServer) NewWorkerWithOptions(taskQueue string, registerFunc func(registry worker.Registry), opts worker.Options) worker.Worker { logger.go ×2
52 > opts.WorkflowPanicPolicy = worker.FailWorkflow
53 >
54 > w := worker.New(ts.GetDefaultClient(), taskQueue, opts)
55 > registerFunc(w)
56 > ts.workers = append(ts.workers, w)
57 >
58 > if err := w.Start(); err != nil {
59 ts.fatal(err)
60 }
61
62 > return w logger.go ×2
63 }
64
65 // GetDefaultClient returns the default Temporal client configured for making requests to the server.
66 //
67 // It is configured to use a pre-registered test namespace and will be closed on TestServer.Stop.
68 > func (ts *TestServer) GetDefaultClient() client.Client { lite_server.go ×25
69 > if ts.defaultClient == nil {
70 > ts.defaultClient = ts.NewClientWithOptions(ts.defaultClientOptions)
71 > }
72 > return ts.defaultClient
73 }
74
75 // GetDefaultNamespace returns the randomly generated namespace which has been pre-registered with the test server.
76 > func (ts *TestServer) GetDefaultNamespace() string { visibility_store.go ×17
77 > return ts.defaultTestNamespace
78 > }
79
80 // GetFrontendHostPort returns the host:port for this server.
81 //
82 // When constructing a Temporal client from within the same process,
83 // GetDefaultClient or NewClientWithOptions should be used instead.
84 func (ts *TestServer) GetFrontendHostPort() string {
85 return ts.server.FrontendHostPort()
86 }
87
88 // NewClientWithOptions returns a new Temporal client configured for making requests to the server.
89 //
90 // If no namespace option is set it will use a pre-registered test namespace.
91 // The returned client will be closed on TestServer.Stop.
92 > func (ts *TestServer) NewClientWithOptions(opts client.Options) client.Client { lite_server.go ×25
93 > if opts.Namespace == "" {
94 > opts.Namespace = ts.defaultTestNamespace
95 > }
96 > if opts.Logger == nil {
97 > opts.Logger = &testLogger{ts.t}
98 > }
99
100 > ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) lite_server.go ×25
101 > defer cancel()
102 >
103 > c, err := ts.server.NewClientWithOptions(ctx, opts)
104 > if err != nil {
105 ts.fatal(fmt.Errorf("error creating client: %w", err))
106 }
107
108 > ts.clients = append(ts.clients, c) lite_server.go ×25
109 >
110 > return c
111 }
112
113 // Stop closes test clients and shuts down the server.
114 > func (ts *TestServer) Stop() { server.go ×3
115 > for _, w := range ts.workers {
116 > w.Stop() logger.go ×1
117 > }
118 > for _, c := range ts.clients { server.go ×3
119 > c.Close()
120 > }
121 > if err := ts.server.Stop(); err != nil {
122 // Log instead of throwing error because there's no need to fail the test
123 // if it already succeeded.
124 ts.t.Logf("error shutting down Temporal server: %s", err)
125 }
126 }
127
128 // NewServer starts and returns a new TestServer.
129 //
130 // If not specifying the WithT option, the caller should execute Stop when finished to close
131 // the server and release resources.
132 > func NewServer(opts ...TestServerOption) *TestServer { lite_server.go ×25
133 > testNamespace := fmt.Sprintf("temporaltest-%d", rand.Intn(1e6))
134 >
135 > ts := TestServer{
136 > defaultTestNamespace: testNamespace,
137 > }
138 >
139 > // Apply options
140 > for _, opt := range opts {
141 > opt.apply(&ts)
142 > }
143
144 > if ts.t != nil { lite_server.go ×25
145 > ts.t.Cleanup(ts.Stop) server.go ×3
146 > }
147
148 > s, err := temporalite.NewLiteServer(&temporalite.LiteServerConfig{ lite_server.go ×25
149 > Namespaces: []string{ts.defaultTestNamespace},
150 > Ephemeral: true,
151 > Logger: log.NewNoopLogger(),
152 > DynamicConfig: dynamicconfig.StaticClient{
153 > dynamicconfig.ForceSearchAttributesCacheRefreshOnRead.Key(): []dynamicconfig.ConstrainedValue{{Value: true}},
154 > },
155 > // Disable "accept incoming network connections?" prompt on macOS
156 > FrontendIP: "127.0.0.1",
157 > }, ts.serverOptions...)
158 > if err != nil {
159 ts.fatal(fmt.Errorf("error creating server: %w", err))
160 }
161 > ts.server = s lite_server.go ×25
162 >
163 > // Start does not block as long as InterruptOn is unset.
164 > if err := s.Start(); err != nil {
165 ts.fatal(err)
166 }
167
168 // This sleep helps avoid a panic in github.com/temporalio/[email protected]/swim/labels.go:175
169 > time.Sleep(100 * time.Millisecond) lite_server.go ×25
170 >
171 > return &ts
172 }