Пример #1
0
func (s *RethinkSuite) TestConcurrentSelectManyRows(c *test.C) {
	if testing.Short() {
		c.Skip("Skipping long test")
	}

	// Ensure table + database exist
	DBCreate("test").RunWrite(session)
	DB("test").TableCreate("TestMany").RunWrite(session)
	DB("test").Table("TestMany").Delete().RunWrite(session)

	// Insert rows
	for i := 0; i < 100; i++ {
		DB("test").Table("TestMany").Insert(map[string]interface{}{
			"i": i,
		}).Exec(session)
	}

	// Test queries concurrently
	attempts := 10
	waitChannel := make(chan error, attempts)

	for i := 0; i < attempts; i++ {
		go func(i int, c chan error) {
			res, err := DB("test").Table("TestMany").Run(session)
			if err != nil {
				c <- err
				return
			}

			var response []map[string]interface{}
			err = res.All(&response)
			if err != nil {
				c <- err
				return
			}

			if len(response) != 100 {
				c <- fmt.Errorf("expected response length 100, received %d", len(response))
				return
			}

			res.Close()

			c <- nil
		}(i, waitChannel)
	}

	for i := 0; i < attempts; i++ {
		ret := <-waitChannel
		if ret != nil {
			c.Fatalf("non-nil error returned (%s)", ret)
		}
	}
}
Пример #2
0
func (s *RethinkSuite) TestControlHttp(c *test.C) {
	if testing.Short() {
		c.Skip("-short set")
	}

	var response map[string]interface{}
	query := HTTP("httpbin.org/get?data=1")
	res, err := query.Run(session)
	c.Assert(err, test.IsNil)

	err = res.One(&response)

	c.Assert(err, test.IsNil)
	c.Assert(response["args"], jsonEquals, map[string]interface{}{
		"data": "1",
	})
}
Пример #3
0
func (s *RethinkSuite) TestConcurrentSelectManyWorkers(c *test.C) {
	if testing.Short() {
		c.Skip("Skipping long test")
	}

	rand.Seed(time.Now().UnixNano())
	sess, _ := Connect(ConnectOpts{
		Address: url,
		AuthKey: authKey,
		MaxOpen: 200,
		MaxIdle: 200,
	})

	// Ensure table + database exist
	DBCreate("test").RunWrite(sess)
	DB("test").TableDrop("TestConcurrent").RunWrite(sess)
	DB("test").TableCreate("TestConcurrent").RunWrite(sess)
	DB("test").TableDrop("TestConcurrent2").RunWrite(sess)
	DB("test").TableCreate("TestConcurrent2").RunWrite(sess)

	// Insert rows
	for j := 0; j < 200; j++ {
		DB("test").Table("TestConcurrent").Insert(map[string]interface{}{
			"id": j,
			"i":  j,
		}).Exec(sess)
		DB("test").Table("TestConcurrent2").Insert(map[string]interface{}{
			"j": j,
			"k": j * 2,
		}).Exec(sess)
	}

	// Test queries concurrently
	numQueries := 1000
	numWorkers := 10
	queryChan := make(chan int)
	doneChan := make(chan error)

	// Start workers
	for i := 0; i < numWorkers; i++ {
		go func() {
			for _ = range queryChan {
				res, err := DB("test").Table("TestConcurrent2").EqJoin("j", DB("test").Table("TestConcurrent")).Zip().Run(sess)
				if err != nil {
					doneChan <- err
					return
				}

				var response []map[string]interface{}
				err = res.All(&response)
				if err != nil {
					doneChan <- err
					return
				}
				if err := res.Close(); err != nil {
					doneChan <- err
					return
				}

				if len(response) != 200 {
					doneChan <- fmt.Errorf("expected response length 200, received %d", len(response))
					return
				}

				res, err = DB("test").Table("TestConcurrent").Get(response[rand.Intn(len(response))]["id"]).Run(sess)
				if err != nil {
					doneChan <- err
					return
				}

				err = res.All(&response)
				if err != nil {
					doneChan <- err
					return
				}
				if err := res.Close(); err != nil {
					doneChan <- err
					return
				}

				if len(response) != 1 {
					doneChan <- fmt.Errorf("expected response length 1, received %d", len(response))
					return
				}

				doneChan <- nil
			}
		}()
	}

	go func() {
		for i := 0; i < numQueries; i++ {
			queryChan <- i
		}
	}()

	for i := 0; i < numQueries; i++ {
		ret := <-doneChan
		if ret != nil {
			c.Fatalf("non-nil error returned (%s)", ret)
		}
	}
}