Skip to content

Commit

Permalink
Add WithErrorReportCallback
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasBergmeier6176 committed Nov 8, 2023
1 parent 5fcc311 commit 9199723
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 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.38 Further options for ErrorReporting
- Add WithErrorReportCallback for testing Services.

## v0.0.36 Ease for ErrorReporting
- Add WithErrorReportChannel for testing Services.

Expand Down
8 changes: 3 additions & 5 deletions pkg/gcp/test/errorreporting.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@ var (
)

type fakeErrorreportingServer struct {
c chan<- *errorreportingpb.ReportedErrorEvent
ReportedEvents []*errorreportingpb.ReportedErrorEvent
f ReportErrorFunc
}

func (s *fakeErrorreportingServer) ReportErrorEvent(ctx context.Context, req *errorreportingpb.ReportErrorEventRequest) (*errorreportingpb.ReportErrorEventResponse, error) {
if s.c != nil {
s.c <- req.Event
if s.f != nil {
return s.f(ctx, req)
}
s.ReportedEvents = append(s.ReportedEvents, req.Event)
return &errorreportingpb.ReportErrorEventResponse{}, nil
}
23 changes: 15 additions & 8 deletions pkg/gcp/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,34 @@ type server struct {
fes *fakeErrorreportingServer
}

type ReportErrorFunc func(ctx context.Context, req *errorreportingpb.ReportErrorEventRequest) (*errorreportingpb.ReportErrorEventResponse, error)

type testServicesOption struct {
c chan<- *errorreportingpb.ReportedErrorEvent
f ReportErrorFunc
}

func WithErrorReportChannel(evs chan<- *errorreportingpb.ReportedErrorEvent) testServicesOption {
return testServicesOption{
c: evs,
f: func(ctx context.Context, req *errorreportingpb.ReportErrorEventRequest) (*errorreportingpb.ReportErrorEventResponse, error) {
evs <- req.Event
return &errorreportingpb.ReportErrorEventResponse{}, nil
},
}
}

func WithErrorReportCallback(f ReportErrorFunc) testServicesOption {
return testServicesOption{
f: f,
}
}

func MustMakeTestServices(ctx context.Context, project, serviceName string, opts ...testServicesOption) *server {

var fes *fakeErrorreportingServer
for _, opt := range opts {
if opt.c != nil {
if opt.f != nil {
fes = &fakeErrorreportingServer{
c: opt.c,
f: opt.f,
}
}
}
Expand Down Expand Up @@ -127,10 +138,6 @@ func MustMakeTestServices(ctx context.Context, project, serviceName string, opts
}
}

func (s *server) ReportedErrorEvents() []*errorreportingpb.ReportedErrorEvent {
return s.fes.ReportedEvents
}

func (s *server) Close() error {
// Ignore close errors because usually
// we are not that particular about testing
Expand Down

0 comments on commit 9199723

Please sign in to comment.