@ -435,12 +435,12 @@ func TestPrivatePeers(t *testing.T) {
func testAddrBookAddressSelection ( t * testing . T , bookSize int ) {
func testAddrBookAddressSelection ( t * testing . T , bookSize int ) {
// generate all combinations of old (m) and new addresses
// generate all combinations of old (m) and new addresses
for nOld := 0 ; nOld <= bookSize ; nOld ++ {
nNew := bookSize - nOld
dbgStr := fmt . Sprintf ( "book of size %d (new %d, old %d)" , bookSize , nNew , nOld )
for nBook Old := 0 ; nBook Old <= bookSize ; nBook Old ++ {
nBook New := bookSize - nBook Old
dbgStr := fmt . Sprintf ( "book of size %d (new %d, old %d)" , bookSize , nBook New , nBook Old )
// create book and get selection
// create book and get selection
book , fname := createAddrBookWithMOldAndNNewAddrs ( t , nOld , nNew )
book , fname := createAddrBookWithMOldAndNNewAddrs ( t , nBook Old , nBook New )
defer deleteTempFile ( fname )
defer deleteTempFile ( fname )
addrs := book . GetSelectionWithBias ( biasToSelectNewPeers )
addrs := book . GetSelectionWithBias ( biasToSelectNewPeers )
assert . NotNil ( t , addrs , "%s - expected a non-nil selection" , dbgStr )
assert . NotNil ( t , addrs , "%s - expected a non-nil selection" , dbgStr )
@ -460,27 +460,25 @@ func testAddrBookAddressSelection(t *testing.T, bookSize int) {
// Given:
// Given:
// n - num new addrs, m - num old addrs
// n - num new addrs, m - num old addrs
// k - num new addrs expected in the beginning (based on bias %)
// k - num new addrs expected in the beginning (based on bias %)
// i=min(n, k), aka expFirst New
// i=min(n, max( k,r-m) ), aka expNew
// j=min(m, r-i), aka expOld
// j=min(m, r-i), aka expOld
//
//
// We expect this layout:
// We expect this layout:
// indices: 0...i-1 i...i+j-1 i+j...r
// addresses: N0..Ni-1 O0..Oj-1 Ni...
// indices: 0...i-1 i...i+j-1
// addresses: N0..Ni-1 O0..Oj-1
//
//
// There is at least one partition and at most three.
// There is at least one partition and at most three.
var (
var (
k = percentageOfNum ( biasToSelectNewPeers , nAddrs )
expFirstNew = cmn . MinInt ( nNew , k )
expOld = cmn . MinInt ( nOld , nAddrs - expFirstNew )
expNew = nAddrs - expOld
expLastNew = expNew - expFirstNew
k = percentageOfNum ( biasToSelectNewPeers , nAddrs )
expNew = cmn . MinInt ( nNew , cmn . MaxInt ( k , nAddrs - nBookOld ) )
expOld = cmn . MinInt ( nOld , nAddrs - expNew )
)
)
// Verify that the number of old and new addresses are as expected
// Verify that the number of old and new addresses are as expected
if nNew < expNew || nNew > expNew {
if nNew != expNew {
t . Fatalf ( "%s - expected new addrs %d, got %d" , dbgStr , expNew , nNew )
t . Fatalf ( "%s - expected new addrs %d, got %d" , dbgStr , expNew , nNew )
}
}
if nOld < expOld || nOld > expOld {
if nOld != expOld {
t . Fatalf ( "%s - expected old addrs %d, got %d" , dbgStr , expOld , nOld )
t . Fatalf ( "%s - expected old addrs %d, got %d" , dbgStr , expOld , nOld )
}
}
@ -499,15 +497,12 @@ func testAddrBookAddressSelection(t *testing.T, bookSize int) {
case expOld == 0 : // all new addresses
case expOld == 0 : // all new addresses
expSeqLens = [ ] int { nAddrs }
expSeqLens = [ ] int { nAddrs }
expSeqTypes = [ ] int { 1 }
expSeqTypes = [ ] int { 1 }
case expFirst New == 0 : // all old addresses
case expNew == 0 : // all old addresses
expSeqLens = [ ] int { nAddrs }
expSeqLens = [ ] int { nAddrs }
expSeqTypes = [ ] int { 2 }
expSeqTypes = [ ] int { 2 }
case nAddrs - expFirst New - expOld == 0 : // new addresses, old addresses
expSeqLens = [ ] int { expFirst New , expOld }
case nAddrs - expNew - expOld == 0 : // new addresses, old addresses
expSeqLens = [ ] int { expNew , expOld }
expSeqTypes = [ ] int { 1 , 2 }
expSeqTypes = [ ] int { 1 , 2 }
default : // new addresses, old addresses, new addresses
expSeqLens = [ ] int { expFirstNew , expOld , expLastNew }
expSeqTypes = [ ] int { 1 , 2 , 1 }
}
}
assert . Equal ( t , expSeqLens , seqLens ,
assert . Equal ( t , expSeqLens , seqLens ,