Skip to content

Commit

Permalink
Bidder Uniqueness Gatekeeping Test (#1506)
Browse files Browse the repository at this point in the history
  • Loading branch information
SyntaxNode authored Sep 24, 2020
1 parent e496c8b commit 120f735
Showing 1 changed file with 56 additions and 0 deletions.
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 a 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 duplicate bidder name values.")
assert.LessOrEqual(t, measuredThreshold, currentThreshold)
}

// minUniquePrefixLength measures the minimun amount of characters needed to uniquely identify
// one 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
}

0 comments on commit 120f735

Please sign in to comment.