From 9199723da2d0802da7b607f54ba5cd39d8ec7b3e Mon Sep 17 00:00:00 2001 From: Andreas Bergmeier Date: Wed, 8 Nov 2023 11:21:03 +0100 Subject: [PATCH] Add WithErrorReportCallback --- CHANGELOG.md | 3 +++ pkg/gcp/test/errorreporting.go | 8 +++----- pkg/gcp/test/test.go | 23 +++++++++++++++-------- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9eaacc8..fc8159a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/pkg/gcp/test/errorreporting.go b/pkg/gcp/test/errorreporting.go index f6b2afd..7a0e754 100644 --- a/pkg/gcp/test/errorreporting.go +++ b/pkg/gcp/test/errorreporting.go @@ -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 } diff --git a/pkg/gcp/test/test.go b/pkg/gcp/test/test.go index c63eaea..baf0e5d 100644 --- a/pkg/gcp/test/test.go +++ b/pkg/gcp/test/test.go @@ -28,13 +28,24 @@ 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, } } @@ -42,9 +53,9 @@ func MustMakeTestServices(ctx context.Context, project, serviceName string, opts var fes *fakeErrorreportingServer for _, opt := range opts { - if opt.c != nil { + if opt.f != nil { fes = &fakeErrorreportingServer{ - c: opt.c, + f: opt.f, } } } @@ -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