1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-09 11:51:02 -05:00
v2fly/app/pubsub/internal/pubsub_test.go

39 lines
989 B
Go
Raw Normal View History

2016-01-31 11:01:28 -05:00
package internal_test
2015-12-11 09:56:10 -05:00
import (
"testing"
"time"
2016-01-31 11:01:28 -05:00
"github.com/v2ray/v2ray-core/app/pubsub"
. "github.com/v2ray/v2ray-core/app/pubsub/internal"
2015-12-11 09:56:10 -05:00
apptesting "github.com/v2ray/v2ray-core/app/testing"
v2testing "github.com/v2ray/v2ray-core/testing"
"github.com/v2ray/v2ray-core/testing/assert"
)
func TestPubsub(t *testing.T) {
v2testing.Current(t)
2016-01-31 11:01:28 -05:00
messages := make(map[string]pubsub.PubsubMessage)
2015-12-11 09:56:10 -05:00
2016-01-31 11:01:28 -05:00
ps := New()
ps.Subscribe(&apptesting.Context{}, "t1", func(message pubsub.PubsubMessage) {
2015-12-11 09:56:10 -05:00
messages["t1"] = message
})
2016-01-31 11:01:28 -05:00
ps.Subscribe(&apptesting.Context{}, "t2", func(message pubsub.PubsubMessage) {
2015-12-11 09:56:10 -05:00
messages["t2"] = message
})
2016-01-31 11:01:28 -05:00
message := pubsub.PubsubMessage([]byte("This is a pubsub message."))
ps.Publish(&apptesting.Context{}, "t2", message)
2015-12-11 09:56:10 -05:00
<-time.Tick(time.Second)
_, found := messages["t1"]
assert.Bool(found).IsFalse()
actualMessage, found := messages["t2"]
assert.Bool(found).IsTrue()
assert.StringLiteral(string(actualMessage)).Equals(string(message))
}