2018-12-04 05:57:11 +00:00
|
|
|
// Statping
|
2018-11-01 14:37:20 +00:00
|
|
|
// Copyright (C) 2018. Hunter Long and the project contributors
|
|
|
|
// Written by Hunter Long <info@socialeck.com> and the project contributors
|
|
|
|
//
|
2018-12-04 04:17:29 +00:00
|
|
|
// https://github.com/hunterlong/statping
|
2018-11-01 14:37:20 +00:00
|
|
|
//
|
|
|
|
// The licenses for most software and other practical works are designed
|
|
|
|
// to take away your freedom to share and change the works. By contrast,
|
|
|
|
// the GNU General Public License is intended to guarantee your freedom to
|
|
|
|
// share and change all versions of a program--to make sure it remains free
|
|
|
|
// software for all its users.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package notifiers
|
|
|
|
|
|
|
|
import (
|
2018-12-04 04:17:29 +00:00
|
|
|
"github.com/hunterlong/statping/core/notifier"
|
2018-11-01 14:37:20 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-12-14 09:19:55 +00:00
|
|
|
MOBILE_ID string
|
|
|
|
MOBILE_NUMBER string
|
2018-11-01 14:37:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
MOBILE_ID = os.Getenv("MOBILE_ID")
|
2018-12-14 09:19:55 +00:00
|
|
|
MOBILE_NUMBER = os.Getenv("MOBILE_NUMBER")
|
2018-11-01 14:37:20 +00:00
|
|
|
mobile.Var1 = MOBILE_ID
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMobileNotifier(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
mobile.Var1 = MOBILE_ID
|
2018-12-14 09:19:55 +00:00
|
|
|
mobile.Var2 = os.Getenv("MOBILE_NUMBER")
|
2018-11-01 14:37:20 +00:00
|
|
|
if MOBILE_ID == "" {
|
|
|
|
t.Log("mobile notifier testing skipped, missing MOBILE_ID environment variable")
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
|
|
|
currentCount = CountNotifiers()
|
|
|
|
|
|
|
|
t.Run("Load mobile", func(t *testing.T) {
|
|
|
|
mobile.Var1 = MOBILE_ID
|
2018-12-14 09:19:55 +00:00
|
|
|
mobile.Var2 = MOBILE_NUMBER
|
2018-11-01 14:37:20 +00:00
|
|
|
mobile.Delay = time.Duration(100 * time.Millisecond)
|
2018-12-14 09:57:36 +00:00
|
|
|
mobile.Limits = 10
|
2018-11-01 14:37:20 +00:00
|
|
|
err := notifier.AddNotifier(mobile)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, "Hunter Long", mobile.Author)
|
|
|
|
assert.Equal(t, MOBILE_ID, mobile.Var1)
|
2018-12-14 09:19:55 +00:00
|
|
|
assert.Equal(t, MOBILE_NUMBER, mobile.Var2)
|
2018-11-01 14:37:20 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Load mobile Notifier", func(t *testing.T) {
|
|
|
|
notifier.Load()
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("mobile Notifier Tester", func(t *testing.T) {
|
|
|
|
assert.True(t, mobile.CanTest())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("mobile Within Limits", func(t *testing.T) {
|
|
|
|
ok, err := mobile.WithinLimits()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.True(t, ok)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("mobile OnFailure", func(t *testing.T) {
|
|
|
|
mobile.OnFailure(TestService, TestFailure)
|
|
|
|
assert.Equal(t, 1, len(mobile.Queue))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("mobile OnFailure multiple times", func(t *testing.T) {
|
2019-09-03 21:10:13 +00:00
|
|
|
for i := 0; i <= 5; i++ {
|
2018-11-01 14:37:20 +00:00
|
|
|
mobile.OnFailure(TestService, TestFailure)
|
|
|
|
}
|
2019-09-03 21:10:13 +00:00
|
|
|
assert.Equal(t, 7, len(mobile.Queue))
|
2018-11-01 14:37:20 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("mobile OnSuccess", func(t *testing.T) {
|
|
|
|
mobile.OnSuccess(TestService)
|
2019-09-03 21:10:13 +00:00
|
|
|
assert.Equal(t, 7, len(mobile.Queue))
|
2018-11-01 14:37:20 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("mobile OnSuccess Again", func(t *testing.T) {
|
2018-12-14 17:34:42 +00:00
|
|
|
t.SkipNow()
|
2019-08-15 23:26:36 +00:00
|
|
|
assert.True(t, TestService.Online)
|
2018-11-01 14:37:20 +00:00
|
|
|
mobile.OnSuccess(TestService)
|
|
|
|
assert.Equal(t, 1, len(mobile.Queue))
|
|
|
|
go notifier.Queue(mobile)
|
2018-12-14 10:13:35 +00:00
|
|
|
time.Sleep(20 * time.Second)
|
2018-12-14 17:34:42 +00:00
|
|
|
assert.Equal(t, 1, len(mobile.Queue))
|
2018-11-01 14:37:20 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("mobile Within Limits again", func(t *testing.T) {
|
|
|
|
ok, err := mobile.WithinLimits()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.True(t, ok)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("mobile Test", func(t *testing.T) {
|
2019-02-05 00:12:35 +00:00
|
|
|
t.SkipNow()
|
2018-11-01 14:37:20 +00:00
|
|
|
err := mobile.OnTest()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("mobile Queue", func(t *testing.T) {
|
2018-12-14 22:22:18 +00:00
|
|
|
t.SkipNow()
|
2018-11-01 14:37:20 +00:00
|
|
|
go notifier.Queue(mobile)
|
2018-12-14 10:13:35 +00:00
|
|
|
time.Sleep(15 * time.Second)
|
2018-11-01 14:37:20 +00:00
|
|
|
assert.Equal(t, MOBILE_ID, mobile.Var1)
|
|
|
|
assert.Equal(t, 0, len(mobile.Queue))
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|