hybridgroup.gobot/eventer_test.go

79 lines
1.5 KiB
Go
Raw Permalink Normal View History

2014-11-30 16:19:53 +08:00
package gobot
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
2014-11-30 16:19:53 +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 {
require.Fail(t, "Could not add event to list of Event names")
2014-11-30 16:19:53 +08:00
}
assert.Equal(t, "test", e.Event("test"))
assert.Equal(t, "", e.Event("unknown"))
2014-11-30 16:19:53 +08:00
}
func TestEventerDeleteEvent(t *testing.T) {
e := NewEventer()
e.AddEvent("test1")
e.DeleteEvent("test1")
if _, ok := e.Events()["test1"]; ok {
require.Fail(t, "Could not add delete event from list of Event names")
}
}
func TestEventerOn(t *testing.T) {
e := NewEventer()
sem := make(chan bool)
_ = e.On("test", func(data interface{}) {
sem <- true
})
// wait some time to ensure the eventer go routine is working
time.Sleep(10 * time.Millisecond)
e.Publish("test", true)
select {
case <-sem:
case <-time.After(10 * time.Millisecond):
require.Fail(t, "On was not called")
}
}
func TestEventerOnce(t *testing.T) {
e := NewEventer()
sem := make(chan bool)
_ = e.Once("test", func(data interface{}) {
sem <- true
})
// wait some time to ensure the eventer go routine is working
time.Sleep(10 * time.Millisecond)
e.Publish("test", true)
select {
case <-sem:
case <-time.After(10 * time.Millisecond):
require.Fail(t, "Once was not called")
}
e.Publish("test", true)
select {
case <-sem:
require.Fail(t, "Once was called twice")
case <-time.After(10 * time.Millisecond):
}
}