// StartActionLoop will start the action loop for a fake tablet, // using ft.FakeMysqlDaemon as the backing mysqld. func (ft *FakeTablet) StartActionLoop(t *testing.T, wr *wrangler.Wrangler) { if ft.Agent != nil { t.Fatalf("Agent for %v is already running", ft.Tablet.Alias) } // Listen on a random port for gRPC var err error ft.Listener, err = net.Listen("tcp", ":0") if err != nil { t.Fatalf("Cannot listen: %v", err) } gRPCPort := int32(ft.Listener.Addr().(*net.TCPAddr).Port) // if needed, listen on a random port for HTTP vtPort := ft.Tablet.PortMap["vt"] if ft.StartHTTPServer { ft.HTTPListener, err = net.Listen("tcp", ":0") if err != nil { t.Fatalf("Cannot listen on http port: %v", err) } handler := http.NewServeMux() ft.HTTPServer = http.Server{ Handler: handler, } go ft.HTTPServer.Serve(ft.HTTPListener) vtPort = int32(ft.HTTPListener.Addr().(*net.TCPAddr).Port) } // create a test agent on that port, and re-read the record // (it has new ports and IP) ft.Agent = tabletmanager.NewTestActionAgent(context.Background(), wr.TopoServer(), ft.Tablet.Alias, vtPort, gRPCPort, ft.FakeMysqlDaemon) ft.Tablet = ft.Agent.Tablet() // create the gRPC server ft.RPCServer = grpc.NewServer() grpctmserver.RegisterForTest(ft.RPCServer, ft.Agent) go ft.RPCServer.Serve(ft.Listener) // and wait for it to serve, so we don't start using it before it's // ready. timeout := 5 * time.Second step := 10 * time.Millisecond c := tmclient.NewTabletManagerClient() for timeout >= 0 { ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) err := c.Ping(ctx, topo.NewTabletInfo(ft.Agent.Tablet(), -1)) cancel() if err == nil { break } time.Sleep(step) timeout -= step } if timeout < 0 { panic("StartActionLoop failed.") } }
// StartActionLoop will start the action loop for a fake tablet, // using ft.FakeMysqlDaemon as the backing mysqld. func (ft *FakeTablet) StartActionLoop(t *testing.T, wr *wrangler.Wrangler) { if ft.Agent != nil { t.Fatalf("Agent for %v is already running", ft.Tablet.Alias) } // Listen on a random port for gRPC var err error ft.Listener, err = net.Listen("tcp", ":0") if err != nil { t.Fatalf("Cannot listen: %v", err) } gRPCPort := ft.Listener.Addr().(*net.TCPAddr).Port // if needed, listen on a random port for HTTP vtPort := ft.Tablet.Portmap["vt"] if ft.StartHttpServer { ft.HTTPListener, err = net.Listen("tcp", ":0") if err != nil { t.Fatalf("Cannot listen on http port: %v", err) } handler := http.NewServeMux() ft.HTTPServer = http.Server{ Handler: handler, } go ft.HTTPServer.Serve(ft.HTTPListener) vtPort = ft.HTTPListener.Addr().(*net.TCPAddr).Port } // create a test agent on that port, and re-read the record // (it has new ports and IP) ft.Agent = tabletmanager.NewTestActionAgent(context.Background(), wr.TopoServer(), ft.Tablet.Alias, vtPort, gRPCPort, ft.FakeMysqlDaemon) ft.Tablet = ft.Agent.Tablet().Tablet // create the gRPC server ft.RPCServer = grpc.NewServer() grpctmserver.RegisterForTest(ft.RPCServer, ft.Agent) go ft.RPCServer.Serve(ft.Listener) }