Skip to content
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

Bidder Uniqueness Gatekeeping Test #1506

Merged
merged 4 commits into from
Sep 24, 2020
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions openrtb_ext/bidders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,59 @@ func TestBidderListDoesNotDefineContext(t *testing.T) {
bidders := BidderList()
assert.NotContains(t, bidders, BidderNameContext)
}

// TestBidderUniquenessGatekeeping acts as a gatekeeper of bidder name uniqueness. If this test fails
// when you're building a new adapter, please consider choosing a different bidder name to maintain the
// current uniqueness threshold, or else start a discussion in the PR.
func TestBidderUniquenessGatekeeping(t *testing.T) {
// Get List Of Bidders
// - Exclude duplicates of adapters for the same bidder, as it's unlikely the publisher will use both.
var bidders []string
for _, bidder := range BidderMap {
if bidder != BidderTripleliftNative && bidder != BidderAdkernelAdn && bidder != BidderSmartadserver {
bidders = append(bidders, string(bidder))
}
}

currentThreshold := 6
measuredThreshold := minUniquePrefixLength(bidders)

assert.NotZero(t, measuredThreshold, "BidderMap contains duoplicate bidder name values.")
assert.LessOrEqual(t, measuredThreshold, currentThreshold)
}

// minUniquePrefixLength measures the minimun amount of characters needed to ensure uniqueness
// of the strings, or returns 0 if there are duplicates.
func minUniquePrefixLength(b []string) int {
targetingKeyMaxLength := 20
for prefixLength := 1; prefixLength <= targetingKeyMaxLength; prefixLength++ {
if uniqueForPrefixLength(b, prefixLength) {
return prefixLength
}
}
return 0
}

func uniqueForPrefixLength(b []string, prefixLength int) bool {
m := make(map[string]struct{})

if prefixLength <= 0 {
return false
}

for i, n := range b {
ns := string(n)

if len(ns) > prefixLength {
ns = ns[0:prefixLength]
}

m[ns] = struct{}{}

if len(m) != i+1 {
return false
}
}

return true
}