Files
tasks/chasm/interceptor_test.go
zeekay 5a601d081b rebrand: go.temporal.io/server -> github.com/hanzoai/tasks
Full module path rebrand from Temporal to Hanzo Tasks:
- go.mod module: github.com/hanzoai/tasks
- All internal Go imports updated (1906 files)
- cmd/server renamed to cmd/tasksd
- Binary name: tasksd (was temporal-server)
- Docker images: hanzoai/tasks (was temporalio/server)
- README rewritten with Hanzo Tasks branding
- LLM.md created with integration notes
- Makefile, goreleaser, CI workflows updated
- go.temporal.io/sdk and go.temporal.io/api imports preserved (separate repos)

Fork of Temporal.io (MIT license).
2026-03-19 10:36:40 -07:00

114 lines
2.8 KiB
Go

package chasm_test
import (
"context"
"net"
"testing"
"github.com/stretchr/testify/require"
"github.com/hanzoai/tasks/chasm"
"github.com/hanzoai/tasks/chasm/lib/tests/gen/testspb/v1"
"github.com/hanzoai/tasks/common/log"
"github.com/hanzoai/tasks/common/metrics"
"go.uber.org/mock/gomock"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
type ServiceHandler struct {
testspb.UnimplementedTestServiceServer
}
func (h ServiceHandler) Test(
ctx context.Context,
req *testspb.TestRequest,
) (resp *testspb.TestResponse, err error) {
hasEngineCtx := chasm.EngineFromContext(ctx) != nil
return &testspb.TestResponse{
RequestId: req.RequestId,
HasEngineCtx: hasEngineCtx,
}, nil
}
type ServiceLibrary struct {
chasm.UnimplementedLibrary
}
func NewServiceLibrary() *ServiceLibrary {
return &ServiceLibrary{}
}
func (l *ServiceLibrary) RegisterServices(server *grpc.Server) {
testspb.RegisterTestServiceServer(server, ServiceHandler{})
}
func TestChasmEngineInterceptor_ShouldRespond(t *testing.T) {
ctrl := gomock.NewController(t)
mockEngine := chasm.NewMockEngine(ctrl)
engineInterceptor := chasm.ChasmEngineInterceptorProvider(
mockEngine,
log.NewNoopLogger(),
metrics.NoopMetricsHandler,
)
server, address := startTestServer(t, grpc.UnaryInterceptor(engineInterceptor.Intercept))
defer server.Stop()
response := testRoundTrip(t, address)
require.True(t, response.HasEngineCtx)
}
func TestChasmVisibilityInterceptor_ShouldRespond(t *testing.T) {
ctrl := gomock.NewController(t)
mockVisibilityManager := chasm.NewMockVisibilityManager(ctrl)
visibilityInterceptor := chasm.ChasmVisibilityInterceptorProvider(mockVisibilityManager)
server, address := startTestServer(t, grpc.UnaryInterceptor(visibilityInterceptor.Intercept))
defer server.Stop()
testRoundTrip(t, address)
}
func testRoundTrip(t *testing.T, address string) *testspb.TestResponse {
conn, err := grpc.NewClient(address, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
t.Fatalf("failed to connect: %v", err)
}
defer func() {
err := conn.Close()
require.NoError(t, err)
}()
client := testspb.NewTestServiceClient(conn)
var response *testspb.TestResponse
response, err = client.Test(context.Background(), &testspb.TestRequest{
RequestId: "test-request-id",
})
require.NoError(t, err)
require.Equal(t, "test-request-id", response.GetRequestId())
return response
}
func startTestServer(t *testing.T, opt ...grpc.ServerOption) (*grpc.Server, string) {
server := grpc.NewServer(opt...)
listener, err := net.Listen("tcp", "localhost:0") // :0 picks a random available port
if err != nil {
panic(err)
}
lib := NewServiceLibrary()
lib.RegisterServices(server)
go func() {
err := server.Serve(listener)
require.NoError(t, err)
}()
return server, listener.Addr().String()
}