From 2c8068e04b67c70ebaed9e2fc5f51344601f3f83 Mon Sep 17 00:00:00 2001 From: Noah Hilverling Date: Tue, 3 Mar 2020 16:41:54 +0100 Subject: [PATCH] MySQL/Redis: Test fatal instead of panic --- connection/mysql_test.go | 13 +++++++++++-- connection/redis_test.go | 13 +++++++++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/connection/mysql_test.go b/connection/mysql_test.go index a5977fce..32bc7f7b 100644 --- a/connection/mysql_test.go +++ b/connection/mysql_test.go @@ -10,6 +10,7 @@ import ( "github.com/Icinga/icingadb/config/testbackends" "github.com/Icinga/icingadb/utils" "github.com/go-sql-driver/mysql" + "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" @@ -292,10 +293,18 @@ func TestGetConnectionCheckInterval(t *testing.T) { atomic.StoreUint32(dbw.ConnectionLostCounterAtomic, 11) assert.Equal(t, 60*time.Second, dbw.getConnectionCheckInterval()) - //Should panic, if not connected and counter > 13 + //Should exit, if not connected and counter > 13 dbw.CompareAndSetConnected(false) atomic.StoreUint32(dbw.ConnectionLostCounterAtomic, 14) - assert.Panics(t, func() { dbw.getConnectionCheckInterval() }, "Should panic") + + exited := false + defer func() { logrus.StandardLogger().ExitFunc = nil }() + logrus.StandardLogger().ExitFunc = func(i int) { + exited = true + } + + dbw.getConnectionCheckInterval() + assert.Equal(t, true, exited, "Should have exited") } func TestDBWrapper_SqlFetchAll(t *testing.T) { diff --git a/connection/redis_test.go b/connection/redis_test.go index 733bf49b..7d0d1095 100644 --- a/connection/redis_test.go +++ b/connection/redis_test.go @@ -5,6 +5,7 @@ package connection import ( "github.com/Icinga/icingadb/config/testbackends" "github.com/go-redis/redis" + "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" "sync" "sync/atomic" @@ -54,10 +55,18 @@ func TestRDBWrapper_GetConnectionCheckInterval(t *testing.T) { atomic.StoreUint32(rdbw.ConnectionLostCounterAtomic, 11) assert.Equal(t, 60*time.Second, rdbw.getConnectionCheckInterval()) - //Should panic, if not connected and counter > 13 + //Should exit, if not connected and counter > 13 rdbw.CompareAndSetConnected(false) atomic.StoreUint32(rdbw.ConnectionLostCounterAtomic, 14) - assert.Panics(t, func() { rdbw.getConnectionCheckInterval() }, "Should panic") + + exited := false + defer func() { logrus.StandardLogger().ExitFunc = nil }() + logrus.StandardLogger().ExitFunc = func(i int) { + exited = true + } + + rdbw.getConnectionCheckInterval() + assert.Equal(t, true, exited, "Should have exited") } func TestRDBWrapper_CheckConnection(t *testing.T) {