Skip to content

Commit

Permalink
Expose FakeSetup with created bindings
Browse files Browse the repository at this point in the history
This allows FakeSetup to be passed into functions.
  • Loading branch information
AndreasBergmeier6176 committed Jan 18, 2024
1 parent 68dfbe5 commit 19b46a2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## v0.0.51 Allow passing of Test Clients
- Introduces `FakeSetup`, which allows for passing TestClients along.

## v0.0.47 Fix accessing OrderingKey
**Bugfixes**
- Corrects implementation of function `ApplyCloudEventsPubSubOrderingKey`
Expand Down
4 changes: 4 additions & 0 deletions pkg/gcp/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"cloud.google.com/go/errorreporting"
"cloud.google.com/go/logging"
"cloud.google.com/go/pubsub"
texporter "github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/trace"
"github.com/otto-de/sherlock-microservice/pkg/gke"
"go.opentelemetry.io/otel/sdk/resource"
Expand All @@ -16,9 +17,11 @@ import (

// Services contains all Google Cloud Services that
// we use
// TODO: Spit non-Services to another struct
type Services struct {
Logging *logging.Client
ErrorReporting *errorreporting.Client
PubSub *pubsub.Client
TracerProvider *sdktrace.TracerProvider
MonitoredResource *monitoredres.MonitoredResource
}
Expand Down Expand Up @@ -146,6 +149,7 @@ func DiscoverServices(project, serviceName string, tracerProviderOptions []sdktr
// Does **not** handle errors in close since there usually
// is not much that can be done on Close failure anyway.
func (s *Services) Close() {
s.PubSub.Close()
s.TracerProvider.ForceFlush(context.Background()) // flushes any pending spans
s.ErrorReporting.Close()
s.Logging.Close()
Expand Down
31 changes: 20 additions & 11 deletions pkg/gcp/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@ import (
"google.golang.org/grpc/credentials/insecure"
)

type fakeServers struct {
PubSub *pstest.Server
}
type FakeSetup struct {
Servers fakeServers
Services gcp.Services
}

type server struct {
gcp.Services
PubSub *pubsub.Client
PubSubServer *pstest.Server
FakeSetup

listener net.Listener
grpcServer *grpc.Server
Expand Down Expand Up @@ -124,13 +130,17 @@ func MustMakeTestServices(ctx context.Context, project, serviceName string, opts
}()

return &server{
Services: gcp.Services{
ErrorReporting: errRep,
Logging: lc,
TracerProvider: tp,
FakeSetup: FakeSetup{
Servers: fakeServers{
PubSub: psServer,
},
Services: gcp.Services{
TracerProvider: tp,
ErrorReporting: errRep,
Logging: lc,
PubSub: pubSub,
},
},
PubSub: pubSub,
PubSubServer: psServer,
fes: fes,
grpcServer: grpcServer,
listener: l,
Expand All @@ -141,10 +151,9 @@ func MustMakeTestServices(ctx context.Context, project, serviceName string, opts
func (s *server) Close() error {
// Ignore close errors because usually
// we are not that particular about testing
s.PubSub.Close()
s.Services.Close()
s.grpcServer.GracefulStop()
s.PubSubServer.Close()
s.Servers.PubSub.Close()
s.psServerConn.Close()
s.listener.Close()
return nil
Expand Down

0 comments on commit 19b46a2

Please sign in to comment.