本文整理汇总了Golang中github.com/cockroachdb/cockroach/util.CreateTestAddr函数的典型用法代码示例。如果您正苦于以下问题:Golang CreateTestAddr函数的具体用法?Golang CreateTestAddr怎么用?Golang CreateTestAddr使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了CreateTestAddr函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: startFakeServerGossip
// startFakeServerGossip creates local gossip instances and remote faked gossip instance.
// The remote gossip instance launches its faked gossip service just for
// check the client message.
func startFakeServerGossip(t *testing.T) (local *Gossip, remote *fakeGossipServer, stopper *stop.Stopper) {
lclock := hlc.NewClock(hlc.UnixNano)
stopper = stop.NewStopper()
lRPCContext := rpc.NewContext(&base.Context{Insecure: true}, lclock, stopper)
laddr := util.CreateTestAddr("tcp")
lserver := rpc.NewServer(laddr, lRPCContext)
if err := lserver.Start(); err != nil {
t.Fatal(err)
}
local = New(lRPCContext, TestBootstrap)
local.start(lserver, stopper)
rclock := hlc.NewClock(hlc.UnixNano)
raddr := util.CreateTestAddr("tcp")
rRPCContext := rpc.NewContext(&base.Context{Insecure: true}, rclock, stopper)
rserver := rpc.NewServer(raddr, rRPCContext)
if err := rserver.Start(); err != nil {
t.Fatal(err)
}
remote, err := newFakeGossipServer(rserver, stopper)
if err != nil {
t.Fatal(err)
}
addr := rserver.Addr()
remote.nodeAddr = util.MakeUnresolvedAddr(addr.Network(), addr.String())
time.Sleep(time.Millisecond)
return
}
示例2: TestNodeJoin
// TestNodeJoin verifies a new node is able to join a bootstrapped
// cluster consisting of one node.
func TestNodeJoin(t *testing.T) {
defer leaktest.AfterTest(t)
engineStopper := stop.NewStopper()
defer engineStopper.Stop()
e := engine.NewInMem(roachpb.Attributes{}, 1<<20, engineStopper)
stopper := stop.NewStopper()
_, err := BootstrapCluster("cluster-1", []engine.Engine{e}, stopper)
if err != nil {
t.Fatal(err)
}
stopper.Stop()
// Set an aggressive gossip interval to make sure information is exchanged tout de suite.
testContext.GossipInterval = gossip.TestInterval
// Start the bootstrap node.
engines1 := []engine.Engine{e}
addr1 := util.CreateTestAddr("tcp")
server1, node1, stopper1 := createAndStartTestNode(addr1, engines1, addr1, t)
defer stopper1.Stop()
// Create a new node.
engines2 := []engine.Engine{engine.NewInMem(roachpb.Attributes{}, 1<<20, engineStopper)}
server2, node2, stopper2 := createAndStartTestNode(util.CreateTestAddr("tcp"), engines2, server1.Addr(), t)
defer stopper2.Stop()
// Verify new node is able to bootstrap its store.
if err := util.IsTrueWithin(func() bool { return node2.lSender.GetStoreCount() == 1 }, 50*time.Millisecond); err != nil {
t.Fatal(err)
}
// Verify node1 sees node2 via gossip and vice versa.
node1Key := gossip.MakeNodeIDKey(node1.Descriptor.NodeID)
node2Key := gossip.MakeNodeIDKey(node2.Descriptor.NodeID)
if err := util.IsTrueWithin(func() bool {
nodeDesc1 := &roachpb.NodeDescriptor{}
if err := node1.ctx.Gossip.GetInfoProto(node2Key, nodeDesc1); err != nil {
return false
}
if addr2 := nodeDesc1.Address.AddressField; addr2 != server2.Addr().String() {
t.Errorf("addr2 gossip %s doesn't match addr2 address %s", addr2, server2.Addr().String())
}
nodeDesc2 := &roachpb.NodeDescriptor{}
if err := node2.ctx.Gossip.GetInfoProto(node1Key, nodeDesc2); err != nil {
return false
}
if addr1 := nodeDesc2.Address.AddressField; addr1 != server1.Addr().String() {
t.Errorf("addr1 gossip %s doesn't match addr1 address %s", addr1, server1.Addr().String())
}
return true
}, 50*time.Millisecond); err != nil {
t.Error(err)
}
}
示例3: startGossip
// startGossip creates local and remote gossip instances.
// Both remote and local instances launch the gossip service.
func startGossip(t *testing.T) (local, remote *Gossip, stopper *stop.Stopper) {
stopper = stop.NewStopper()
lclock := hlc.NewClock(hlc.UnixNano)
lRPCContext := rpc.NewContext(&base.Context{Insecure: true}, lclock, stopper)
laddr := util.CreateTestAddr("tcp")
lserver := rpc.NewServer(lRPCContext)
lTLSConfig, err := lRPCContext.GetServerTLSConfig()
if err != nil {
t.Fatal(err)
}
lln, err := util.ListenAndServe(stopper, lserver, laddr, lTLSConfig)
if err != nil {
t.Fatal(err)
}
local = New(lRPCContext, TestBootstrap)
local.SetNodeID(1)
if err := local.SetNodeDescriptor(&roachpb.NodeDescriptor{
NodeID: 1,
Address: util.MakeUnresolvedAddr(laddr.Network(), laddr.String()),
}); err != nil {
t.Fatal(err)
}
rclock := hlc.NewClock(hlc.UnixNano)
rRPCContext := rpc.NewContext(&base.Context{Insecure: true}, rclock, stopper)
raddr := util.CreateTestAddr("tcp")
rserver := rpc.NewServer(rRPCContext)
rTLSConfig, err := rRPCContext.GetServerTLSConfig()
if err != nil {
t.Fatal(err)
}
rln, err := util.ListenAndServe(stopper, rserver, raddr, rTLSConfig)
if err != nil {
t.Fatal(err)
}
remote = New(rRPCContext, TestBootstrap)
remote.SetNodeID(2)
if err := remote.SetNodeDescriptor(&roachpb.NodeDescriptor{
NodeID: 2,
Address: util.MakeUnresolvedAddr(raddr.Network(), raddr.String()),
}); err != nil {
t.Fatal(err)
}
local.start(lserver, lln.Addr(), stopper)
remote.start(rserver, rln.Addr(), stopper)
time.Sleep(time.Millisecond)
return
}
示例4: startGossip
// startGossip creates local and remote gossip instances.
// The remote gossip instance launches its gossip service.
func startGossip(t *testing.T) (local, remote *Gossip, lserver, rserver *rpc.Server) {
laddr := util.CreateTestAddr("unix")
lserver = rpc.NewServer(laddr)
lserver.Start()
local = New()
raddr := util.CreateTestAddr("unix")
rserver = rpc.NewServer(raddr)
rserver.Start()
remote = New()
local.start(lserver)
remote.start(rserver)
time.Sleep(time.Millisecond)
return
}
示例5: TestNodeJoin
// TestNodeJoin verifies a new node is able to join a bootstrapped
// cluster consisting of one node.
func TestNodeJoin(t *testing.T) {
defer leaktest.AfterTest(t)()
engineStopper := stop.NewStopper()
defer engineStopper.Stop()
e := engine.NewInMem(roachpb.Attributes{}, 1<<20, engineStopper)
if _, err := bootstrapCluster([]engine.Engine{e}, kv.NewTxnMetrics(metric.NewRegistry())); err != nil {
t.Fatal(err)
}
// Start the bootstrap node.
engines1 := []engine.Engine{e}
addr1 := util.CreateTestAddr("tcp")
_, server1Addr, node1, stopper1 := createAndStartTestNode(addr1, engines1, addr1, t)
defer stopper1.Stop()
// Create a new node.
engines2 := []engine.Engine{engine.NewInMem(roachpb.Attributes{}, 1<<20, engineStopper)}
addr2 := util.CreateTestAddr("tcp")
_, server2Addr, node2, stopper2 := createAndStartTestNode(addr2, engines2, server1Addr, t)
defer stopper2.Stop()
// Verify new node is able to bootstrap its store.
util.SucceedsSoon(t, func() error {
if sc := node2.stores.GetStoreCount(); sc != 1 {
return util.Errorf("GetStoreCount() expected 1; got %d", sc)
}
return nil
})
// Verify node1 sees node2 via gossip and vice versa.
node1Key := gossip.MakeNodeIDKey(node1.Descriptor.NodeID)
node2Key := gossip.MakeNodeIDKey(node2.Descriptor.NodeID)
util.SucceedsSoon(t, func() error {
var nodeDesc1 roachpb.NodeDescriptor
if err := node1.ctx.Gossip.GetInfoProto(node2Key, &nodeDesc1); err != nil {
return err
}
if addr2Str, server2AddrStr := nodeDesc1.Address.String(), server2Addr.String(); addr2Str != server2AddrStr {
return util.Errorf("addr2 gossip %s doesn't match addr2 address %s", addr2Str, server2AddrStr)
}
var nodeDesc2 roachpb.NodeDescriptor
if err := node2.ctx.Gossip.GetInfoProto(node1Key, &nodeDesc2); err != nil {
return err
}
if addr1Str, server1AddrStr := nodeDesc2.Address.String(), server1Addr.String(); addr1Str != server1AddrStr {
return util.Errorf("addr1 gossip %s doesn't match addr1 address %s", addr1Str, server1AddrStr)
}
return nil
})
}
示例6: TestCorruptedClusterID
// TestCorruptedClusterID verifies that a node fails to start when a
// store's cluster ID is empty.
func TestCorruptedClusterID(t *testing.T) {
defer leaktest.AfterTest(t)
engineStopper := stop.NewStopper()
e := engine.NewInMem(roachpb.Attributes{}, 1<<20, engineStopper)
defer engineStopper.Stop()
if _, err := bootstrapCluster([]engine.Engine{e}); err != nil {
t.Fatal(err)
}
// Set the cluster ID to the empty UUID.
sIdent := roachpb.StoreIdent{
ClusterID: *uuid.EmptyUUID,
NodeID: 1,
StoreID: 1,
}
if err := engine.MVCCPutProto(e, nil, keys.StoreIdentKey(), roachpb.ZeroTimestamp, nil, &sIdent); err != nil {
t.Fatal(err)
}
engines := []engine.Engine{e}
server, serverAddr, _, node, stopper := createTestNode(util.CreateTestAddr("tcp"), engines, nil, t)
stopper.Stop()
if err := node.start(server, serverAddr, engines, roachpb.Attributes{}); !testutils.IsError(err, "unidentified store") {
t.Errorf("unexpected error %v", err)
}
}
示例7: createAndStartNewServer
// createAndStartNewServer creates and starts a new server with a test address.
func createAndStartNewServer(t *testing.T, ctx *Context) *Server {
s := NewServer(util.CreateTestAddr("tcp"), ctx)
if err := s.Start(); err != nil {
t.Fatal(err)
}
return s
}
示例8: startGossip
// startGossip creates and starts a gossip instance.
func startGossip(nodeID roachpb.NodeID, stopper *stop.Stopper, t *testing.T) *Gossip {
clock := hlc.NewClock(hlc.UnixNano)
rpcContext := rpc.NewContext(&base.Context{Insecure: true}, clock, stopper)
addr := util.CreateTestAddr("tcp")
server := rpc.NewServer(rpcContext)
tlsConfig, err := rpcContext.GetServerTLSConfig()
if err != nil {
t.Fatal(err)
}
ln, err := util.ListenAndServe(stopper, server, addr, tlsConfig)
if err != nil {
t.Fatal(err)
}
g := New(rpcContext, TestBootstrap, stopper)
g.SetNodeID(nodeID)
if err := g.SetNodeDescriptor(&roachpb.NodeDescriptor{
NodeID: nodeID,
Address: util.MakeUnresolvedAddr(addr.Network(), addr.String()),
}); err != nil {
t.Fatal(err)
}
g.start(server, ln.Addr())
time.Sleep(time.Millisecond)
return g
}
示例9: newTestServer
func newTestServer(t *testing.T, ctx *Context, manual bool) (*Server, net.Listener) {
var s *Server
if manual {
s = &Server{
insecure: ctx.Insecure,
activeConns: make(map[net.Conn]struct{}),
methods: map[string]method{},
}
} else {
s = NewServer(ctx)
}
tlsConfig, err := ctx.GetServerTLSConfig()
if err != nil {
t.Fatal(err)
}
addr := util.CreateTestAddr("tcp")
ln, err := util.ListenAndServe(ctx.Stopper, s, addr, tlsConfig)
if err != nil {
t.Fatal(err)
}
return s, ln
}
示例10: TestCorruptedClusterID
// TestCorruptedClusterID verifies that a node fails to start when a
// store's cluster ID is empty.
func TestCorruptedClusterID(t *testing.T) {
defer leaktest.AfterTest(t)
eagerStopper := stop.NewStopper()
e := engine.NewInMem(proto.Attributes{}, 1<<20)
_, err := BootstrapCluster("cluster-1", []engine.Engine{e}, eagerStopper)
if err != nil {
t.Fatal(err)
}
eagerStopper.Stop()
// Set the cluster ID to an empty string.
sIdent := proto.StoreIdent{
ClusterID: "",
NodeID: 1,
StoreID: 1,
}
if err = engine.MVCCPutProto(e, nil, keys.StoreIdentKey(), proto.ZeroTimestamp, nil, &sIdent); err != nil {
t.Fatal(err)
}
engines := []engine.Engine{e}
server, _, node, stopper := createTestNode(util.CreateTestAddr("tcp"), engines, nil, t)
if err := node.start(server, engines, proto.Attributes{}, stopper); err == nil {
t.Errorf("unexpected success")
}
stopper.Stop()
}
示例11: TestClientHeartbeatBadServer
// TestClientHeartbeatBadServer verifies that the client is not marked
// as "ready" until a heartbeat request succeeds.
func TestClientHeartbeatBadServer(t *testing.T) {
tlsConfig, err := LoadTestTLSConfig("..")
if err != nil {
t.Fatal(err)
}
addr := util.CreateTestAddr("tcp")
// Create a server which doesn't support heartbeats.
s := &Server{
Server: rpc.NewServer(),
tlsConfig: tlsConfig,
addr: addr,
closeCallbacks: make([]func(conn net.Conn), 0, 1),
}
if err := s.Start(); err != nil {
t.Fatal(err)
}
// Now, create a client. It should attempt a heartbeat and fail,
// causing retry loop to activate.
c := NewClient(s.Addr(), nil, tlsConfig)
select {
case <-c.Ready:
t.Error("unexpected client heartbeat success")
case <-c.Closed:
}
s.Close()
}
示例12: TestBootstrapNewStore
// TestBootstrapNewStore starts a cluster with two unbootstrapped
// stores and verifies both stores are added.
func TestBootstrapNewStore(t *testing.T) {
e := engine.NewInMem(engine.Attributes{}, 1<<20)
localDB, err := BootstrapCluster("cluster-1", e)
if err != nil {
t.Fatal(err)
}
localDB.Close()
// Start a new node with two new stores which will require bootstrapping.
engines := []engine.Engine{
e,
engine.NewInMem(engine.Attributes{}, 1<<20),
engine.NewInMem(engine.Attributes{}, 1<<20),
}
server, node := createTestNode(util.CreateTestAddr("tcp"), engines, nil, t)
defer server.Close()
// Non-initialized stores (in this case the new in-memory-based
// store) will be bootstrapped by the node upon start. This happens
// in a goroutine, so we'll have to wait a bit (maximum 10ms) until
// we can find the new node.
if err := util.IsTrueWithin(func() bool { return node.localDB.GetStoreCount() == 3 }, 50*time.Millisecond); err != nil {
t.Error(err)
}
}
示例13: Listen
func (lt *localRPCTransport) Listen(id roachpb.StoreID, server ServerInterface) error {
addr := util.CreateTestAddr("tcp")
rpcServer := crpc.NewServer(addr, &crpc.Context{
Context: base.Context{
Insecure: true,
},
Stopper: lt.stopper,
DisableCache: true,
})
err := rpcServer.RegisterAsync(raftMessageName, false, /*not public*/
func(argsI proto.Message, callback func(proto.Message, error)) {
args := argsI.(*RaftMessageRequest)
resp, err := server.RaftMessage(args)
callback(resp, err)
}, &RaftMessageRequest{})
if err != nil {
return err
}
lt.mu.Lock()
if _, ok := lt.servers[id]; ok {
log.Fatalf("node %d already listening", id)
}
lt.servers[id] = rpcServer
lt.mu.Unlock()
return rpcServer.Start()
}
示例14: Listen
func (lt *localRPCTransport) Listen(id proto.RaftNodeID, server ServerInterface) error {
addr := util.CreateTestAddr("tcp")
rpcServer := crpc.NewServer(addr, &crpc.Context{
Context: base.Context{
Insecure: true,
},
Stopper: lt.stopper,
DisableCache: true,
})
err := rpcServer.RegisterAsync(raftMessageName,
func(argsI gogoproto.Message, callback func(gogoproto.Message, error)) {
protoArgs := argsI.(*proto.RaftMessageRequest)
args := RaftMessageRequest{
GroupID: protoArgs.GroupID,
}
if err := args.Message.Unmarshal(protoArgs.Msg); err != nil {
callback(nil, err)
}
err := server.RaftMessage(&args, &RaftMessageResponse{})
callback(&proto.RaftMessageResponse{}, err)
}, &proto.RaftMessageRequest{})
if err != nil {
return err
}
lt.mu.Lock()
if _, ok := lt.servers[id]; ok {
log.Fatalf("node %d already listening", id)
}
lt.servers[id] = rpcServer
lt.mu.Unlock()
return rpcServer.Start()
}
示例15: SimulateNetwork
// SimulateNetwork creates nodeCount gossip nodes. The network should
// be set to either "tcp" or "unix". The gossipInterval should be set
// to a compressed simulation timescale, though large enough to give
// the concurrent goroutines enough time to pass data back and forth
// in order to yield accurate estimates of how old data actually ends
// up being at the various nodes. After each gossipInterval period,
// simCallback is invoked; when it returns false, the simulation
// ends. If it returns true, the simulation continues another cycle.
//
// Node0 gossips the node count as well as the gossip sentinel. The
// gossip bootstrap hosts are set to the first three nodes (or fewer if
// less than three are available).
//
// At each cycle of the simulation, node 0 gossips the sentinel. If
// the simulation requires other nodes to gossip, this should be done
// via simCallback.
//
// The simulation callback receives a map of nodes, keyed by node address.
func SimulateNetwork(nodeCount int, network string, gossipInterval time.Duration,
simCallback func(cycle int, nodes map[string]*Gossip) bool) {
// seed the random number generator for non-determinism across
// multiple runs.
rand.Seed(time.Now().UTC().UnixNano())
tlsConfig := rpc.LoadInsecureTLSConfig()
log.Infof("simulating network with %d nodes", nodeCount)
servers := make([]*rpc.Server, nodeCount)
addrs := make([]net.Addr, nodeCount)
for i := 0; i < nodeCount; i++ {
addr := util.CreateTestAddr(network)
servers[i] = rpc.NewServer(addr, tlsConfig)
if err := servers[i].Start(); err != nil {
log.Fatal(err)
}
addrs[i] = servers[i].Addr()
}
var bootstrap []net.Addr
if nodeCount < 3 {
bootstrap = addrs
} else {
bootstrap = addrs[:3]
}
nodes := make(map[string]*Gossip, nodeCount)
for i := 0; i < nodeCount; i++ {
node := New(tlsConfig)
node.Name = fmt.Sprintf("Node%d", i)
node.SetBootstrap(bootstrap)
node.SetInterval(gossipInterval)
node.Start(servers[i])
// Node 0 gossips node count.
if i == 0 {
node.AddInfo(KeyNodeCount, int64(nodeCount), time.Hour)
}
nodes[addrs[i].String()] = node
}
gossipTimeout := time.Tick(gossipInterval)
var complete bool
for cycle := 0; !complete; cycle++ {
select {
case <-gossipTimeout:
// Node 0 gossips sentinel every cycle.
nodes[addrs[0].String()].AddInfo(KeySentinel, int64(cycle), time.Hour)
if !simCallback(cycle, nodes) {
complete = true
}
}
}
// Stop all servers & nodes.
for i := 0; i < nodeCount; i++ {
servers[i].Close()
nodes[addrs[i].String()].Stop()
}
}