|
@@ -68,6 +68,14 @@ func (i poolTestPeer) freeClientId() string {
|
|
|
|
|
|
|
|
func (i poolTestPeer) updateCapacity(uint64) {}
|
|
func (i poolTestPeer) updateCapacity(uint64) {}
|
|
|
|
|
|
|
|
|
|
+type poolTestPeerWithCap struct {
|
|
|
|
|
+ poolTestPeer
|
|
|
|
|
+
|
|
|
|
|
+ cap uint64
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func (i *poolTestPeerWithCap) updateCapacity(cap uint64) { i.cap = cap }
|
|
|
|
|
+
|
|
|
func testClientPool(t *testing.T, connLimit, clientCount, paidCount int, randomDisconnect bool) {
|
|
func testClientPool(t *testing.T, connLimit, clientCount, paidCount int, randomDisconnect bool) {
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
var (
|
|
var (
|
|
@@ -308,9 +316,9 @@ func TestFreeClientKickedOut(t *testing.T) {
|
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
for i := 0; i < 10; i++ {
|
|
|
pool.connect(poolTestPeer(i), 1)
|
|
pool.connect(poolTestPeer(i), 1)
|
|
|
- clock.Run(100 * time.Millisecond)
|
|
|
|
|
|
|
+ clock.Run(time.Millisecond)
|
|
|
}
|
|
}
|
|
|
- if pool.connect(poolTestPeer(11), 1) {
|
|
|
|
|
|
|
+ if pool.connect(poolTestPeer(10), 1) {
|
|
|
t.Fatalf("New free client should be rejected")
|
|
t.Fatalf("New free client should be rejected")
|
|
|
}
|
|
}
|
|
|
clock.Run(5 * time.Minute)
|
|
clock.Run(5 * time.Minute)
|
|
@@ -320,8 +328,8 @@ func TestFreeClientKickedOut(t *testing.T) {
|
|
|
for i := 0; i < 10; i++ {
|
|
for i := 0; i < 10; i++ {
|
|
|
select {
|
|
select {
|
|
|
case id := <-kicked:
|
|
case id := <-kicked:
|
|
|
- if id != i {
|
|
|
|
|
- t.Fatalf("Kicked client mismatch, want %v, got %v", i, id)
|
|
|
|
|
|
|
+ if id >= 10 {
|
|
|
|
|
+ t.Fatalf("Old client should be kicked, now got: %d", id)
|
|
|
}
|
|
}
|
|
|
case <-time.NewTimer(time.Second).C:
|
|
case <-time.NewTimer(time.Second).C:
|
|
|
t.Fatalf("timeout")
|
|
t.Fatalf("timeout")
|
|
@@ -364,11 +372,20 @@ func TestDowngradePriorityClient(t *testing.T) {
|
|
|
pool.setLimits(10, uint64(10)) // Total capacity limit is 10
|
|
pool.setLimits(10, uint64(10)) // Total capacity limit is 10
|
|
|
pool.setPriceFactors(priceFactors{1, 0, 1}, priceFactors{1, 0, 1})
|
|
pool.setPriceFactors(priceFactors{1, 0, 1}, priceFactors{1, 0, 1})
|
|
|
|
|
|
|
|
- pool.addBalance(poolTestPeer(0).ID(), uint64(time.Minute), false)
|
|
|
|
|
- pool.connect(poolTestPeer(0), 10)
|
|
|
|
|
|
|
+ p := &poolTestPeerWithCap{
|
|
|
|
|
+ poolTestPeer: poolTestPeer(0),
|
|
|
|
|
+ }
|
|
|
|
|
+ pool.addBalance(p.ID(), uint64(time.Minute), false)
|
|
|
|
|
+ pool.connect(p, 10)
|
|
|
|
|
+ if p.cap != 10 {
|
|
|
|
|
+ t.Fatalf("The capcacity of priority peer hasn't been updated, got: %d", p.cap)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
clock.Run(time.Minute) // All positive balance should be used up.
|
|
clock.Run(time.Minute) // All positive balance should be used up.
|
|
|
time.Sleep(300 * time.Millisecond) // Ensure the callback is called
|
|
time.Sleep(300 * time.Millisecond) // Ensure the callback is called
|
|
|
-
|
|
|
|
|
|
|
+ if p.cap != 1 {
|
|
|
|
|
+ t.Fatalf("The capcacity of peer should be downgraded, got: %d", p.cap)
|
|
|
|
|
+ }
|
|
|
pb := pool.ndb.getOrNewPB(poolTestPeer(0).ID())
|
|
pb := pool.ndb.getOrNewPB(poolTestPeer(0).ID())
|
|
|
if pb.value != 0 {
|
|
if pb.value != 0 {
|
|
|
t.Fatalf("Positive balance mismatch, want %v, got %v", 0, pb.value)
|
|
t.Fatalf("Positive balance mismatch, want %v, got %v", 0, pb.value)
|