2014-11-30 16:19:53 +08:00
|
|
|
package gobot
|
|
|
|
|
2016-02-22 13:21:24 +08:00
|
|
|
import (
|
|
|
|
"testing"
|
2016-09-12 00:27:16 +08:00
|
|
|
"time"
|
2017-02-25 20:55:41 +08:00
|
|
|
|
2023-10-20 16:27:09 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
2024-02-13 17:33:46 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-02-22 13:21:24 +08:00
|
|
|
)
|
2014-11-30 16:19:53 +08:00
|
|
|
|
2016-09-11 19:27:43 +08:00
|
|
|
func TestEventerAddEvent(t *testing.T) {
|
2014-11-30 16:19:53 +08:00
|
|
|
e := NewEventer()
|
|
|
|
e.AddEvent("test")
|
|
|
|
|
|
|
|
if _, ok := e.Events()["test"]; !ok {
|
2024-02-13 17:33:46 +08:00
|
|
|
require.Fail(t, "Could not add event to list of Event names")
|
2014-11-30 16:19:53 +08:00
|
|
|
}
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, "test", e.Event("test"))
|
2024-02-13 17:33:46 +08:00
|
|
|
assert.Equal(t, "", e.Event("unknown"))
|
2014-11-30 16:19:53 +08:00
|
|
|
}
|
2016-09-11 19:27:43 +08:00
|
|
|
|
|
|
|
func TestEventerDeleteEvent(t *testing.T) {
|
|
|
|
e := NewEventer()
|
|
|
|
e.AddEvent("test1")
|
|
|
|
e.DeleteEvent("test1")
|
|
|
|
|
|
|
|
if _, ok := e.Events()["test1"]; ok {
|
2024-02-13 17:33:46 +08:00
|
|
|
require.Fail(t, "Could not add delete event from list of Event names")
|
2016-09-11 19:27:43 +08:00
|
|
|
}
|
|
|
|
}
|
2016-09-12 00:27:16 +08:00
|
|
|
|
|
|
|
func TestEventerOn(t *testing.T) {
|
|
|
|
e := NewEventer()
|
|
|
|
|
|
|
|
sem := make(chan bool)
|
2023-06-13 01:51:25 +08:00
|
|
|
_ = e.On("test", func(data interface{}) {
|
2016-09-12 00:27:16 +08:00
|
|
|
sem <- true
|
|
|
|
})
|
|
|
|
|
2024-02-13 17:33:46 +08:00
|
|
|
// wait some time to ensure the eventer go routine is working
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
|
|
|
|
e.Publish("test", true)
|
2016-09-12 00:27:16 +08:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-sem:
|
|
|
|
case <-time.After(10 * time.Millisecond):
|
2024-02-13 17:33:46 +08:00
|
|
|
require.Fail(t, "On was not called")
|
2016-09-12 00:27:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEventerOnce(t *testing.T) {
|
|
|
|
e := NewEventer()
|
|
|
|
|
|
|
|
sem := make(chan bool)
|
2023-06-13 01:51:25 +08:00
|
|
|
_ = e.Once("test", func(data interface{}) {
|
2016-09-12 00:27:16 +08:00
|
|
|
sem <- true
|
|
|
|
})
|
|
|
|
|
2024-02-13 17:33:46 +08:00
|
|
|
// wait some time to ensure the eventer go routine is working
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
|
|
|
|
e.Publish("test", true)
|
2016-09-12 00:27:16 +08:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-sem:
|
|
|
|
case <-time.After(10 * time.Millisecond):
|
2024-02-13 17:33:46 +08:00
|
|
|
require.Fail(t, "Once was not called")
|
2016-09-12 00:27:16 +08:00
|
|
|
}
|
|
|
|
|
2024-02-13 17:33:46 +08:00
|
|
|
e.Publish("test", true)
|
2016-09-12 00:27:16 +08:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-sem:
|
2024-02-13 17:33:46 +08:00
|
|
|
require.Fail(t, "Once was called twice")
|
2016-09-12 00:27:16 +08:00
|
|
|
case <-time.After(10 * time.Millisecond):
|
|
|
|
}
|
|
|
|
}
|