Files
Roey BermanandGitHub 600eb2fc41 Upgrade to Nexus SDK 0.2.0 (#7241)
## What changed?

- Rename operation ID to token in the code and protos
- Upgrade the Nexus Go SDK to latest main (to be tagged 0.2.0)
- Upgrade the api dependency to get rename changes from ID to token
- Fixed some lint warnings around usage of `nexus.HandlerErrorf`
2025-02-05 19:07:13 +00:00

79 lines
2.9 KiB
Go

// The MIT License
//
// Copyright (c) 2024 Temporal Technologies Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package nexustest
import (
"context"
"fmt"
"net"
"net/http"
"testing"
"time"
"github.com/nexus-rpc/sdk-go/nexus"
"github.com/stretchr/testify/require"
"go.temporal.io/server/internal/freeport"
)
func AllocListenAddress() string {
return fmt.Sprintf("localhost:%d", freeport.MustGetFreePort())
}
func NewNexusServer(t *testing.T, listenAddr string, handler nexus.Handler) {
hh := nexus.NewHTTPHandler(nexus.HandlerOptions{
Handler: handler,
})
srv := &http.Server{Addr: listenAddr, Handler: hh}
listener, err := net.Listen("tcp", listenAddr)
require.NoError(t, err)
errCh := make(chan error, 1)
go func() {
errCh <- srv.Serve(listener)
}()
t.Cleanup(func() {
// Graceful shutdown
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
err = srv.Shutdown(ctx)
if ctx.Err() != nil {
require.NoError(t, err)
require.ErrorIs(t, <-errCh, http.ErrServerClosed)
}
})
}
type Handler struct {
nexus.UnimplementedHandler
OnStartOperation func(ctx context.Context, service, operation string, input *nexus.LazyValue, options nexus.StartOperationOptions) (nexus.HandlerStartOperationResult[any], error)
OnCancelOperation func(ctx context.Context, service, operation, token string, options nexus.CancelOperationOptions) error
}
func (h Handler) StartOperation(ctx context.Context, service, operation string, input *nexus.LazyValue, options nexus.StartOperationOptions) (nexus.HandlerStartOperationResult[any], error) {
return h.OnStartOperation(ctx, service, operation, input, options)
}
func (h Handler) CancelOperation(ctx context.Context, service, operation, token string, options nexus.CancelOperationOptions) error {
return h.OnCancelOperation(ctx, service, operation, token, options)
}