Ejemplo n.º 1
0
func TestMain(m *testing.M) {
	security.SetReadFileFn(securitytest.Asset)
	randutil.SeedForTests()
	serverutils.InitTestServerFactory(server.TestServerFactory)
	serverutils.InitTestClusterFactory(testcluster.TestClusterFactory)
	os.Exit(m.Run())
}
Ejemplo n.º 2
0
func TestQuit(t *testing.T) {
	defer leaktest.AfterTest(t)()
	c := newCLITest()
	c.Run("quit")
	// Wait until this async command stops the server.
	<-c.Stopper().IsStopped()
	// Manually run the cleanup functions.
	c.cleanupFunc()
	security.SetReadFileFn(securitytest.Asset)
}
Ejemplo n.º 3
0
// maybeStartLocalServer will start a temporary local server if no database URL has been
// provided.
func maybeStartLocalServer() *server.TestServer {
	if *dbURL != "" {
		return nil
	}
	// Start a local test server.
	security.SetReadFileFn(securitytest.Asset)
	serv := server.StartTestServer(nil)
	*dbURL = "https://root@" + serv.ServingAddr() + "?certs=test_certs"
	return serv
}
Ejemplo n.º 4
0
func main() {
	stopper := stop.NewStopper()
	defer stopper.Stop()

	fmt.Printf("A simple program that keeps moving money between bank accounts.\n\n")
	flag.Parse()
	if *numAccounts < 2 {
		fmt.Fprintf(os.Stderr, "At least two accounts are required to transfer money.\n")
		os.Exit(1)
	}
	if *numParallelTransfers < 1 {
		fmt.Fprintf(os.Stderr, "At least one transfer routine must be active.\n")
		os.Exit(1)
	}
	if !*useTransaction {
		fmt.Printf("Use of transactions has been disabled.\n")
	}
	// Initialize the bank.
	var bank Bank
	bank.firstAccount = *firstAccount
	bank.numAccounts = *numAccounts
	if *dbName == "" {
		// Run a test cockroach instance to represent the bank.
		security.SetReadFileFn(securitytest.Asset)
		serv := server.StartTestServer(nil)
		defer serv.Stop()
		*dbName = fmt.Sprintf("rpcs://%s@%s?certs=test_certs",
			security.NodeUser, serv.ServingAddr())
	}
	// Create a database handle.
	db, err := client.Open(stopper, *dbName)
	if err != nil {
		log.Fatal(err)
	}
	bank.db = db
	// Initialize all the bank accounts.
	const initCash = 1000
	totalCash := bank.initBankAccounts(initCash)

	// Start all the money transfer routines.
	for i := 0; i < *numParallelTransfers; i++ {
		// Keep transferring upto 10% of initCash between accounts.
		go bank.continuouslyTransferMoney(initCash / 10)
	}

	go bank.periodicallyReportStats()
	bank.periodicallyCheckBalances(totalCash)
}
Ejemplo n.º 5
0
func main() {
	var dbDriver string
	flag.StringVar(&dbDriver, "db-driver", "cockroach", "database driver to use")
	var dbAddr string
	flag.StringVar(&dbAddr, "db-addr", "", "database address. defaults to ephemeral cockroach instance")

	numChannels := flag.Int("num-channels", 100, "number of channels")
	numWriters := flag.Int("num-writers", 2, "number of writers")
	flag.Parse()

	if dbAddr == "" {
		security.SetReadFileFn(securitytest.Asset)
		srv := server.StartTestServer(nil)
		defer srv.Stop()

		dbDriver = "cockroach"
		dbAddr = fmt.Sprintf("https://root@%s?certs=test_certs", srv.ServingAddr())
	}
	db, err := sql.Open(dbDriver, dbAddr)
	if err != nil {
		log.Fatal(err)
	}

	if err := createTables(db); err != nil {
		log.Fatal(err)
	}

	var stats statistics
	var wg sync.WaitGroup
	for i := 0; i < *numWriters; i++ {
		wg.Add(1)
		w := writer{db, *numChannels, &wg, &stats}
		go w.run()
	}
	go stats.report()
	wg.Wait()
}
Ejemplo n.º 6
0
func init() {
	security.SetReadFileFn(securitytest.Asset)
}
Ejemplo n.º 7
0
func (c cliTest) stop() {
	c.cleanupFunc()
	security.SetReadFileFn(securitytest.Asset)
	c.Stop()
}
Ejemplo n.º 8
0
func main() {
	flag.Parse()
	var db *sql.DB
	var err error
	var url string
	if *usePostgres {
		if db, err = sql.Open("postgres", "dbname=postgres sslmode=disable"); err != nil {
			log.Fatal(err)
		}
	} else {
		security.SetReadFileFn(securitytest.Asset)
		serv := server.StartTestServer(nil)
		defer serv.Stop()
		url = "https://root@" + serv.ServingAddr() + "?certs=test_certs"
		if db, err = sql.Open("cockroach", url); err != nil {
			log.Fatal(err)
		}
	}
	if _, err := db.Exec("CREATE DATABASE bank"); err != nil {
		if pqErr, ok := err.(*pq.Error); *usePostgres && (!ok || pqErr.Code.Name() != "duplicate_database") {
			log.Fatal(err)
		}
	}
	db.Close()

	// Open db client with database settings.
	if *usePostgres {
		if db, err = sql.Open("postgres", "dbname=bank sslmode=disable"); err != nil {
			log.Fatal(err)
		}
	} else {
		if db, err = sql.Open("cockroach", url+"&database=bank"); err != nil {
			log.Fatal(err)
		}
	}

	// concurrency + 1, for this thread and the "concurrency" number of
	// goroutines that move money
	db.SetMaxOpenConns(*concurrency + 1)

	if _, err = db.Exec("CREATE TABLE IF NOT EXISTS accounts (id BIGINT PRIMARY KEY, balance BIGINT NOT NULL)"); err != nil {
		log.Fatal(err)
	}

	if _, err = db.Exec("TRUNCATE TABLE accounts"); err != nil {
		log.Fatal(err)
	}

	for i := 0; i < *numAccounts; i++ {
		if _, err = db.Exec("INSERT INTO accounts (id, balance) VALUES ($1, $2)", i, 1000); err != nil {
			log.Fatal(err)
		}
	}

	verifyBank(db)

	lastNow := time.Now()
	readings := make(chan measurement, 10000)

	for i := 0; i < *concurrency; i++ {
		go moveMoney(db, readings)
	}

	for range time.NewTicker(*balanceCheckInterval).C {
		now := time.Now()
		elapsed := time.Since(lastNow)
		lastNow = now
		transfers := len(readings)
		log.Infof("%d transfers were executed at %.1f/second.", transfers, float64(transfers)/elapsed.Seconds())
		if transfers > 0 {
			var aggr measurement
			for i := 0; i < transfers; i++ {
				reading := <-readings
				aggr.read += reading.read
				aggr.write += reading.write
				aggr.total += reading.total
			}
			d := time.Duration(transfers)
			log.Infof("read time: %v, write time: %v, txn time: %v", aggr.read/d, aggr.write/d, aggr.total/d)
		}
		verifyBank(db)
	}
}
Ejemplo n.º 9
0
func init() {
	security.SetReadFileFn(securitytest.Asset)
	tracing.SetTestTracing()
}
Ejemplo n.º 10
0
func main() {
	flag.Parse()
	var db *sql.DB
	var err error
	var url string
	if *usePostgres {
		if db, err = sql.Open("postgres", "dbname=postgres sslmode=disable"); err != nil {
			log.Fatal(err)
		}
	} else {
		security.SetReadFileFn(securitytest.Asset)
		serv := server.StartTestServer(nil)
		defer serv.Stop()
		url = "https://root@" + serv.ServingAddr() + "?certs=test_certs"
		if db, err = sql.Open("cockroach", url); err != nil {
			log.Fatal(err)
		}
	}
	if _, err := db.Exec("CREATE DATABASE bank"); err != nil {
		if pqErr, ok := err.(*pq.Error); *usePostgres && (!ok || pqErr.Code.Name() != "duplicate_database") {
			log.Fatal(err)
		}
	}
	db.Close()

	// Open db client with database settings.
	if *usePostgres {
		if db, err = sql.Open("postgres", "dbname=bank sslmode=disable"); err != nil {
			log.Fatal(err)
		}
	} else {
		if db, err = sql.Open("cockroach", url+"&database=bank"); err != nil {
			log.Fatal(err)
		}
	}

	// concurrency + 1, for this thread and the "concurrency" number of
	// goroutines that move money
	db.SetMaxOpenConns(*concurrency + 1)

	if _, err = db.Exec("CREATE TABLE IF NOT EXISTS accounts (id BIGINT PRIMARY KEY, balance BIGINT NOT NULL)"); err != nil {
		log.Fatal(err)
	}

	if _, err = db.Exec("TRUNCATE TABLE accounts"); err != nil {
		log.Fatal(err)
	}

	for i := 0; i < *numAccounts; i++ {
		if _, err = db.Exec("INSERT INTO accounts (id, balance) VALUES ($1, $2)", i, 0); err != nil {
			log.Fatal(err)
		}
	}

	verifyBank(db)

	var lastNumTransfers uint64
	lastNow := time.Now()

	for i := 0; i < *concurrency; i++ {
		go moveMoney(db)
	}

	for range time.NewTicker(*balanceCheckInterval).C {
		now := time.Now()
		elapsed := time.Since(lastNow)
		numTransfers := atomic.LoadUint64(&numTransfers)
		log.Infof("%d transfers were executed at %.1f/second.", (numTransfers - lastNumTransfers), float64(numTransfers-lastNumTransfers)/elapsed.Seconds())
		verifyBank(db)
		lastNumTransfers = numTransfers
		lastNow = now
	}
}
Ejemplo n.º 11
0
func TestMain(m *testing.M) {
	security.SetReadFileFn(securitytest.Asset)
	serverutils.InitTestServerFactory(server.TestServerFactory)
	os.Exit(m.Run())
}
Ejemplo n.º 12
0
// ResetTest sets up the test environment. In particular, it embeds the
// EmbeddedCertsDir folder and makes the tls package load from there.
func ResetTest() {
	security.SetReadFileFn(securitytest.Asset)
}
Ejemplo n.º 13
0
func main() {
	flag.Usage = usage
	flag.Parse()

	if flag.NArg() != 1 {
		usage()
		os.Exit(2)
	}
	mountpoint := flag.Arg(0)

	security.SetReadFileFn(securitytest.Asset)
	serv := server.StartTestServer(nil)
	defer serv.Stop()
	url := "https://root@" + serv.ServingAddr() + "?certs=test_certs"

	// Open DB connection first.
	db, err := sql.Open("cockroach", url)
	if err != nil {
		log.Fatal(err)
	}
	defer func() { _ = db.Close() }()

	if err := initSchema(db); err != nil {
		log.Fatal(err)
	}

	cfs := CFS{db}
	{
		// For testing only.
		if err := cfs.create(rootNodeID, "myfile", cfs.newFileNode()); err != nil {
			log.Fatal(err)
		}
		if err := cfs.create(rootNodeID, "mydir", cfs.newDirNode()); err != nil {
			log.Fatal(err)
		}
		results, err := cfs.list(0)
		if err != nil {
			log.Fatal(err)
		}
		log.Print(results)
	}

	// Mount filesystem.
	c, err := fuse.Mount(
		mountpoint,
		fuse.FSName("CockroachFS"),
		fuse.Subtype("CockroachFS"),
		fuse.LocalVolume(),
		fuse.VolumeName(""),
	)
	if err != nil {
		log.Fatal(err)
	}
	defer func() {
		_ = c.Close()
	}()

	go func() {
		sig := make(chan os.Signal, 1)
		signal.Notify(sig, os.Interrupt)
		for range sig {
			if err := fuse.Unmount(mountpoint); err != nil {
				log.Printf("Signal received, but could not unmount: %s", err)
			} else {
				break
			}
		}
	}()

	// Serve root.
	err = fs.Serve(c, cfs)
	if err != nil {
		log.Fatal(err)
	}

	// check if the mount process has an error to report
	<-c.Ready
	if err := c.MountError; err != nil {
		log.Fatal(err)
	}
}