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-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2/gobottest"
|
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 {
|
2016-08-30 19:27:50 +08:00
|
|
|
t.Errorf("Could not add event to list of Event names")
|
2014-11-30 16:19:53 +08:00
|
|
|
}
|
2017-02-25 20:55:41 +08:00
|
|
|
gobottest.Assert(t, e.Event("test"), "test")
|
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 {
|
|
|
|
t.Errorf("Could not add delete event from list of Event names")
|
|
|
|
}
|
|
|
|
}
|
2016-09-12 00:27:16 +08:00
|
|
|
|
|
|
|
func TestEventerOn(t *testing.T) {
|
|
|
|
e := NewEventer()
|
|
|
|
e.AddEvent("test")
|
|
|
|
|
|
|
|
sem := make(chan bool)
|
|
|
|
e.On("test", func(data interface{}) {
|
|
|
|
sem <- true
|
|
|
|
})
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
e.Publish("test", true)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-sem:
|
|
|
|
case <-time.After(10 * time.Millisecond):
|
|
|
|
t.Errorf("On was not called")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEventerOnce(t *testing.T) {
|
|
|
|
e := NewEventer()
|
|
|
|
e.AddEvent("test")
|
|
|
|
|
|
|
|
sem := make(chan bool)
|
|
|
|
e.Once("test", func(data interface{}) {
|
|
|
|
sem <- true
|
|
|
|
})
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
e.Publish("test", true)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-sem:
|
|
|
|
case <-time.After(10 * time.Millisecond):
|
|
|
|
t.Errorf("Once was not called")
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
e.Publish("test", true)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-sem:
|
|
|
|
t.Errorf("Once was called twice")
|
|
|
|
case <-time.After(10 * time.Millisecond):
|
|
|
|
}
|
|
|
|
}
|