Skip to content

Commit

Permalink
move func maps to service definition
Browse files Browse the repository at this point in the history
  • Loading branch information
jasondborneman committed Jan 5, 2024
1 parent d689bd6 commit 78ca536
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 80 deletions.
40 changes: 0 additions & 40 deletions example/weather/services/tester/func_map.go

This file was deleted.

11 changes: 4 additions & 7 deletions example/weather/services/tester/run_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,26 @@ func endTest(tr *gentester.TestResult, start time.Time, tc *TestCollection, resu

func getStackTrace(wg *sync.WaitGroup, m *sync.Mutex) string {
m.Lock()
defer wg.Done()
defer m.Unlock()
// keep backup of the real stderr
old := os.Stderr
f, w, _ := os.Pipe()
os.Stderr = w
defer w.Close()

debug.PrintStack()

outC := make(chan string)
go func() {
var buf bytes.Buffer
_, err := io.Copy(&buf, f)
if err != nil {
log.Errorf(context.Background(), err, "error copying buffer when captiring stack trace for test panic")
}
io.Copy(&buf, f)
outC <- buf.String()
}()

w.Close()
// restoring the real stderr
os.Stderr = old
out := <-outC
m.Unlock()
wg.Done()

return out
}
Expand Down
12 changes: 3 additions & 9 deletions example/weather/services/tester/test_methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,33 +51,27 @@ func (svc *Service) TestAll(ctx context.Context, p *gentester.TesterPayload) (re

// Runs the Smoke tests as a collection in parallel
func (svc *Service) TestSmoke(ctx context.Context) (res *gentester.TestResults, err error) {
svc.smokeTestMapInit(ctx)

// Smoke tests
smokeCollection := TestCollection{
Name: "Smoke Tests",
}
return svc.runTests(ctx, filteringPayload, &smokeCollection, smokeTestMap, false)
return svc.runTests(ctx, filteringPayload, &smokeCollection, svc.smokeTestMap, false)
}

// Runs the ACL Service tests as a collection in parallel
func (svc *Service) TestForecaster(ctx context.Context) (res *gentester.TestResults, err error) {
svc.forecasterTestMapInit(ctx)

// ACL tests
aclCollection := TestCollection{
Name: "Forecaster Tests",
}
return svc.runTests(ctx, filteringPayload, &aclCollection, forecasterTestMap, false)
return svc.runTests(ctx, filteringPayload, &aclCollection, svc.forecasterTestMap, false)
}

// Runs the Login Service tests as a collection synchronously
func (svc *Service) TestLocator(ctx context.Context) (res *gentester.TestResults, err error) {
svc.locatorTestMapInit(ctx)

// Login tests
loginCollection := TestCollection{
Name: "Locator Tests",
}
return svc.runTests(ctx, filteringPayload, &loginCollection, locatorTestMap, true)
return svc.runTests(ctx, filteringPayload, &loginCollection, svc.locatorTestMap, true)
}
22 changes: 21 additions & 1 deletion example/weather/services/tester/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,35 @@ type (
Service struct {
lc locator.Client
fc forecaster.Client
// Define test func maps for each collection tested (except TestAll)
smokeTestMap map[string]func(context.Context, *TestCollection)
forecasterTestMap map[string]func(context.Context, *TestCollection)
locatorTestMap map[string]func(context.Context, *TestCollection)
}
)

// New instantiates a new tester service.
func New(lc locator.Client, fc forecaster.Client) *Service {
return &Service{
svc := &Service{
lc: lc,
fc: fc,
}
// initalize the test func maps with test function names as keys to funcs to be run.
svc.smokeTestMap = map[string]func(context.Context, *TestCollection){
"TestForecasterValidLatLong": svc.TestForecasterValidLatLong,
"TestLocatorValidIP": svc.TestLocatorValidIP,
}
svc.forecasterTestMap = map[string]func(context.Context, *TestCollection){
"TestForecasterValidLatLong": svc.TestForecasterValidLatLong,
"TestForecasterInvalidLat": svc.TestForecasterInvalidLat,
"TestForecasterInvalidLong": svc.TestForecasterInvalidLong,
}
svc.locatorTestMap = map[string]func(context.Context, *TestCollection){
"TestLocatorValidIP": svc.TestLocatorValidIP,
"TestLocatorInvalidIP": svc.TestLocatorInvalidIP,
}

return svc
}

func logError(ctx context.Context, err error) error {
Expand Down
16 changes: 2 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,11 @@ github.com/envoyproxy/protoc-gen-validate v1.0.2 h1:QkIBuU5k+x7/QXPvPPnWXWlCdaBF
github.com/envoyproxy/protoc-gen-validate v1.0.2/go.mod h1:GpiZQP3dDbg4JouG/NNS7QWXpgx6x8QiMKdmN72jogE=
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/go-chi/chi/v5 v5.0.10 h1:rLz5avzKpjqxrYwXNfmjkrYYXOyLJd37pz53UFHC6vk=
github.com/go-chi/chi/v5 v5.0.10/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-chi/chi/v5 v5.0.11 h1:BnpYbFZ3T3S1WMpD79r7R5ThWX40TaFB7L31Y8xqSwA=
github.com/go-chi/chi/v5 v5.0.11/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi4=
github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.3.0 h1:2y3SDp0ZXuc6/cjLSZ+Q3ir+QB9T/iG5yYRXqsagWSY=
github.com/go-logr/logr v1.3.0/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
Expand All @@ -42,8 +38,6 @@ github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiu
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4=
github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
Expand Down Expand Up @@ -81,8 +75,6 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.46.0 h1:PzIubN4/sjByhDRHLviCjJuweBXWFZWhghjg7cS28+M=
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.46.0/go.mod h1:Ct6zzQEuGK3WpJs2n4dn+wfJYzd/+hNnxMRTWjGn30M=
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.46.1 h1:SpGay3w+nEwMpfVnbqOLH5gY52/foP8RE8UzTZ1pdSE=
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.46.1/go.mod h1:4UoMYEZOC0yN/sPGH76KPkkU7zgiEWYWL9vwmbnTJPE=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.1 h1:aFJWCqJMNjENlcleuuOkGAPH82y0yULBScfXcIEdS24=
Expand Down Expand Up @@ -126,14 +118,10 @@ golang.org/x/tools v0.16.1/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.6.8 h1:IhEN5q69dyKagZPYMSdIjS2HqprW324FRQZJcGqPAsM=
google.golang.org/appengine v1.6.8/go.mod h1:1jJ3jBArFh5pcgW8gCtRJnepW8FzD1V44FJffLiz/Ds=
google.golang.org/genproto v0.0.0-20231120223509-83a465c0220f h1:Vn+VyHU5guc9KjB5KrjI2q0wCOWEOIh0OEsleqakHJg=
google.golang.org/genproto v0.0.0-20231120223509-83a465c0220f/go.mod h1:nWSwAFPb+qfNJXsoeO3Io7zf4tMSfN8EA8RlDA04GhY=
google.golang.org/genproto/googleapis/api v0.0.0-20231106174013-bbf56f31fb17 h1:JpwMPBpFN3uKhdaekDpiNlImDdkUAyiJ6ez/uxGaUSo=
google.golang.org/genproto/googleapis/api v0.0.0-20231106174013-bbf56f31fb17/go.mod h1:0xJLfVdJqpAPl8tDg1ujOCGzx6LFLttXT5NhllGOXY4=
google.golang.org/genproto v0.0.0-20231211222908-989df2bf70f3 h1:1hfbdAfFbkmpg41000wDVqr7jUpK/Yo+LPnIxxGzmkg=
google.golang.org/genproto v0.0.0-20231211222908-989df2bf70f3/go.mod h1:5RBcpGRxr25RbDzY5w+dmaqpSEvl8Gwl1x2CICf60ic=
google.golang.org/genproto/googleapis/api v0.0.0-20231212172506-995d672761c0 h1:s1w3X6gQxwrLEpxnLd/qXTVLgQE2yXwaOaoa6IlY/+o=
google.golang.org/genproto/googleapis/api v0.0.0-20231212172506-995d672761c0/go.mod h1:CAny0tYF+0/9rmDB9fahA9YLzX3+AEVl1qXbv5hhj6c=
google.golang.org/genproto/googleapis/rpc v0.0.0-20231127180814-3a041ad873d4 h1:DC7wcm+i+P1rN3Ff07vL+OndGg5OhNddHyTA+ocPqYE=
google.golang.org/genproto/googleapis/rpc v0.0.0-20231127180814-3a041ad873d4/go.mod h1:eJVxU6o+4G1PSczBr85xmyvSNYAKvAYgkub40YGomFM=
google.golang.org/genproto/googleapis/rpc v0.0.0-20231212172506-995d672761c0 h1:/jFB8jK5R3Sq3i/lmeZO0cATSzFfZaJq1J2Euan3XKU=
google.golang.org/genproto/googleapis/rpc v0.0.0-20231212172506-995d672761c0/go.mod h1:FUoWkonphQm3RhTS+kOEhF8h0iDpm4tdXolVCeZ9KKA=
google.golang.org/grpc v1.60.1 h1:26+wFr+cNqSGFcOXcabYC0lUVJVRa2Sb2ortSK7VrEU=
Expand Down
Loading

0 comments on commit 78ca536

Please sign in to comment.