github.com/codysnider/go-ethereum@v1.10.18-0.20220420071915-14f4ae99222a/p2p/dnsdisc/sync_test.go (about)

     1  // Copyright 2018 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package dnsdisc
    18  
    19  import (
    20  	"math/rand"
    21  	"strconv"
    22  	"testing"
    23  )
    24  
    25  func TestLinkCache(t *testing.T) {
    26  	var lc linkCache
    27  
    28  	// Check adding links.
    29  	lc.addLink("1", "2")
    30  	if !lc.changed {
    31  		t.Error("changed flag not set")
    32  	}
    33  	lc.changed = false
    34  	lc.addLink("1", "2")
    35  	if lc.changed {
    36  		t.Error("changed flag set after adding link that's already present")
    37  	}
    38  	lc.addLink("2", "3")
    39  	lc.addLink("3", "1")
    40  	lc.addLink("2", "4")
    41  	lc.changed = false
    42  
    43  	if !lc.isReferenced("3") {
    44  		t.Error("3 not referenced")
    45  	}
    46  	if lc.isReferenced("6") {
    47  		t.Error("6 is referenced")
    48  	}
    49  
    50  	lc.resetLinks("1", nil)
    51  	if !lc.changed {
    52  		t.Error("changed flag not set")
    53  	}
    54  	if len(lc.backrefs) != 0 {
    55  		t.Logf("%+v", lc)
    56  		t.Error("reference maps should be empty")
    57  	}
    58  }
    59  
    60  func TestLinkCacheRandom(t *testing.T) {
    61  	tags := make([]string, 1000)
    62  	for i := range tags {
    63  		tags[i] = strconv.Itoa(i)
    64  	}
    65  
    66  	// Create random links.
    67  	var lc linkCache
    68  	var remove []string
    69  	for i := 0; i < 100; i++ {
    70  		a, b := tags[rand.Intn(len(tags))], tags[rand.Intn(len(tags))]
    71  		lc.addLink(a, b)
    72  		remove = append(remove, a)
    73  	}
    74  
    75  	// Remove all the links.
    76  	for _, s := range remove {
    77  		lc.resetLinks(s, nil)
    78  	}
    79  	if len(lc.backrefs) != 0 {
    80  		t.Logf("%+v", lc)
    81  		t.Error("reference maps should be empty")
    82  	}
    83  }