github.com/auxten/ginkgo@v0.0.0-20220130172820-7d98ad59d232/seed/ring_test.go (about)

     1  package seed
     2  
     3  import (
     4  	"sync"
     5  	"testing"
     6  
     7  	. "github.com/smartystreets/goconvey/convey"
     8  )
     9  
    10  func TestLocator(t *testing.T) {
    11  	Convey("consistent hash ring", t, func() {
    12  		ring := &Seed{
    13  			RWMutex:    sync.RWMutex{},
    14  			Blocks:     []*Block{{}, {}, {}, {}, {}, {}, {}, {}},
    15  			VNodeCount: 2,
    16  		}
    17  		ring.Remove(Host{
    18  			IP:   [4]byte{},
    19  			Port: 0,
    20  		})
    21  		hosts := ring.LocateBlock(1, 1)
    22  		So(hosts, ShouldHaveLength, 0)
    23  		hosts = ring.LocateBlock(1000, 1)
    24  		So(hosts, ShouldHaveLength, 0)
    25  		ring.Add(Host{
    26  			IP:   [4]byte{},
    27  			Port: 0,
    28  		})
    29  		hosts = ring.LocateBlock(1, 1)
    30  		So(hosts, ShouldHaveLength, 1)
    31  		hosts = ring.LocateBlock(1000, 1)
    32  		So(hosts, ShouldHaveLength, 1)
    33  		So(hosts[0].String(), ShouldResemble, "0.0.0.0:0")
    34  		So(ring.GetAllHosts()[0].String(), ShouldResemble, "0.0.0.0:0")
    35  
    36  		hosts = ring.LocateBlock(1, 2)
    37  		So(hosts, ShouldHaveLength, 1)
    38  		hosts = ring.LocateBlock(1000, 2)
    39  		So(hosts, ShouldHaveLength, 1)
    40  		So(hosts[0].String(), ShouldResemble, "0.0.0.0:0")
    41  
    42  		ring.Remove(Host{
    43  			IP:   [4]byte{},
    44  			Port: 0,
    45  		})
    46  		hosts = ring.LocateBlock(1, 1)
    47  		So(hosts, ShouldHaveLength, 0)
    48  		hosts = ring.LocateBlock(1000, 1)
    49  		So(hosts, ShouldHaveLength, 0)
    50  		So(ring.GetAllHosts(), ShouldHaveLength, 0)
    51  
    52  		for i := 0; i < 1000; i++ {
    53  			ring.Add(Host{
    54  				IP:   [4]byte{10, 0, byte(i / 256), byte(i % 256)},
    55  				Port: uint16(i),
    56  			})
    57  		}
    58  
    59  		hosts = ring.LocateBlock(1, 2)
    60  		So(hosts, ShouldHaveLength, 2)
    61  		hosts = ring.LocateBlock(1000, 2)
    62  		So(hosts, ShouldHaveLength, 2)
    63  		So(hosts[0].String(), ShouldNotResemble, hosts[1].String())
    64  		hosts2 := ring.LocateBlock(1, 2)
    65  		// should not deterministic
    66  		So(hosts[0].String(), ShouldNotResemble, hosts2[0].String())
    67  		So(ring.GetAllHosts(), ShouldHaveLength, 1000)
    68  
    69  		for i := 0; i < 1000; i++ {
    70  			ring.Remove(Host{
    71  				IP:   [4]byte{10, 0, byte(i / 256), byte(i % 256)},
    72  				Port: uint16(i),
    73  			})
    74  		}
    75  		hosts = ring.LocateBlock(1, 1)
    76  		So(hosts, ShouldHaveLength, 0)
    77  		hosts = ring.LocateBlock(1000, 1)
    78  		So(hosts, ShouldHaveLength, 0)
    79  		So(ring.GetAllHosts(), ShouldHaveLength, 0)
    80  	})
    81  }
    82  
    83  func TestParseHost(t *testing.T) {
    84  	Convey("parse IPv4 host", t, func() {
    85  		h, err := ParseHost("127.0.0.1:10001")
    86  		So(err, ShouldBeNil)
    87  		So(h, ShouldResemble, Host{
    88  			IP:   [4]byte{127, 0, 0, 1},
    89  			Port: 10001,
    90  		})
    91  	})
    92  	Convey("parse IPv6 host", t, func() {
    93  		h, err := ParseHost("[::1]:10001")
    94  		So(err, ShouldNotBeNil)
    95  		So(h, ShouldResemble, Host{
    96  			IP:   [4]byte{0, 0, 0, 0},
    97  			Port: 0,
    98  		})
    99  	})
   100  	Convey("parse bad host", t, func() {
   101  		h, err := ParseHost("127.0.0.1:1000001")
   102  		So(err, ShouldNotBeNil)
   103  		So(h, ShouldResemble, Host{
   104  			IP:   [4]byte{0, 0, 0, 0},
   105  			Port: 0,
   106  		})
   107  	})
   108  	Convey("parse bad host", t, func() {
   109  		h, err := ParseHost("1270.0.1:1000001")
   110  		So(err, ShouldNotBeNil)
   111  		So(h, ShouldResemble, Host{
   112  			IP:   [4]byte{0, 0, 0, 0},
   113  			Port: 0,
   114  		})
   115  	})
   116  }