func TestWait(t *testing.T) { t.Parallel() expected := []byte("42") w := waitout.New(expected) sent := make(chan struct{}) go func() { defer close(sent) w.Write(expected) }() w.Wait() <-sent }
// Start the server, this will return once the server has been started. func (s *Server) Start() { port, err := freeport.Get() if err != nil { s.T.Fatalf(err.Error()) } s.Port = port dir, err := ioutil.TempDir("", "mysql-DataDir-") if err != nil { s.T.Fatalf(err.Error()) } s.DataDir = dir s.Socket = filepath.Join(dir, "socket") cf, err := os.Create(filepath.Join(dir, "my.cnf")) if err != nil { s.T.Fatalf(err.Error()) } if err := configTemplate.Execute(cf, s); err != nil { s.T.Fatalf(err.Error()) } if err := cf.Close(); err != nil { s.T.Fatalf(err.Error()) } defaultsFile := fmt.Sprintf("--defaults-file=%s", cf.Name()) s.cmd = exec.Command("mysql_install_db", defaultsFile, "--basedir", mysqlBaseDir) if os.Getenv("MYSQLTEST_VERBOSE") == "1" { s.cmd.Stdout = os.Stdout s.cmd.Stderr = os.Stderr } if err := s.cmd.Run(); err != nil { s.T.Fatalf(err.Error()) } waiter := waitout.New(mysqlReadyForConnections) s.cmd = exec.Command("mysqld", defaultsFile, "--basedir", mysqlBaseDir) if os.Getenv("MYSQLTEST_VERBOSE") == "1" { s.cmd.Stdout = os.Stdout s.cmd.Stderr = io.MultiWriter(os.Stderr, waiter) } else { s.cmd.Stderr = waiter } if err := s.cmd.Start(); err != nil { s.T.Fatalf(err.Error()) } waiter.Wait() }
func (h *Harness) start() { var err error if h.port == 0 { if h.port, err = freeport.Get(); err != nil { h.t.Fatal(err) } } cf, err := ioutil.TempFile("", "gmetric_test_gmond_conf") if err != nil { h.t.Fatal(err) } h.configPath = cf.Name() td := struct{ Port int }{Port: h.port} if err := configTemplate.Execute(cf, td); err != nil { h.t.Fatal(err) } if err := cf.Close(); err != nil { h.t.Fatal(err) } waiter := waitout.New(gmondServerStarted) h.cmd = exec.Command("gmond", "--conf", h.configPath) if os.Getenv("GMONDTEST_VERBOSE") == "1" { h.cmd.Stderr = io.MultiWriter(os.Stderr, waiter) h.cmd.Stdout = os.Stdout } else { h.cmd.Stderr = waiter } if err := h.cmd.Start(); err != nil { h.t.Fatal(err) } waiter.Wait() h.Client = &gmetric.Client{ Addr: []net.Addr{ &net.UDPAddr{IP: net.ParseIP(localhostIP), Port: h.port}, }, } if err := h.Client.Open(); err != nil { h.t.Fatal(err) } }
// Start the server, this will return once the server has been started. func (s *Server) Start(args ...string) { if s.Port == 0 { port, err := freeport.Get() if err != nil { s.T.Fatalf(err.Error()) } s.Port = port } if s.testName == "" { s.T.Fatalf("Cannot determine name for test") } dir, err := ioutil.TempDir("", "mgotest-dbpath-"+s.testName) if err != nil { s.T.Fatalf(err.Error()) } s.DBPath = dir cf, err := ioutil.TempFile(s.DBPath, "config-") if err != nil { s.T.Fatalf(err.Error()) } if err := configTemplate.Execute(cf, s); err != nil { s.T.Fatalf(err.Error()) } if err := cf.Close(); err != nil { s.T.Fatalf(err.Error()) } waiter := waitout.New(mgoWaitingForConnections) args = append(args, "--config", cf.Name(), "--setParameter", "enableTestCommands=1") s.cmd = exec.Command("mongod", args...) s.cmd.Env = envPlusLcAll() if os.Getenv("MGOTEST_VERBOSE") == "1" { s.cmd.Stdout = io.MultiWriter(os.Stdout, waiter) s.cmd.Stderr = os.Stderr } else { s.cmd.Stdout = waiter } if err := s.cmd.Start(); err != nil { s.T.Fatalf(err.Error()) } waiter.Wait() }
func TestManyInParallel(t *testing.T) { t.Parallel() const count = 100 expected := []byte("42") w := waitout.New(expected) event := make(chan bool) for i := 0; i < count; i++ { go func() { if r := recover(); r != nil { t.Fatal(r) } w.Write(expected) event <- true }() } for i := 0; i < count; i++ { go func() { if r := recover(); r != nil { t.Fatal(r) } w.Wait() event <- true }() } done := make(chan struct{}) go func() { defer close(done) for i := 0; i < count*2; i++ { <-event } }() <-done w.Wait() }