This is the code to check gocql session is closed or not. I am using gocql.Session.Closed but getting sigsegv error on closed part when cassandra.service is down. The expected output should be to give status is down although the code is getting broken.
func CheckCassandraHealthCheck(cassandraCluster *gocql.Session, Host string, Port int) map[string]interface{} {
response := make(map[string]interface{})
response1 := make(map[string]interface{})
response[constant.TYPE] = constant.CASSANDRATYPE
response1[constant.HOST] = Host
response1[constant.PORT] = Port
log.Println("CASSANDRA Host is ", response1[constant.HOST])
log.Println("CASSANDRA Port is ", response1[constant.PORT])
if !cassandraCluster.Closed() {
response[constant.STATUS] = constant.UP
response[constant.DETAILS] = response1
log.Println("Status of ", constant.CASSANDRASTRING, " is ", response[constant.STATUS])
return response
}
response[constant.DETAILS] = response1
response[constant.STATUS] = constant.DOWN
log.Println("Status of ", constant.CASSANDRASTRING, " is ", response[constant.STATUS])
return response
}
func CreateCassandraCluster(host string) (*gocql.Session, error) {
cluster := gocql.NewCluster(host)
cluster.ConnectTimeout = time.Second * 10
cluster.DisableInitialHostLookup = true
session, err := cluster.CreateSession()
if err != nil {
return session, err
}
return session, nil
}
getting error:-
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x378 pc=0x73bb59]
goroutine 43 [running]:
testing.tRunner.func1.2({0xac09c0, 0x1105680})
/usr/local/go/src/testing/testing.go:1389 +0x24e
testing.tRunner.func1()
/usr/local/go/src/testing/testing.go:1392 +0x39f
panic({0xac09c0, 0x1105680})
/usr/local/go/src/runtime/panic.go:838 +0x207
github.com/gocql/gocql.(*Session).Closed(0xc0000d0310?)
/home/srijan/go/pkg/mod/github.com/gocql/gocql@v1.1.0/session.go:483 +0x19
healthcheck/healthcheck/util.CheckCassandraHealthCheck({0xb6e8c0?, 0x0}, {0xb827e2, 0xe}, 0xd4c6d1?)
/home/srijan/go-workspace/smt-healthcheck/healthcheck/util/CassandraHealthCheck.go:22 +0x29f
this is my test file
func TestCassandraConnection(t *testing.T) {
expectedOutCome := make(map[string]interface{})
detailsOutcome := make(map[string]interface{})
expectedOutCome[constant.TYPE] = constant.CASSANDRATYPE
detailsOutcome[constant.HOST] = "localhost"
detailsOutcome[constant.PORT] = 9042
expectedOutCome[constant.DETAILS] = detailsOutcome
expectedOutCome[constant.STATUS] = constant.UP
session, err := util.CreateCassandraCluster("localhost")
if err != nil {
t.Errorf("Error connecting to cassandra db %+v", err)
}
data := models.IOModel{Components: models.Components{Type: 2, Host: "localhost", Port: 9042, Pool: session, Name: "Stage Cassandra"}, Expected: expectedOutCome}
result := util.CheckCassandraHealthCheck(data.Components.Pool, data.Components.Host, data.Components.Port)
if !reflect.DeepEqual(result, data.Expected) {
t.Errorf("Cassandra HealtCheck failed expected %+v and getting result %+v", expectedOutCome, result)
}
session.Close()
t.Logf("Result Obtained is %+v", result)
}
How to check session is closed or not if session.Closed() is giving sigsegv error?