-
Notifications
You must be signed in to change notification settings - Fork 748
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Test for data race conditions in adapters #1756
Merged
Merged
Changes from 14 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
7074f58
Data race test first draft
cbcc293
Step out real quick
a4c6c4a
Rollback CopyWithOption
dde3442
Implemented bid request deep copy function instead of importing library
8dc32b2
merge recent modifications to adapters/adapterstest/test_json.go
e81eceb
resolved conflicts
015fc30
informative comment
e22ffd7
Merge branch 'master' into sharedMemoryTest
4ce81b9
resolved openrtb2 merge conflicts
eb75ee4
Rolled back to use the github.com/jinzhu/copier library
87d8461
step out real quick
4e81fcf
Scott's review and deep copy library switch
77d9a54
updated with latest master and resolved merge conflicts
e8d3a6a
Added slice assertions
6be78a1
Major corrections. Removed the assertNoDataRace functions entirely
f96f9e7
Merge branch 'master' into sharedMemoryTest
1023f30
go mod tidy-ed
5f81f1d
Scott's latest review. Improving error messages and reducing large co…
4138379
Merge branch 'master' into sharedMemoryTest
d0e5e70
Modified imp shallow copies comment
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,8 +7,10 @@ import ( | |
"regexp" | ||
"testing" | ||
|
||
"github.com/mohae/deepcopy" | ||
"github.com/mxmCherry/openrtb/v15/openrtb2" | ||
"github.com/prebid/prebid-server/adapters" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/yudai/gojsondiff" | ||
"github.com/yudai/gojsondiff/formatter" | ||
|
||
|
@@ -107,24 +109,10 @@ func runSpec(t *testing.T, filename string, spec *testSpec, bidder adapters.Bidd | |
} else if isVideoTest { | ||
reqInfo.PbsEntryPoint = "video" | ||
} | ||
actualReqs, errs := bidder.MakeRequests(&spec.BidRequest, &reqInfo) | ||
diffErrorLists(t, fmt.Sprintf("%s: MakeRequests", filename), errs, spec.MakeRequestErrors) | ||
diffHttpRequestLists(t, filename, actualReqs, spec.HttpCalls) | ||
|
||
bidResponses := make([]*adapters.BidderResponse, 0) | ||
|
||
var bidsErrs = make([]error, 0, len(spec.MakeBidsErrors)) | ||
for i := 0; i < len(actualReqs); i++ { | ||
thisBidResponse, theseErrs := bidder.MakeBids(&spec.BidRequest, spec.HttpCalls[i].Request.ToRequestData(t), spec.HttpCalls[i].Response.ToResponseData(t)) | ||
bidsErrs = append(bidsErrs, theseErrs...) | ||
bidResponses = append(bidResponses, thisBidResponse) | ||
} | ||
requests := testMakeRequestsImpl(t, filename, spec, bidder, &reqInfo) | ||
|
||
diffErrorLists(t, fmt.Sprintf("%s: MakeBids", filename), bidsErrs, spec.MakeBidsErrors) | ||
|
||
for i := 0; i < len(spec.BidResponses); i++ { | ||
diffBidLists(t, filename, bidResponses[i], spec.BidResponses[i].Bids) | ||
} | ||
testMakeBidsImpl(t, filename, spec, bidder, requests) | ||
} | ||
|
||
type testSpec struct { | ||
|
@@ -194,8 +182,8 @@ type expectedBid struct { | |
// | ||
// Marshalling the structs and then using a JSON-diff library isn't great either, since | ||
|
||
// diffHttpRequests compares the actual http requests to the expected ones. | ||
func diffHttpRequestLists(t *testing.T, filename string, actual []*adapters.RequestData, expected []httpCall) { | ||
// assertMakeRequestsOutput compares the actual http requests to the expected ones. | ||
func assertMakeRequestsOutput(t *testing.T, filename string, actual []*adapters.RequestData, expected []httpCall) { | ||
t.Helper() | ||
|
||
if len(expected) != len(actual) { | ||
|
@@ -206,7 +194,7 @@ func diffHttpRequestLists(t *testing.T, filename string, actual []*adapters.Requ | |
} | ||
} | ||
|
||
func diffErrorLists(t *testing.T, description string, actual []error, expected []testSpecExpectedError) { | ||
func assertErrorList(t *testing.T, description string, actual []error, expected []testSpecExpectedError) { | ||
t.Helper() | ||
|
||
if len(expected) != len(actual) { | ||
|
@@ -227,10 +215,10 @@ func diffErrorLists(t *testing.T, description string, actual []error, expected [ | |
} | ||
} | ||
|
||
func diffBidLists(t *testing.T, filename string, response *adapters.BidderResponse, expected []expectedBid) { | ||
func assertMakeBidsOutput(t *testing.T, filename string, bidderResponse *adapters.BidderResponse, expected []expectedBid) { | ||
t.Helper() | ||
|
||
if (response == nil || len(response.Bids) == 0) != (len(expected) == 0) { | ||
if (bidderResponse == nil || len(bidderResponse.Bids) == 0) != (len(expected) == 0) { | ||
if len(expected) == 0 { | ||
t.Fatalf("%s: expectedBidResponses indicated a nil response, but mockResponses supplied a non-nil response", filename) | ||
} | ||
|
@@ -239,17 +227,15 @@ func diffBidLists(t *testing.T, filename string, response *adapters.BidderRespon | |
} | ||
|
||
// Expected nil response - give diffBids something to work with. | ||
if response == nil { | ||
response = new(adapters.BidderResponse) | ||
if bidderResponse == nil { | ||
bidderResponse = new(adapters.BidderResponse) | ||
} | ||
|
||
actual := response.Bids | ||
|
||
if len(actual) != len(expected) { | ||
t.Fatalf("%s: MakeBids returned wrong bid count. Expected %d, got %d", filename, len(expected), len(actual)) | ||
if len(bidderResponse.Bids) != len(expected) { | ||
t.Fatalf("%s: MakeBids returned wrong bid count. Expected %d, got %d", filename, len(expected), len(bidderResponse.Bids)) | ||
} | ||
for i := 0; i < len(actual); i++ { | ||
diffBids(t, fmt.Sprintf("%s: typedBid[%d]", filename, i), actual[i], &(expected[i])) | ||
for i := 0; i < len(bidderResponse.Bids); i++ { | ||
diffBids(t, fmt.Sprintf("%s: typedBid[%d]", filename, i), bidderResponse.Bids[i], &(expected[i])) | ||
} | ||
} | ||
|
||
|
@@ -331,3 +317,120 @@ func diffJson(t *testing.T, description string, actual []byte, expected []byte) | |
} | ||
} | ||
} | ||
|
||
// testMakeRequestsImpl asserts the results of the bidder MakeRequests implementation against the | ||
// expected JSON-defined results and makes sure the adapter's implementations of MakeRequests do | ||
// not incurr in data races | ||
func testMakeRequestsImpl(t *testing.T, filename string, spec *testSpec, bidder adapters.Bidder, reqInfo *adapters.ExtraRequestInfo) []*adapters.RequestData { | ||
t.Helper() | ||
|
||
deepBidReqCopy, shallowBidReqCopy := getDataRaceTestCopies(&spec.BidRequest) | ||
|
||
// Run MakeRequests | ||
requests, errs := bidder.MakeRequests(&spec.BidRequest, reqInfo) | ||
|
||
// Compare MakeRequests actual output versus expected values found in JSON file | ||
assertErrorList(t, fmt.Sprintf("%s: MakeRequests", filename), errs, spec.MakeRequestErrors) | ||
assertMakeRequestsOutput(t, filename, requests, spec.HttpCalls) | ||
|
||
// Assert no data races occur using original bidRequest copies of references and values | ||
assertNoDataRace(t, deepBidReqCopy, shallowBidReqCopy, filename) | ||
|
||
return requests | ||
} | ||
|
||
// getDataRaceTestCopies returns a deep copy and a shallow copy of the original bidRequest that will get | ||
// compared inside assertNoDataRace() to verify no data races occur. | ||
// - The shallow copy is helpful because it provides reference values to shared memory that we don't want | ||
// adapters to modify. | ||
// - The deep copy will help us preserve all the original values, even those of the shared memory fields that | ||
// will remain untouched by the adapter tests so we can compare the real shared memory (that can | ||
// be accessed via the shallow copy) to its original values | ||
func getDataRaceTestCopies(original *openrtb2.BidRequest) (*openrtb2.BidRequest, *openrtb2.BidRequest) { | ||
deepReqCopy := deepcopy.Copy(original).(*openrtb2.BidRequest) | ||
|
||
shallowReqCopy := *original | ||
shallowReqCopy.Imp = make([]openrtb2.Imp, 0, len(original.Imp)) | ||
for i := 0; i < len(original.Imp); i++ { | ||
shallowImpCopy := original.Imp[i] | ||
shallowReqCopy.Imp = append(shallowReqCopy.Imp, shallowImpCopy) | ||
} | ||
|
||
return deepReqCopy, &shallowReqCopy | ||
} | ||
|
||
// assertNoDataRace compares the contents of the reference fields found in the original openrtb2.BidRequest such as Site, | ||
// App, Device and so on, to their original shared-memory values to make sure they were not modified and we are not incurring | ||
// in data races. Because some adapters modify the lenght of the []Imp array, we call assertNoImpsDataRace() to assert we are | ||
// data-race free there. This function is necessary because a simple `assert.Equalf()` call would also compare non shared | ||
// memory fields that adapters are free to modify, therefore leading us to false positives in terms of data-race detection. | ||
func assertNoDataRace(t *testing.T, bidRequestBefore *openrtb2.BidRequest, bidRequestAfter *openrtb2.BidRequest, filename string) { | ||
t.Helper() | ||
|
||
// Assert reference fields were not modified by bidder adapter MakeRequests implementation | ||
assert.Equal(t, bidRequestBefore.Site, bidRequestAfter.Site, "Data race in BidRequest.Site field in file %s", filename) | ||
assert.Equal(t, bidRequestBefore.App, bidRequestAfter.App, "Data race in BidRequest.App field in file %s", filename) | ||
assert.Equal(t, bidRequestBefore.Device, bidRequestAfter.Device, "Data race in BidRequest.Device field in file %s", filename) | ||
assert.Equal(t, bidRequestBefore.User, bidRequestAfter.User, "Data race in BidRequest.User field in file %s", filename) | ||
assert.Equal(t, bidRequestBefore.Source, bidRequestAfter.Source, "Data race in BidRequest.Source field in file %s", filename) | ||
assert.Equal(t, bidRequestBefore.Regs, bidRequestAfter.Regs, "Data race in BidRequest.Regs field in file %s", filename) | ||
|
||
// Assert slice fields were not modified by bidder adapter MakeRequests implementation | ||
assert.Equal(t, bidRequestBefore.WSeat, bidRequestAfter.WSeat, "Data race in BidRequest.[]WSeat array") | ||
assert.Equal(t, bidRequestBefore.BSeat, bidRequestAfter.BSeat, "Data race in BidRequest.[]BSeat array") | ||
assert.Equal(t, bidRequestBefore.Cur, bidRequestAfter.Cur, "Data race in BidRequest.[]Cur array") | ||
assert.Equal(t, bidRequestBefore.WLang, bidRequestAfter.WLang, "Data race in BidRequest.[]WLang array") | ||
assert.Equal(t, bidRequestBefore.BCat, bidRequestAfter.BCat, "Data race in BidRequest.[]BCat array") | ||
assert.Equal(t, bidRequestBefore.BAdv, bidRequestAfter.BAdv, "Data race in BidRequest.[]BAdv array") | ||
assert.Equal(t, bidRequestBefore.BApp, bidRequestAfter.BApp, "Data race in BidRequest.[]BApp array") | ||
assert.Equal(t, bidRequestBefore.Ext, bidRequestAfter.Ext, "Data race in BidRequest.[]Ext array") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a string, not a slice. It's ok for the bidder to modify it since strings in Go are immutable. |
||
|
||
// Assert Imps separately | ||
assertNoImpsDataRace(t, bidRequestBefore.Imp, bidRequestAfter.Imp, filename) | ||
} | ||
|
||
// assertNoImpsDataRace compares the contents of the reference fields found in the original openrtb2.Imp objects to | ||
// their original values to make sure they were not modified and we are not incurring in data races. | ||
func assertNoImpsDataRace(t *testing.T, impsBefore []openrtb2.Imp, impsAfter []openrtb2.Imp, filename string) { | ||
t.Helper() | ||
|
||
if assert.Len(t, impsAfter, len(impsBefore), "Original []Imp array was modified and length is not equal to original after MakeRequests was called. File:%s", filename) { | ||
// Assert no data races occured in individual Imp elements | ||
for i := 0; i < len(impsBefore); i++ { | ||
assert.Equal(t, impsBefore[i].Banner, impsAfter[i].Banner, "Data race in bidRequest.Imp[%d].Banner field. File:%s", i, filename) | ||
assert.Equal(t, impsBefore[i].Video, impsAfter[i].Video, "Data race in bidRequest.Imp[%d].Video field. File:%s", i, filename) | ||
assert.Equal(t, impsBefore[i].Audio, impsAfter[i].Audio, "Data race in bidRequest.Imp[%d].Audio field. File:%s", i, filename) | ||
assert.Equal(t, impsBefore[i].Native, impsAfter[i].Native, "Data race in bidRequest.Imp[%d].Native field. File:%s", i, filename) | ||
assert.Equal(t, impsBefore[i].PMP, impsAfter[i].PMP, "Data race in bidRequest.Imp[%d].PMP field. File:%s", i, filename) | ||
assert.Equal(t, impsBefore[i].Secure, impsAfter[i].Secure, "Data race in bidRequest.Imp[%d].Secure field. File:%s", i, filename) | ||
assert.Equal(t, impsBefore[i].Metric, impsAfter[i].Metric, "Data race in bidRequest.Imp[%d].[]Metric array. File:%s", i) | ||
assert.Equal(t, impsBefore[i].IframeBuster, impsAfter[i].IframeBuster, "Data race in bidRequest.Imp[%d].[]IframeBuster array", i) | ||
} | ||
} | ||
} | ||
|
||
// testMakeBidsImpl asserts the results of the bidder MakeBids implementation against the expected JSON-defined results | ||
func testMakeBidsImpl(t *testing.T, filename string, spec *testSpec, bidder adapters.Bidder, makeRequestsOut []*adapters.RequestData) { | ||
t.Helper() | ||
|
||
bidResponses := make([]*adapters.BidderResponse, 0) | ||
var bidsErrs = make([]error, 0, len(spec.MakeBidsErrors)) | ||
|
||
// We should have as many bids as number of adapters.RequestData found in MakeRequests output | ||
for i := 0; i < len(makeRequestsOut); i++ { | ||
// Run MakeBids with JSON refined spec.HttpCalls info that was asserted to match MakeRequests | ||
// output inside testMakeRequestsImpl | ||
thisBidResponse, theseErrs := bidder.MakeBids(&spec.BidRequest, spec.HttpCalls[i].Request.ToRequestData(t), spec.HttpCalls[i].Response.ToResponseData(t)) | ||
|
||
bidsErrs = append(bidsErrs, theseErrs...) | ||
bidResponses = append(bidResponses, thisBidResponse) | ||
} | ||
|
||
// Assert actual errors thrown by MakeBids implementation versus expected JSON-defined spec.MakeBidsErrors | ||
assertErrorList(t, fmt.Sprintf("%s: MakeBids", filename), bidsErrs, spec.MakeBidsErrors) | ||
|
||
// Assert MakeBids implementation BidResponses with expected JSON-defined spec.BidResponses[i].Bids | ||
for i := 0; i < len(spec.BidResponses); i++ { | ||
assertMakeBidsOutput(t, filename, bidResponses[i], spec.BidResponses[i].Bids) | ||
} | ||
} | ||
SyntaxNode marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add in this comment that the
Imp[]
slice is purposefully excluded.