From 78fb290c00714d6f45cc9eb8391e2557884eb936 Mon Sep 17 00:00:00 2001 From: Scott Kay Date: Fri, 18 Sep 2020 14:57:52 -0400 Subject: [PATCH 1/4] Bidder Uniqueness Gatekeeping Test --- openrtb_ext/bidders_test.go | 48 +++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/openrtb_ext/bidders_test.go b/openrtb_ext/bidders_test.go index 9f05f526905..31c1863eda0 100644 --- a/openrtb_ext/bidders_test.go +++ b/openrtb_ext/bidders_test.go @@ -66,3 +66,51 @@ 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 since it's likely the publisher will choose + // one of them and not use both. + var bidders []string + for _, bidder := range BidderMap { + if bidder != BidderTripleliftNative && bidder != BidderAdkernelAdn { + bidders = append(bidders, string(bidder)) + } + } + + // Measure Minimum Characters Needed For Uniqueness + prefixLength := 20 + 1 + for uniqueForPrefixLength(bidders, prefixLength-1) { + prefixLength-- + } + + currentUniquenessThreshold := 7 + assert.Equal(t, currentUniquenessThreshold, prefixLength+1) +} + +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 +} From d4288142fd7e6d65d7a63311998dc5713977ac59 Mon Sep 17 00:00:00 2001 From: Scott Kay Date: Fri, 18 Sep 2020 15:10:10 -0400 Subject: [PATCH 2/4] Algorithm Fix + Refactor --- openrtb_ext/bidders_test.go | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/openrtb_ext/bidders_test.go b/openrtb_ext/bidders_test.go index 31c1863eda0..588b8486a45 100644 --- a/openrtb_ext/bidders_test.go +++ b/openrtb_ext/bidders_test.go @@ -81,14 +81,18 @@ func TestBidderUniquenessGatekeeping(t *testing.T) { } } - // Measure Minimum Characters Needed For Uniqueness - prefixLength := 20 + 1 - for uniqueForPrefixLength(bidders, prefixLength-1) { - prefixLength-- - } + currentUniquenessThreshold := 6 + assert.Equal(t, currentUniquenessThreshold, maxUniqueForPrefixLength(bidders)) +} - currentUniquenessThreshold := 7 - assert.Equal(t, currentUniquenessThreshold, prefixLength+1) +func maxUniqueForPrefixLength(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 { From b03570140148aeb5f243640b9a8071e19db50f9a Mon Sep 17 00:00:00 2001 From: Scott Kay Date: Wed, 23 Sep 2020 23:47:34 -0400 Subject: [PATCH 3/4] PR Feedback --- openrtb_ext/bidders_test.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/openrtb_ext/bidders_test.go b/openrtb_ext/bidders_test.go index 588b8486a45..dd6f7f010cf 100644 --- a/openrtb_ext/bidders_test.go +++ b/openrtb_ext/bidders_test.go @@ -72,20 +72,24 @@ func TestBidderListDoesNotDefineContext(t *testing.T) { // 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 since it's likely the publisher will choose - // one of them and not use both. + // - 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 { + if bidder != BidderTripleliftNative && bidder != BidderAdkernelAdn && bidder != BidderSmartadserver { bidders = append(bidders, string(bidder)) } } - currentUniquenessThreshold := 6 - assert.Equal(t, currentUniquenessThreshold, maxUniqueForPrefixLength(bidders)) + currentThreshold := 6 + measuredThreshold := minUniquePrefixLength(bidders) + + assert.NotZero(t, measuredThreshold, "BidderMap contains duoplicate bidder name values.") + assert.LessOrEqual(t, measuredThreshold, currentThreshold) } -func maxUniqueForPrefixLength(b []string) int { +// 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) { From 2f3d5b383b0ce1fcc44d06dd72509f4fe260dbbf Mon Sep 17 00:00:00 2001 From: Scott Kay Date: Thu, 24 Sep 2020 13:04:30 -0400 Subject: [PATCH 4/4] Updated Comments --- openrtb_ext/bidders_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/openrtb_ext/bidders_test.go b/openrtb_ext/bidders_test.go index dd6f7f010cf..7b6a03b4de1 100644 --- a/openrtb_ext/bidders_test.go +++ b/openrtb_ext/bidders_test.go @@ -72,7 +72,7 @@ func TestBidderListDoesNotDefineContext(t *testing.T) { // 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. + // - 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 { @@ -83,12 +83,12 @@ func TestBidderUniquenessGatekeeping(t *testing.T) { currentThreshold := 6 measuredThreshold := minUniquePrefixLength(bidders) - assert.NotZero(t, measuredThreshold, "BidderMap contains duoplicate bidder name values.") + assert.NotZero(t, measuredThreshold, "BidderMap contains duplicate 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. +// 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++ {