本文整理匯總了Golang中github.com/cloudfoundry/yagnats/fakeyagnats.FakeYagnats.Subscriptions方法的典型用法代碼示例。如果您正苦於以下問題:Golang FakeYagnats.Subscriptions方法的具體用法?Golang FakeYagnats.Subscriptions怎麽用?Golang FakeYagnats.Subscriptions使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/cloudfoundry/yagnats/fakeyagnats.FakeYagnats
的用法示例。
在下文中一共展示了FakeYagnats.Subscriptions方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: init
func init() {
Describe("natsHandler", func() {
var (
settingsService *fakesettings.FakeSettingsService
client *fakeyagnats.FakeYagnats
logger boshlog.Logger
handler boshhandler.Handler
)
BeforeEach(func() {
settingsService = &fakesettings.FakeSettingsService{
Settings: boshsettings.Settings{
AgentID: "my-agent-id",
Mbus: "nats://fake-username:[email protected]:1234",
},
}
logger = boshlog.NewLogger(boshlog.LevelNone)
client = fakeyagnats.New()
handler = NewNatsHandler(settingsService, client, logger)
})
Describe("Start", func() {
It("starts", func() {
var receivedRequest boshhandler.Request
handler.Start(func(req boshhandler.Request) (resp boshhandler.Response) {
receivedRequest = req
return boshhandler.NewValueResponse("expected value")
})
defer handler.Stop()
Expect(client.ConnectedConnectionProvider()).ToNot(BeNil())
Expect(client.SubscriptionCount()).To(Equal(1))
subscriptions := client.Subscriptions("agent.my-agent-id")
Expect(len(subscriptions)).To(Equal(1))
expectedPayload := []byte(`{"method":"ping","arguments":["foo","bar"], "reply_to": "reply to me!"}`)
subscription := subscriptions[0]
subscription.Callback(&yagnats.Message{
Subject: "agent.my-agent-id",
Payload: expectedPayload,
})
Expect(receivedRequest).To(Equal(boshhandler.Request{
ReplyTo: "reply to me!",
Method: "ping",
Payload: expectedPayload,
}))
Expect(client.PublishedMessageCount()).To(Equal(1))
messages := client.PublishedMessages("reply to me!")
Expect(len(messages)).To(Equal(1))
Expect(messages[0].Payload).To(Equal([]byte(`{"value":"expected value"}`)))
})
It("does not respond if the response is nil", func() {
err := handler.Start(func(req boshhandler.Request) (resp boshhandler.Response) {
return nil
})
Expect(err).ToNot(HaveOccurred())
defer handler.Stop()
subscription := client.Subscriptions("agent.my-agent-id")[0]
subscription.Callback(&yagnats.Message{
Subject: "agent.my-agent-id",
Payload: []byte(`{"method":"ping","arguments":["foo","bar"], "reply_to": "reply to me!"}`),
})
Expect(client.PublishedMessageCount()).To(Equal(0))
})
It("responds with an error if the response is bigger than 1MB", func() {
err := handler.Start(func(req boshhandler.Request) (resp boshhandler.Response) {
// gets inflated by json.Marshal when enveloping
size := 0
switch req.Method {
case "small":
size = 1024*1024 - 12
case "big":
size = 1024 * 1024
default:
panic("unknown request size")
}
chars := make([]byte, size)
for i := range chars {
chars[i] = 'A'
}
return boshhandler.NewValueResponse(string(chars))
})
Expect(err).ToNot(HaveOccurred())
defer handler.Stop()
subscription := client.Subscriptions("agent.my-agent-id")[0]
subscription.Callback(&yagnats.Message{
Subject: "agent.my-agent-id",
Payload: []byte(`{"method":"small","arguments":[], "reply_to": "fake-reply-to"}`),
})
//.........這裏部分代碼省略.........