Example #1
0
File: test.go Project: aemengo/nats
func RunServerWithConfig(configFile string) (*server.Server, *server.Options) {
	return gnatsd.RunServerWithConfig(configFile)
}
func runTest(url, user string, count int64) {
	var wg sync.WaitGroup
	var srv *server.Server

	if useLocalServer {
		srv, _ = test.RunServerWithConfig("./gnatsd.conf")
	}

	opts := nats.DefaultOptions
	opts.Timeout = time.Second * 600
	opts.Url = url
	opts.User = user
	opts.Password = "******"

	opts.ReconnectedCB = func(c *nats.Conn) {
		if *doReconnect {
			wg.Done()
		}
	}

	var connStartTime = time.Now()

	connList := make([]*nats.Conn, 0, count)
	wg.Add(int(count))

	// create connections simultaneously to make the test complete faster.
	for i := int64(0); i < count; i++ {
		go func() {
			// randomize connect times to prevent connection read errors
			time.Sleep(time.Millisecond * time.Duration(rand.Intn(2000)))
			nc, err := opts.Connect()
			if err != nil {
				log.Fatalf("Can't connect: %v\n", err)
			}
			connList = append(connList, nc)
			wg.Done()
		}()
	}
	defer func() {
		for _, nc := range connList {
			nc.Close()
		}
	}()

	// wait for all connections to connect
	wg.Wait()
	totalConnectTime := time.Now().Sub(connStartTime)

	var reconnectWaitTime time.Duration

	// Bounce the server
	if *doReconnect {
		wg.Add(int(count))

		srv.Shutdown()
		disconnectTime := time.Now()
		srv, _ = test.RunServerWithConfig("./gnatsd.conf")

		// wait for all connections to reconnect
		wg.Wait()
		reconnectWaitTime = time.Now().Sub(disconnectTime)
	}
	defer srv.Shutdown()

	if *csvOutput {
		log.Printf("%s,%f", user, totalConnectTime.Seconds())
	} else {
		if *doReconnect {
			log.Printf("user=%s, connect time=%v, reconnect time=%v",
				user, totalConnectTime, reconnectWaitTime)
		} else {
			log.Printf("user=%s, connect time=%v",
				user, totalConnectTime)
		}
	}
}