mirror of
https://github.com/hanzoai/tasks.git
synced 2026-10-04 02:04:03 +00:00
- Fix a couple of occasional panics in Nexus functional and unit tests. - Remove default failure message for unsuccessful operations.
82 lines
3.0 KiB
Go
82 lines
3.0 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/temporalite"
|
|
)
|
|
|
|
func AllocListenAddress(t *testing.T) string {
|
|
pp := temporalite.NewPortProvider()
|
|
listenAddr := fmt.Sprintf("localhost:%d", pp.MustGetFreePort())
|
|
require.NoError(t, pp.Close())
|
|
return listenAddr
|
|
}
|
|
|
|
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, operationID 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, operationID string, options nexus.CancelOperationOptions) error {
|
|
return h.OnCancelOperation(ctx, service, operation, operationID, options)
|
|
}
|