github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/swarm/network/simulation/bucket_test.go (about)

     1  
     2  //此源码被清华学神尹成大魔王专业翻译分析并修改
     3  //尹成QQ77025077
     4  //尹成微信18510341407
     5  //尹成所在QQ群721929980
     6  //尹成邮箱 yinc13@mails.tsinghua.edu.cn
     7  //尹成毕业于清华大学,微软区块链领域全球最有价值专家
     8  //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
     9  //版权所有2018 Go Ethereum作者
    10  //此文件是Go以太坊库的一部分。
    11  //
    12  //Go-Ethereum库是免费软件:您可以重新分发它和/或修改
    13  //根据GNU发布的较低通用公共许可证的条款
    14  //自由软件基金会,或者许可证的第3版,或者
    15  //(由您选择)任何更高版本。
    16  //
    17  //Go以太坊图书馆的发行目的是希望它会有用,
    18  //但没有任何保证;甚至没有
    19  //适销性或特定用途的适用性。见
    20  //GNU较低的通用公共许可证,了解更多详细信息。
    21  //
    22  //你应该收到一份GNU较低级别的公共许可证副本
    23  //以及Go以太坊图书馆。如果没有,请参见<http://www.gnu.org/licenses/>。
    24  
    25  package simulation
    26  
    27  import (
    28  	"sync"
    29  	"testing"
    30  
    31  	"github.com/ethereum/go-ethereum/node"
    32  	"github.com/ethereum/go-ethereum/p2p/simulations/adapters"
    33  )
    34  
    35  //TestServiceBucket使用子测试测试所有Bucket功能。
    36  //它通过向两个节点的bucket中添加项来构造两个节点的模拟。
    37  //在servicefunc构造函数中,然后通过setnodeitem。测试upnodesitems
    38  //通过停止一个节点并验证其项的可用性来完成。
    39  func TestServiceBucket(t *testing.T) {
    40  	testKey := "Key"
    41  	testValue := "Value"
    42  
    43  	sim := New(map[string]ServiceFunc{
    44  		"noop": func(ctx *adapters.ServiceContext, b *sync.Map) (node.Service, func(), error) {
    45  			b.Store(testKey, testValue+ctx.Config.ID.String())
    46  			return newNoopService(), nil, nil
    47  		},
    48  	})
    49  	defer sim.Close()
    50  
    51  	id1, err := sim.AddNode()
    52  	if err != nil {
    53  		t.Fatal(err)
    54  	}
    55  
    56  	id2, err := sim.AddNode()
    57  	if err != nil {
    58  		t.Fatal(err)
    59  	}
    60  
    61  	t.Run("ServiceFunc bucket Store", func(t *testing.T) {
    62  		v, ok := sim.NodeItem(id1, testKey)
    63  		if !ok {
    64  			t.Fatal("bucket item not found")
    65  		}
    66  		s, ok := v.(string)
    67  		if !ok {
    68  			t.Fatal("bucket item value is not string")
    69  		}
    70  		if s != testValue+id1.String() {
    71  			t.Fatalf("expected %q, got %q", testValue+id1.String(), s)
    72  		}
    73  
    74  		v, ok = sim.NodeItem(id2, testKey)
    75  		if !ok {
    76  			t.Fatal("bucket item not found")
    77  		}
    78  		s, ok = v.(string)
    79  		if !ok {
    80  			t.Fatal("bucket item value is not string")
    81  		}
    82  		if s != testValue+id2.String() {
    83  			t.Fatalf("expected %q, got %q", testValue+id2.String(), s)
    84  		}
    85  	})
    86  
    87  	customKey := "anotherKey"
    88  	customValue := "anotherValue"
    89  
    90  	t.Run("SetNodeItem", func(t *testing.T) {
    91  		sim.SetNodeItem(id1, customKey, customValue)
    92  
    93  		v, ok := sim.NodeItem(id1, customKey)
    94  		if !ok {
    95  			t.Fatal("bucket item not found")
    96  		}
    97  		s, ok := v.(string)
    98  		if !ok {
    99  			t.Fatal("bucket item value is not string")
   100  		}
   101  		if s != customValue {
   102  			t.Fatalf("expected %q, got %q", customValue, s)
   103  		}
   104  
   105  		v, ok = sim.NodeItem(id2, customKey)
   106  		if ok {
   107  			t.Fatal("bucket item should not be found")
   108  		}
   109  	})
   110  
   111  	if err := sim.StopNode(id2); err != nil {
   112  		t.Fatal(err)
   113  	}
   114  
   115  	t.Run("UpNodesItems", func(t *testing.T) {
   116  		items := sim.UpNodesItems(testKey)
   117  
   118  		v, ok := items[id1]
   119  		if !ok {
   120  			t.Errorf("node 1 item not found")
   121  		}
   122  		s, ok := v.(string)
   123  		if !ok {
   124  			t.Fatal("node 1 item value is not string")
   125  		}
   126  		if s != testValue+id1.String() {
   127  			t.Fatalf("expected %q, got %q", testValue+id1.String(), s)
   128  		}
   129  
   130  		v, ok = items[id2]
   131  		if ok {
   132  			t.Errorf("node 2 item should not be found")
   133  		}
   134  	})
   135  
   136  	t.Run("NodeItems", func(t *testing.T) {
   137  		items := sim.NodesItems(testKey)
   138  
   139  		v, ok := items[id1]
   140  		if !ok {
   141  			t.Errorf("node 1 item not found")
   142  		}
   143  		s, ok := v.(string)
   144  		if !ok {
   145  			t.Fatal("node 1 item value is not string")
   146  		}
   147  		if s != testValue+id1.String() {
   148  			t.Fatalf("expected %q, got %q", testValue+id1.String(), s)
   149  		}
   150  
   151  		v, ok = items[id2]
   152  		if !ok {
   153  			t.Errorf("node 2 item not found")
   154  		}
   155  		s, ok = v.(string)
   156  		if !ok {
   157  			t.Fatal("node 1 item value is not string")
   158  		}
   159  		if s != testValue+id2.String() {
   160  			t.Fatalf("expected %q, got %q", testValue+id2.String(), s)
   161  		}
   162  	})
   163  }