Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mansinahar committed Dec 4, 2020
1 parent 85ffc00 commit f8bbf0f
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 81 deletions.
31 changes: 17 additions & 14 deletions exchange/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,10 @@ func (e *exchange) makeAuctionContext(ctx context.Context, needsCache bool) (auc
}

// This piece sends all the requests to the bidder adapters and gathers the results.
func (e *exchange) getAllBids(ctx context.Context, bidderRequests []BidderRequest, bidAdjustments map[string]float64, blabels map[openrtb_ext.BidderName]*pbsmetrics.AdapterLabels, conversions currencies.Conversions) (map[openrtb_ext.BidderName]*pbsOrtbSeatBid, map[openrtb_ext.BidderName]*seatResponseExtra, bool) {
func (e *exchange) getAllBids(ctx context.Context, bidderRequests []BidderRequest,
bidAdjustments map[string]float64, blabels map[openrtb_ext.BidderName]*pbsmetrics.AdapterLabels,
conversions currencies.Conversions) (map[openrtb_ext.BidderName]*pbsOrtbSeatBid,
map[openrtb_ext.BidderName]*seatResponseExtra, bool) {
// Set up pointers to the bid results
adapterBids := make(map[openrtb_ext.BidderName]*pbsOrtbSeatBid, len(bidderRequests))
adapterExtra := make(map[openrtb_ext.BidderName]*seatResponseExtra, len(bidderRequests))
Expand All @@ -343,25 +346,25 @@ func (e *exchange) getAllBids(ctx context.Context, bidderRequests []BidderReques
// Here we actually call the adapters and collect the bids.
bidderRunner := e.recoverSafely(bidderRequests, func(bidderRequest BidderRequest, conversions currencies.Conversions) {
// Passing in aName so a doesn't change out from under the go routine
if bidder.BidderLabels.Adapter == "" {
glog.Errorf("Exchange: bidlables for %s (%s) missing adapter string", bidder.BidderName, bidder.BidderCoreName)
bidder.BidderLabels.Adapter = bidder.BidderCoreName
if bidderRequest.BidderLabels.Adapter == "" {
glog.Errorf("Exchange: bidlables for %s (%s) missing adapter string", bidderRequest.BidderName, bidderRequest.BidderCoreName)
bidderRequest.BidderLabels.Adapter = bidderRequest.BidderCoreName
}
brw := new(bidResponseWrapper)
brw.bidder = bidder.BidderName
brw.bidder = bidderRequest.BidderName
// Defer basic metrics to insure we capture them after all the values have been set
defer func() {
e.me.RecordAdapterRequest(bidder.BidderLabels)
e.me.RecordAdapterRequest(bidderRequest.BidderLabels)
}()
start := time.Now()

adjustmentFactor := 1.0
if givenAdjustment, ok := bidAdjustments[string(bidder.BidderName)]; ok {
if givenAdjustment, ok := bidAdjustments[string(bidderRequest.BidderName)]; ok {
adjustmentFactor = givenAdjustment
}
var reqInfo adapters.ExtraRequestInfo
reqInfo.PbsEntryPoint = bidder.BidderLabels.RType
bids, err := e.adapterMap[bidder.BidderCoreName].requestBid(ctx, bidder.BidRequest, bidder.BidderName, adjustmentFactor, conversions, &reqInfo)
reqInfo.PbsEntryPoint = bidderRequest.BidderLabels.RType
bids, err := e.adapterMap[bidderRequest.BidderCoreName].requestBid(ctx, bidderRequest.BidRequest, bidderRequest.BidderName, adjustmentFactor, conversions, &reqInfo)

// Add in time reporting
elapsed := time.Since(start)
Expand All @@ -374,18 +377,18 @@ func (e *exchange) getAllBids(ctx context.Context, bidderRequests []BidderReques
}

// Timing statistics
e.me.RecordAdapterTime(bidder.BidderLabels, time.Since(start))
e.me.RecordAdapterTime(bidderRequest.BidderLabels, time.Since(start))
serr := errsToBidderErrors(err)
bidder.BidderLabels.AdapterBids = bidsToMetric(brw.adapterBids)
bidder.BidderLabels.AdapterErrors = errorsToMetric(err)
bidderRequest.BidderLabels.AdapterBids = bidsToMetric(brw.adapterBids)
bidderRequest.BidderLabels.AdapterErrors = errorsToMetric(err)
// Append any bid validation errors to the error list
ae.Errors = serr
brw.adapterExtra = ae
if bids != nil {
for _, bid := range bids.bids {
var cpm = float64(bid.bid.Price * 1000)
e.me.RecordAdapterPrice(bidder.BidderLabels, cpm)
e.me.RecordAdapterBidReceived(bidder.BidderLabels, bid.bidType, bid.bid.AdM != "")
e.me.RecordAdapterPrice(bidderRequest.BidderLabels, cpm)
e.me.RecordAdapterBidReceived(bidderRequest.BidderLabels, bid.bidType, bid.bid.AdM != "")
}
}
chBids <- brw
Expand Down
34 changes: 23 additions & 11 deletions exchange/exchange_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1152,18 +1152,10 @@ func TestPanicRecovery(t *testing.T) {
e := NewExchange(adapters, nil, cfg, &metricsConf.DummyMetricsEngine{}, gdpr.AlwaysAllow{}, currencyConverter, nilCategoryFetcher{}).(*exchange)

chBids := make(chan *bidResponseWrapper, 1)
panicker := func(aName openrtb_ext.BidderName, coreBidder openrtb_ext.BidderName, request *openrtb.BidRequest, bidlabels *pbsmetrics.AdapterLabels, conversions currencies.Conversions) {
panicker := func(bidderRequest BidderRequest, conversions currencies.Conversions) {
panic("panic!")
}
cleanReqs := map[openrtb_ext.BidderName]*openrtb.BidRequest{
"bidder1": {
ID: "b-1",
},
"bidder2": {
ID: "b-2",
},
}
recovered := e.recoverSafely(cleanReqs, panicker, chBids)

apnLabels := pbsmetrics.AdapterLabels{
Source: pbsmetrics.DemandWeb,
RType: pbsmetrics.ReqTypeORTB2Web,
Expand All @@ -1172,7 +1164,27 @@ func TestPanicRecovery(t *testing.T) {
CookieFlag: pbsmetrics.CookieFlagYes,
AdapterBids: pbsmetrics.AdapterBidNone,
}
recovered(openrtb_ext.BidderAppnexus, openrtb_ext.BidderAppnexus, nil, &apnLabels, nil)

bidderRequests := []BidderRequest{
{
BidderName: "bidder1",
BidderCoreName: "appnexus",
BidderLabels: apnLabels,
BidRequest: &openrtb.BidRequest{
ID: "b-1",
},
},
{
BidderName: "bidder2",
BidderCoreName: "bidder2",
BidRequest: &openrtb.BidRequest{
ID: "b-2",
},
},
}

recovered := e.recoverSafely(bidderRequests, panicker, chBids)
recovered(bidderRequests[0], nil)
}

func buildImpExt(t *testing.T, jsonFilename string) json.RawMessage {
Expand Down
148 changes: 92 additions & 56 deletions exchange/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,38 +38,48 @@ func (p *permissionsMock) AMPException() bool {
return false
}

func assertReq(t *testing.T, reqByBidders map[openrtb_ext.BidderName]*openrtb.BidRequest,
func assertReq(t *testing.T, bidderRequests []BidderRequest,
applyCOPPA bool, consentedVendors map[string]bool) {
// assert individual bidder requests
assert.NotEqual(t, reqByBidders, 0, "cleanOpenRTBRequest should split request into individual bidder requests")
assert.NotEqual(t, bidderRequests, 0, "cleanOpenRTBRequest should split request into individual bidder requests")

// assert for PI data
// Both appnexus and brightroll should be allowed since brightroll
// is used as an alias for appnexus in the test request
for bidderName, bidder := range reqByBidders {
if !applyCOPPA && consentedVendors[bidderName.String()] {
assert.NotEqual(t, bidder.User.BuyerUID, "", "cleanOpenRTBRequest shouldn't clean PI data as per COPPA or for a consented vendor as per GDPR or per CCPA")
assert.NotEqual(t, bidder.Device.DIDMD5, "", "cleanOpenRTBRequest shouldn't clean PI data as per COPPA or for a consented vendor as per GDPR or per CCPA")
for _, req := range bidderRequests {
if !applyCOPPA && consentedVendors[req.BidderName.String()] {
assert.NotEqual(t, req.BidRequest.User.BuyerUID, "", "cleanOpenRTBRequest shouldn't clean PI data as per COPPA or for a consented vendor as per GDPR or per CCPA")
assert.NotEqual(t, req.BidRequest.Device.DIDMD5, "", "cleanOpenRTBRequest shouldn't clean PI data as per COPPA or for a consented vendor as per GDPR or per CCPA")
} else {
assert.Equal(t, bidder.User.BuyerUID, "", "cleanOpenRTBRequest should clean PI data as per COPPA or for a non-consented vendor as per GDPR or per CCPA", bidderName.String())
assert.Equal(t, bidder.Device.DIDMD5, "", "cleanOpenRTBRequest should clean PI data as per COPPA or for a non-consented vendor as per GDPR or per CCPA", bidderName.String())
assert.Equal(t, req.BidRequest.User.BuyerUID, "", "cleanOpenRTBRequest should clean PI data as per COPPA or for a non-consented vendor as per GDPR or per CCPA", req.BidderName.String())
assert.Equal(t, req.BidRequest.Device.DIDMD5, "", "cleanOpenRTBRequest should clean PI data as per COPPA or for a non-consented vendor as per GDPR or per CCPA", req.BidderName.String())
}
}
}

func TestCleanOpenRTBRequests(t *testing.T) {
testCases := []struct {
req *openrtb.BidRequest
bidReqAssertions func(t *testing.T, reqByBidders map[openrtb_ext.BidderName]*openrtb.BidRequest,
req AuctionRequest
bidReqAssertions func(t *testing.T, bidderRequests []BidderRequest,
applyCOPPA bool, consentedVendors map[string]bool)
hasError bool
applyCOPPA bool
consentedVendors map[string]bool
}{
{req: newRaceCheckingRequest(t), bidReqAssertions: assertReq, hasError: false,
applyCOPPA: true, consentedVendors: map[string]bool{"appnexus": true}},
{req: newAdapterAliasBidRequest(t), bidReqAssertions: assertReq, hasError: false,
applyCOPPA: false, consentedVendors: map[string]bool{"appnexus": true, "brightroll": true}},
{
req: AuctionRequest{BidRequest: newRaceCheckingRequest(t), UserSyncs: &emptyUsersync{}},
bidReqAssertions: assertReq,
hasError: false,
applyCOPPA: true,
consentedVendors: map[string]bool{"appnexus": true},
},
{
req: AuctionRequest{BidRequest: newAdapterAliasBidRequest(t), UserSyncs: &emptyUsersync{}},
bidReqAssertions: assertReq,
hasError: false,
applyCOPPA: false,
consentedVendors: map[string]bool{"appnexus": true, "brightroll": true},
},
}

privacyConfig := config.Privacy{
Expand All @@ -82,12 +92,12 @@ func TestCleanOpenRTBRequests(t *testing.T) {
}

for _, test := range testCases {
reqByBidders, _, _, err := cleanOpenRTBRequests(context.Background(), test.req, nil, &emptyUsersync{}, map[openrtb_ext.BidderName]*pbsmetrics.AdapterLabels{}, pbsmetrics.Labels{}, &permissionsMock{personalInfoAllowed: true}, true, privacyConfig, &config.Account{})
bidderRequests, _, err := cleanOpenRTBRequests(context.Background(), test.req, nil, map[openrtb_ext.BidderName]*pbsmetrics.AdapterLabels{}, &permissionsMock{personalInfoAllowed: true}, true, privacyConfig)
if test.hasError {
assert.NotNil(t, err, "Error shouldn't be nil")
} else {
assert.Nil(t, err, "Err should be nil")
test.bidReqAssertions(t, reqByBidders, test.applyCOPPA, test.consentedVendors)
test.bidReqAssertions(t, bidderRequests, test.applyCOPPA, test.consentedVendors)
}
}
}
Expand Down Expand Up @@ -227,26 +237,29 @@ func TestCleanOpenRTBRequestsCCPA(t *testing.T) {
},
}

results, _, privacyLabels, errs := cleanOpenRTBRequests(
auctionReq := AuctionRequest{
BidRequest: req,
UserSyncs: &emptyUsersync{},
Account: accountConfig,
}

bidderRequests, privacyLabels, errs := cleanOpenRTBRequests(
context.Background(),
req,
auctionReq,
nil,
&emptyUsersync{},
map[openrtb_ext.BidderName]*pbsmetrics.AdapterLabels{},
pbsmetrics.Labels{},
&permissionsMock{personalInfoAllowed: true},
true,
privacyConfig,
&accountConfig)
result := results["appnexus"]
privacyConfig)
result := bidderRequests[0]

assert.Nil(t, errs)
if test.expectDataScrub {
assert.Equal(t, result.User.BuyerUID, "", test.description+":User.BuyerUID")
assert.Equal(t, result.Device.DIDMD5, "", test.description+":Device.DIDMD5")
assert.Equal(t, result.BidRequest.User.BuyerUID, "", test.description+":User.BuyerUID")
assert.Equal(t, result.BidRequest.Device.DIDMD5, "", test.description+":Device.DIDMD5")
} else {
assert.NotEqual(t, result.User.BuyerUID, "", test.description+":User.BuyerUID")
assert.NotEqual(t, result.Device.DIDMD5, "", test.description+":Device.DIDMD5")
assert.NotEqual(t, result.BidRequest.User.BuyerUID, "", test.description+":User.BuyerUID")
assert.NotEqual(t, result.BidRequest.Device.DIDMD5, "", test.description+":Device.DIDMD5")
}
assert.Equal(t, test.expectPrivacyLabels, privacyLabels, test.description+":PrivacyLabels")
}
Expand Down Expand Up @@ -282,12 +295,17 @@ func TestCleanOpenRTBRequestsCCPAErrors(t *testing.T) {
err := json.Unmarshal(req.Ext, &reqExtStruct)
assert.NoError(t, err, test.description+":marshal_ext")

auctionReq := AuctionRequest{
BidRequest: req,
UserSyncs: &emptyUsersync{},
}

privacyConfig := config.Privacy{
CCPA: config.CCPA{
Enforce: true,
},
}
_, _, _, errs := cleanOpenRTBRequests(context.Background(), req, &reqExtStruct, &emptyUsersync{}, map[openrtb_ext.BidderName]*pbsmetrics.AdapterLabels{}, pbsmetrics.Labels{}, &permissionsMock{personalInfoAllowed: true}, true, privacyConfig, &config.Account{})
_, _, errs := cleanOpenRTBRequests(context.Background(), auctionReq, &reqExtStruct, map[openrtb_ext.BidderName]*pbsmetrics.AdapterLabels{}, &permissionsMock{personalInfoAllowed: true}, true, privacyConfig)

assert.ElementsMatch(t, []error{test.expectError}, errs, test.description)
}
Expand Down Expand Up @@ -322,16 +340,21 @@ func TestCleanOpenRTBRequestsCOPPA(t *testing.T) {
req := newBidRequest(t)
req.Regs = &openrtb.Regs{COPPA: test.coppa}

results, _, privacyLabels, errs := cleanOpenRTBRequests(context.Background(), req, nil, &emptyUsersync{}, map[openrtb_ext.BidderName]*pbsmetrics.AdapterLabels{}, pbsmetrics.Labels{}, &permissionsMock{personalInfoAllowed: true}, true, config.Privacy{}, &config.Account{})
result := results["appnexus"]
auctionReq := AuctionRequest{
BidRequest: req,
UserSyncs: &emptyUsersync{},
}

bidderRequests, privacyLabels, errs := cleanOpenRTBRequests(context.Background(), auctionReq, nil, map[openrtb_ext.BidderName]*pbsmetrics.AdapterLabels{}, &permissionsMock{personalInfoAllowed: true}, true, config.Privacy{})
result := bidderRequests[0]

assert.Nil(t, errs)
if test.expectDataScrub {
assert.Equal(t, result.User.BuyerUID, "", test.description+":User.BuyerUID")
assert.Equal(t, result.User.Yob, int64(0), test.description+":User.Yob")
assert.Equal(t, result.BidRequest.User.BuyerUID, "", test.description+":User.BuyerUID")
assert.Equal(t, result.BidRequest.User.Yob, int64(0), test.description+":User.Yob")
} else {
assert.NotEqual(t, result.User.BuyerUID, "", test.description+":User.BuyerUID")
assert.NotEqual(t, result.User.Yob, int64(0), test.description+":User.Yob")
assert.NotEqual(t, result.BidRequest.User.BuyerUID, "", test.description+":User.BuyerUID")
assert.NotEqual(t, result.BidRequest.User.Yob, int64(0), test.description+":User.Yob")
}
assert.Equal(t, test.expectPrivacyLabels, privacyLabels, test.description+":PrivacyLabels")
}
Expand Down Expand Up @@ -424,16 +447,20 @@ func TestCleanOpenRTBRequestsSChain(t *testing.T) {
extRequest = unmarshaledExt
}

results, _, _, errs := cleanOpenRTBRequests(context.Background(), req, extRequest, &emptyUsersync{}, map[openrtb_ext.BidderName]*pbsmetrics.AdapterLabels{}, pbsmetrics.Labels{}, &permissionsMock{}, true, config.Privacy{}, &config.Account{})
result := results["appnexus"]
auctionReq := AuctionRequest{
BidRequest: req,
UserSyncs: &emptyUsersync{},
}

bidderRequests, _, errs := cleanOpenRTBRequests(context.Background(), auctionReq, extRequest, map[openrtb_ext.BidderName]*pbsmetrics.AdapterLabels{}, &permissionsMock{}, true, config.Privacy{})
if test.hasError == true {
assert.NotNil(t, errs)
assert.Nil(t, result)
assert.Len(t, bidderRequests, 0)
} else {
result := bidderRequests[0]
assert.Nil(t, errs)
assert.Equal(t, test.outSourceExt, result.Source.Ext, test.description+":Source.Ext")
assert.Equal(t, test.outRequestExt, result.Ext, test.description+":Ext")
assert.Equal(t, test.outSourceExt, result.BidRequest.Source.Ext, test.description+":Source.Ext")
assert.Equal(t, test.outRequestExt, result.BidRequest.Ext, test.description+":Ext")
}
}
}
Expand Down Expand Up @@ -995,22 +1022,27 @@ func TestCleanOpenRTBRequestsLMT(t *testing.T) {
req := newBidRequest(t)
req.Device.Lmt = test.lmt

auctionReq := AuctionRequest{
BidRequest: req,
UserSyncs: &emptyUsersync{},
}

privacyConfig := config.Privacy{
LMT: config.LMT{
Enforce: test.enforceLMT,
},
}

results, _, privacyLabels, errs := cleanOpenRTBRequests(context.Background(), req, nil, &emptyUsersync{}, map[openrtb_ext.BidderName]*pbsmetrics.AdapterLabels{}, pbsmetrics.Labels{}, &permissionsMock{personalInfoAllowed: true}, true, privacyConfig, &config.Account{})
result := results["appnexus"]
results, privacyLabels, errs := cleanOpenRTBRequests(context.Background(), auctionReq, nil, map[openrtb_ext.BidderName]*pbsmetrics.AdapterLabels{}, &permissionsMock{personalInfoAllowed: true}, true, privacyConfig)
result := results[0]

assert.Nil(t, errs)
if test.expectDataScrub {
assert.Equal(t, result.User.BuyerUID, "", test.description+":User.BuyerUID")
assert.Equal(t, result.Device.DIDMD5, "", test.description+":Device.DIDMD5")
assert.Equal(t, result.BidRequest.User.BuyerUID, "", test.description+":User.BuyerUID")
assert.Equal(t, result.BidRequest.Device.DIDMD5, "", test.description+":Device.DIDMD5")
} else {
assert.NotEqual(t, result.User.BuyerUID, "", test.description+":User.BuyerUID")
assert.NotEqual(t, result.Device.DIDMD5, "", test.description+":Device.DIDMD5")
assert.NotEqual(t, result.BidRequest.User.BuyerUID, "", test.description+":User.BuyerUID")
assert.NotEqual(t, result.BidRequest.Device.DIDMD5, "", test.description+":Device.DIDMD5")
}
assert.Equal(t, test.expectPrivacyLabels, privacyLabels, test.description+":PrivacyLabels")
}
Expand Down Expand Up @@ -1148,26 +1180,30 @@ func TestCleanOpenRTBRequestsGDPR(t *testing.T) {
},
}

results, _, privacyLabels, errs := cleanOpenRTBRequests(
auctionReq := AuctionRequest{
BidRequest: req,
UserSyncs: &emptyUsersync{},
Account: accountConfig,
}

results, privacyLabels, errs := cleanOpenRTBRequests(
context.Background(),
req,
auctionReq,
nil,
&emptyUsersync{},

map[openrtb_ext.BidderName]*pbsmetrics.AdapterLabels{},
pbsmetrics.Labels{},
&permissionsMock{personalInfoAllowed: !test.gdprScrub},
true,
privacyConfig,
&accountConfig)
result := results["appnexus"]
privacyConfig)
result := results[0]

assert.Nil(t, errs)
if test.gdprScrub {
assert.Equal(t, result.User.BuyerUID, "", test.description+":User.BuyerUID")
assert.Equal(t, result.Device.DIDMD5, "", test.description+":Device.DIDMD5")
assert.Equal(t, result.BidRequest.User.BuyerUID, "", test.description+":User.BuyerUID")
assert.Equal(t, result.BidRequest.Device.DIDMD5, "", test.description+":Device.DIDMD5")
} else {
assert.NotEqual(t, result.User.BuyerUID, "", test.description+":User.BuyerUID")
assert.NotEqual(t, result.Device.DIDMD5, "", test.description+":Device.DIDMD5")
assert.NotEqual(t, result.BidRequest.User.BuyerUID, "", test.description+":User.BuyerUID")
assert.NotEqual(t, result.BidRequest.Device.DIDMD5, "", test.description+":Device.DIDMD5")
}
assert.Equal(t, test.expectPrivacyLabels, privacyLabels, test.description+":PrivacyLabels")
}
Expand Down

0 comments on commit f8bbf0f

Please sign in to comment.