Skip to content

Commit

Permalink
mock provider returns errors explaining what needs to be mocked
Browse files Browse the repository at this point in the history
  • Loading branch information
matt2e committed Apr 29, 2024
1 parent 93637d5 commit b338886
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
2 changes: 0 additions & 2 deletions go-runtime/ftl/call_overrider.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package ftl

//TODO: should not be public in ftl package...

import (
"context"
)
Expand Down
14 changes: 11 additions & 3 deletions go-runtime/ftl/ftltest/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ package ftltest

import (
"context"
"fmt"

"github.com/TBD54566975/ftl/go-runtime/ftl"
"github.com/TBD54566975/ftl/internal/rpc"

"github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/ftlv1connect"
)

type mockFunc func(ctx context.Context, req any) (resp any, err error)
Expand All @@ -26,9 +30,13 @@ func newMockProvider() *mockProvider {

func (m *mockProvider) OverrideCall(ctx context.Context, ref ftl.Ref, req any) (override bool, resp any, err error) {
mock, ok := m.mocks[ref]
if !ok {
if ok {
resp, err = mock(ctx, req)
return true, resp, err
}
if rpc.IsClientAvailableInContext[ftlv1connect.VerbServiceClient](ctx) {
return false, nil, nil
}
resp, err = mock(ctx, req)
return true, resp, err
// Return a clean error for testing because we know the client is not available to make real calls
return false, nil, fmt.Errorf("no mock found")
}
4 changes: 2 additions & 2 deletions go-runtime/ftl/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ func (v *Ref) UnmarshalText(text []byte) error {
return nil
}

func (v *Ref) String() string { return v.Module + "." + v.Name }
func (v *Ref) ToProto() *schemapb.Ref {
func (v Ref) String() string { return v.Module + "." + v.Name }
func (v Ref) ToProto() *schemapb.Ref {
return &schemapb.Ref{Module: v.Module, Name: v.Name}
}

Expand Down
4 changes: 4 additions & 0 deletions internal/rpc/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ func ClientFromContext[Client Pingable](ctx context.Context) Client {
return value.(Client) //nolint:forcetypeassert
}

func IsClientAvailableInContext[Client Pingable](ctx context.Context) bool {
return ctx.Value(clientKey[Client]{}) != nil
}

func propagateHeaders(ctx context.Context, isClient bool, header http.Header) (context.Context, error) {
if isClient {
if IsDirectRouted(ctx) {
Expand Down

0 comments on commit b338886

Please sign in to comment.