';进行测试';返回错误“;gocql:超时期间未收到来自cassandra的响应。”;
所以我想在本地机器上测试一个与Cassandra交互的API。在我的';进行测试';返回错误“;gocql:超时期间未收到来自cassandra的响应。”;,go,cassandra,connection-timeout,gocql,Go,Cassandra,Connection Timeout,Gocql,所以我想在本地机器上测试一个与Cassandra交互的API。在我的func TestMain(m*testing.m)函数中,我想在运行测试之前清除表。TestMain函数如下所示 func TestMain(m *testing.M) { keyspace = "staging" cassandra.SetKeyspace(keyspace) client = http.DefaultClient // Ensure all tables are empt
func TestMain(m*testing.m)
函数中,我想在运行测试之前清除表。TestMain
函数如下所示
func TestMain(m *testing.M) {
keyspace = "staging"
cassandra.SetKeyspace(keyspace)
client = http.DefaultClient
// Ensure all tables are empty before tests run
err := ClearAllTables()
if err != nil {
logrus.Errorf("Failed to clear all tables: %v.", err)
os.Exit(1)
}
// Run tests
statusCode := m.Run()
os.Exit(statusCode)
}
func ClearAllTables() (err error) {
// Create a DB session
session := cassandra.CreateSession()
defer session.Close()
// Get names of all existing tables
tableNameList := []string{"cliques", "users"}
// Remove all rows from each table
var count int
for _, tableName := range tableNameList {
if err := session.Query(`TRUNCATE TABLE ` + tableName).Exec(); err != nil {
return err
}
}
return nil
}
ClearAllTables
函数如下所示
func TestMain(m *testing.M) {
keyspace = "staging"
cassandra.SetKeyspace(keyspace)
client = http.DefaultClient
// Ensure all tables are empty before tests run
err := ClearAllTables()
if err != nil {
logrus.Errorf("Failed to clear all tables: %v.", err)
os.Exit(1)
}
// Run tests
statusCode := m.Run()
os.Exit(statusCode)
}
func ClearAllTables() (err error) {
// Create a DB session
session := cassandra.CreateSession()
defer session.Close()
// Get names of all existing tables
tableNameList := []string{"cliques", "users"}
// Remove all rows from each table
var count int
for _, tableName := range tableNameList {
if err := session.Query(`TRUNCATE TABLE ` + tableName).Exec(); err != nil {
return err
}
}
return nil
}
由于某种原因,当我试图截断表时,Cassandra超时,我得到了错误
level=error msg="Failed to clear all tables: gocql: no response received from cassandra within timeout period."
这似乎只有在我测试程序时才会发生。我在main函数中编写了一段代码,效果很好
func main () {
session := cassandra.CreateSession()
defer session.Close()
if err := session.Query(`TRUNCATE TABLE cliques`).Exec(); err != nil {
return
}
fmt.Println("Table truncated") //works
}
我还编写了一段代码,从数据库中返回一些行,在主函数中也可以正常工作
下面是我如何创建我的cassandra会话
// CreateSession connect to a cassandra instance
func CreateSession() *gocql.Session {
// Connect to the cluster
cluster := gocql.NewCluster("127.0.0.1")
cluster.Keyspace = "staging"
cluster.ProtoVersion = 4
cluster.CQLVersion = "3.0.0"
cluster.Consistency = gocql.One
session, err := cluster.CreateSession()
if err != nil {
logrus.Errorf("Failed to connect to Cassandra: %v.", err)
}
return session
}
我遗漏了任何东西,为什么Cassandra可以使用
go run main.go
而不能使用go test
?返回什么?您的代码在两种情况下都不相同。getAlltables只返回我所有表名的一部分。我对它进行了测试,效果良好。在第二种情况下,我只是硬编码了表名,有人会如何复制它?HavegetAllTableNames
只返回cliques
。这行吗?我已经删除了getAllTableNames
方法调用。问题可能来自运行在端口127.0.0.1上的Cassandra群集吗?当我调试测试时,它会在控制台上打印出来<代码>API服务器侦听地址:127.0.0.1:28132。一些内部代码已经使用cassandra端口了吗?