Skip to content

Commit 598e145

Browse files
authored
Extract clients from onebox.go (#10575)
## What changed `onebox.go` now creates its frontend/admin/operator, history/scheduler, and matching test clients directly from the configured test hosts. ## Why? This removes the `fx.Populate` references. One step closer to the goal of deleting the separate `onebox.go` fx graph all-together and making use of the production fx graph from the `temporal` package instead.
1 parent 5b4ab81 commit 598e145

10 files changed

Lines changed: 198 additions & 166 deletions

File tree

common/testing/await/require_ctx_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,7 @@ func newRecordingTB() *recordingTB {
478478

479479
func (r *recordingTB) Helper() {}
480480
func (r *recordingTB) Failed() bool { return r.failed.Load() }
481+
func (r *recordingTB) Name() string { return "recordingTB" }
481482
func (r *recordingTB) Logf(format string, args ...any) {
482483
r.mu.Lock()
483484
defer r.mu.Unlock()

common/testing/grpcinject/fx.go

Lines changed: 0 additions & 7 deletions
This file was deleted.

common/testing/grpcinject/grpcinject.go

Lines changed: 0 additions & 62 deletions
This file was deleted.

common/testing/testcontext/context.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@ import (
88
"time"
99

1010
"go.temporal.io/server/common/debug"
11+
"google.golang.org/grpc/metadata"
1112
)
1213

13-
const defaultTimeout = 90 * time.Second
14+
const (
15+
defaultTimeout = 90 * time.Second
16+
testNameMetadataKey = "temporal-test-name"
17+
)
1418

1519
type contextStore struct {
1620
sync.Mutex
@@ -97,6 +101,10 @@ func getContextState(tb testing.TB, timeout time.Duration) *contextState {
97101
}
98102

99103
ctx, cancel := context.WithTimeout(tb.Context(), timeout)
104+
105+
// Annotate gRPC requests with the test name for OTEL tracing.
106+
ctx = metadata.AppendToOutgoingContext(ctx, testNameMetadataKey, tb.Name())
107+
100108
st := &contextState{
101109
ctx: ctx,
102110
cancel: cancel,

common/testing/testcontext/context_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"time"
88

99
"github.com/stretchr/testify/require"
10+
"google.golang.org/grpc/metadata"
1011
)
1112

1213
func TestWithTimeout(t *testing.T) {
@@ -18,6 +19,15 @@ func TestWithTimeout(t *testing.T) {
1819
require.WithinDuration(t, time.Now().Add(time.Second), deadline, 50*time.Millisecond)
1920
}
2021

22+
func TestNameMetadata(t *testing.T) {
23+
t.Parallel()
24+
25+
ctx := New(t)
26+
md, ok := metadata.FromOutgoingContext(ctx)
27+
require.True(t, ok)
28+
require.Equal(t, []string{t.Name()}, md.Get(testNameMetadataKey))
29+
}
30+
2131
func TestContextDecorators(t *testing.T) {
2232
t.Parallel()
2333

tests/add_tasks_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"time"
99

1010
"github.com/google/uuid"
11+
"go.temporal.io/api/serviceerror"
1112
sdkclient "go.temporal.io/sdk/client"
1213
"go.temporal.io/sdk/worker"
1314
"go.temporal.io/sdk/workflow"
@@ -130,5 +131,6 @@ func (s *AddTasksSuite) TestAddTasks_ErrGetShardByID() {
130131
ShardId: 0,
131132
})
132133
s.Error(err)
133-
s.Contains(strings.ToLower(err.Error()), "invalid shardid")
134+
s.ErrorAs(err, new(*serviceerror.InvalidArgument))
135+
s.Contains(strings.ToLower(err.Error()), "shard id cannot be equal or lower than zero")
134136
}

tests/testcore/clients.go

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
package testcore
2+
3+
import (
4+
"crypto/tls"
5+
"fmt"
6+
"sync"
7+
8+
"go.temporal.io/api/operatorservice/v1"
9+
"go.temporal.io/api/workflowservice/v1"
10+
"go.temporal.io/server/api/adminservice/v1"
11+
"go.temporal.io/server/api/historyservice/v1"
12+
"go.temporal.io/server/api/matchingservice/v1"
13+
schedulerpb "go.temporal.io/server/chasm/lib/scheduler/gen/schedulerpb/v1"
14+
"go.temporal.io/server/common/log"
15+
"go.temporal.io/server/common/log/tag"
16+
"go.temporal.io/server/common/membership/static"
17+
"go.temporal.io/server/common/metrics"
18+
"go.temporal.io/server/common/primitives"
19+
"go.temporal.io/server/common/rpc"
20+
"go.temporal.io/server/common/rpc/encryption"
21+
"google.golang.org/grpc"
22+
)
23+
24+
type clients struct {
25+
logger log.Logger
26+
hostsByService map[primitives.ServiceName]static.Hosts
27+
tlsConfigProvider *encryption.FixedTLSConfigProvider
28+
29+
frontend frontendClients
30+
history historyClients
31+
matching matchingClient
32+
}
33+
34+
type frontendClients struct {
35+
once sync.Once
36+
conn *grpc.ClientConn
37+
admin adminservice.AdminServiceClient
38+
frontend workflowservice.WorkflowServiceClient
39+
operator operatorservice.OperatorServiceClient
40+
}
41+
42+
type historyClients struct {
43+
once sync.Once
44+
conn *grpc.ClientConn
45+
history historyservice.HistoryServiceClient
46+
scheduler schedulerpb.SchedulerServiceClient
47+
}
48+
49+
type matchingClient struct {
50+
client matchingservice.MatchingServiceClient
51+
}
52+
53+
func newClients(
54+
logger log.Logger,
55+
hostsByService map[primitives.ServiceName]static.Hosts,
56+
tlsConfigProvider *encryption.FixedTLSConfigProvider,
57+
) clients {
58+
return clients{
59+
logger: logger,
60+
hostsByService: hostsByService,
61+
tlsConfigProvider: tlsConfigProvider,
62+
}
63+
}
64+
65+
func (c *clients) AdminClient() adminservice.AdminServiceClient {
66+
c.ensureFrontend()
67+
return c.frontend.admin
68+
}
69+
70+
func (c *clients) OperatorClient() operatorservice.OperatorServiceClient {
71+
c.ensureFrontend()
72+
return c.frontend.operator
73+
}
74+
75+
func (c *clients) FrontendClient() workflowservice.WorkflowServiceClient {
76+
c.ensureFrontend()
77+
return c.frontend.frontend
78+
}
79+
80+
func (c *clients) ensureFrontend() {
81+
c.frontend.once.Do(func() {
82+
conn, err := c.newConn(primitives.FrontendService)
83+
if err != nil {
84+
c.logger.Fatal("unable to create frontend test client", tag.Error(err))
85+
}
86+
c.frontend.conn = conn
87+
c.frontend.admin = adminservice.NewAdminServiceClient(conn)
88+
c.frontend.frontend = workflowservice.NewWorkflowServiceClient(conn)
89+
c.frontend.operator = operatorservice.NewOperatorServiceClient(conn)
90+
})
91+
}
92+
93+
func (c *clients) HistoryClient() historyservice.HistoryServiceClient {
94+
c.ensureHistory()
95+
return c.history.history
96+
}
97+
98+
func (c *clients) SchedulerClient() schedulerpb.SchedulerServiceClient {
99+
c.ensureHistory()
100+
return c.history.scheduler
101+
}
102+
103+
func (c *clients) ensureHistory() {
104+
c.history.once.Do(func() {
105+
conn, err := c.newConn(primitives.HistoryService)
106+
if err != nil {
107+
c.logger.Fatal("unable to create history test client", tag.Error(err))
108+
}
109+
c.history.conn = conn
110+
c.history.history = historyservice.NewHistoryServiceClient(conn)
111+
c.history.scheduler = schedulerpb.NewSchedulerServiceClient(conn)
112+
})
113+
}
114+
115+
func (c *clients) MatchingClient() matchingservice.MatchingServiceClient {
116+
return c.matching.client
117+
}
118+
119+
func (c *clients) close() []error {
120+
var errs []error
121+
for _, conn := range []*grpc.ClientConn{
122+
c.frontend.conn,
123+
c.history.conn,
124+
} {
125+
if conn != nil {
126+
errs = append(errs, conn.Close())
127+
}
128+
}
129+
c.frontend.conn = nil
130+
c.history.conn = nil
131+
return errs
132+
}
133+
134+
func (c *clients) newConn(serviceName primitives.ServiceName) (*grpc.ClientConn, error) {
135+
address, err := c.grpcAddress(serviceName)
136+
if err != nil {
137+
return nil, err
138+
}
139+
tlsConfig, err := c.tlsConfig(serviceName)
140+
if err != nil {
141+
return nil, err
142+
}
143+
144+
return rpc.Dial(address, tlsConfig, c.logger, metrics.NoopMetricsHandler)
145+
}
146+
147+
func (c *clients) grpcAddress(serviceName primitives.ServiceName) (string, error) {
148+
hosts := c.hostsByService[serviceName].All
149+
if len(hosts) == 0 {
150+
return "", fmt.Errorf("no %s gRPC hosts configured", serviceName)
151+
}
152+
return hosts[0], nil
153+
}
154+
155+
func (c *clients) tlsConfig(serviceName primitives.ServiceName) (*tls.Config, error) {
156+
if c.tlsConfigProvider == nil {
157+
return nil, nil
158+
}
159+
if serviceName == primitives.FrontendService {
160+
return c.tlsConfigProvider.GetFrontendClientConfig()
161+
}
162+
return c.tlsConfigProvider.GetInternodeClientConfig()
163+
}

tests/testcore/functional_test_base.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ import (
4848
"go.temporal.io/server/common/testing/updateutils"
4949
"go.temporal.io/server/components/nexusoperations"
5050
"go.uber.org/fx"
51-
"google.golang.org/grpc"
52-
"google.golang.org/grpc/metadata"
5351
)
5452

5553
type (
@@ -378,11 +376,6 @@ func (s *FunctionalTestBase) SetupTest() {
378376
s.initAssertions()
379377
s.setupSdk()
380378
s.taskPoller = taskpoller.New(s.T(), s.FrontendClient(), s.Namespace().String())
381-
382-
// Annotate gRPC requests with the test name for OTEL tracing.
383-
s.testCluster.host.grpcClientInterceptor.Set(func(ctx context.Context) context.Context {
384-
return metadata.AppendToOutgoingContext(ctx, "temporal-test-name", s.T().Name())
385-
})
386379
}
387380

388381
func (s *FunctionalTestBase) SetupSubTest() {
@@ -435,13 +428,6 @@ func (s *FunctionalTestBase) setupSdk() {
435428
clientOptions.ConnectionOptions.TLS = provider.FrontendClientConfig
436429
}
437430

438-
if interceptor := s.testCluster.host.grpcClientInterceptor; interceptor != nil {
439-
clientOptions.ConnectionOptions.DialOptions = []grpc.DialOption{
440-
grpc.WithUnaryInterceptor(interceptor.Unary()),
441-
grpc.WithStreamInterceptor(interceptor.Stream()),
442-
}
443-
}
444-
445431
var err error
446432
s.sdkClient, err = sdkclient.Dial(clientOptions)
447433
s.NoError(err)
@@ -499,7 +485,6 @@ func (s *FunctionalTestBase) tearDownTestCluster() error {
499485
func (s *FunctionalTestBase) TearDownTest() {
500486
s.exportOTELTraces()
501487
s.tearDownSdk()
502-
s.testCluster.host.grpcClientInterceptor.Set(nil)
503488
}
504489

505490
// **IMPORTANT**: When overridding this, make sure to invoke `s.FunctionalTestBase.TearDownSubTest()`.

0 commit comments

Comments
 (0)