|
| 1 | +package nexus_standalone_activity_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/google/uuid" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + |
| 11 | + "github.com/nexus-rpc/sdk-go/nexus" |
| 12 | + |
| 13 | + nexuspb "go.temporal.io/api/nexus/v1" |
| 14 | + "go.temporal.io/api/operatorservice/v1" |
| 15 | + "go.temporal.io/sdk/client" |
| 16 | + "go.temporal.io/sdk/testsuite" |
| 17 | + "go.temporal.io/sdk/worker" |
| 18 | + |
| 19 | + "github.com/temporalio/samples-go/nexus-standalone-activity/handler" |
| 20 | + "github.com/temporalio/samples-go/nexus-standalone-activity/service" |
| 21 | +) |
| 22 | + |
| 23 | +const ( |
| 24 | + taskQueue = "nexus-standalone-activity-test" |
| 25 | + endpointName = "nexus-standalone-activity-test-endpoint" |
| 26 | +) |
| 27 | + |
| 28 | +func Test_NexusOperationStandaloneActivity_Using_DevServer(t *testing.T) { |
| 29 | + ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second) |
| 30 | + defer cancel() |
| 31 | + |
| 32 | + // Start the dev server with standalone Nexus support and Activity callbacks enabled, which is |
| 33 | + // what lets a Standalone Activity complete a Nexus Operation. |
| 34 | + server, err := testsuite.StartDevServer(ctx, testsuite.DevServerOptions{ |
| 35 | + CachedDownload: testsuite.CachedDownload{ |
| 36 | + Version: "v1.7.4-standalone-nexus-operations", |
| 37 | + }, |
| 38 | + ExtraArgs: []string{ |
| 39 | + "--dynamic-config-value", "activity.enableCallbacks=true", |
| 40 | + }, |
| 41 | + }) |
| 42 | + require.NoError(t, err) |
| 43 | + defer func() { _ = server.Stop() }() |
| 44 | + |
| 45 | + c := server.Client() |
| 46 | + |
| 47 | + // Create a Nexus endpoint targeting our task queue. |
| 48 | + _, err = c.OperatorService().CreateNexusEndpoint(ctx, &operatorservice.CreateNexusEndpointRequest{ |
| 49 | + Spec: &nexuspb.EndpointSpec{ |
| 50 | + Name: endpointName, |
| 51 | + Target: &nexuspb.EndpointTarget{ |
| 52 | + Variant: &nexuspb.EndpointTarget_Worker_{ |
| 53 | + Worker: &nexuspb.EndpointTarget_Worker{ |
| 54 | + Namespace: "default", |
| 55 | + TaskQueue: taskQueue, |
| 56 | + }, |
| 57 | + }, |
| 58 | + }, |
| 59 | + }, |
| 60 | + }) |
| 61 | + require.NoError(t, err) |
| 62 | + |
| 63 | + // Register Nexus operations on the worker. |
| 64 | + w := worker.New(c, taskQueue, worker.Options{}) |
| 65 | + |
| 66 | + svc := nexus.NewService(service.GreetingServiceName) |
| 67 | + require.NoError(t, svc.Register(handler.GreetOperation)) |
| 68 | + w.RegisterNexusService(svc) |
| 69 | + w.RegisterActivity(handler.CreateGreetingActivity) |
| 70 | + require.NoError(t, w.Start()) |
| 71 | + defer w.Stop() |
| 72 | + |
| 73 | + // Create a standalone NexusClient. |
| 74 | + nexusClient, err := c.NewNexusClient(client.NexusClientOptions{ |
| 75 | + Endpoint: endpointName, |
| 76 | + Service: service.GreetingServiceName, |
| 77 | + }) |
| 78 | + require.NoError(t, err) |
| 79 | + |
| 80 | + handle, err := nexusClient.ExecuteOperation(ctx, service.GreetOperationName, service.GreetingInput{Name: "Test"}, client.StartNexusOperationOptions{ |
| 81 | + ID: "greeting-" + uuid.NewString(), |
| 82 | + ScheduleToCloseTimeout: 10 * time.Second, |
| 83 | + }) |
| 84 | + require.NoError(t, err) |
| 85 | + require.NotEmpty(t, handle.GetID()) |
| 86 | + |
| 87 | + var result service.GreetingOutput |
| 88 | + err = handle.Get(ctx, &result) |
| 89 | + require.NoError(t, err) |
| 90 | + require.Equal(t, "Hello, Test!", result.Message) |
| 91 | +} |
0 commit comments