func TestSubscribePing(t *testing.T) {
	timeout := 3
	handler_opts := NewHandlerOpts().SetTimeout(timeout)
	sub, err := engine.ReplyTo(PingTopic, func(req *Request, resp *Message) {
		var x string
		req.Data(&x)
		ui.Message("Topic %v Received %v", PingTopic, x)
		ui.Message("Topic %v Sending %v", PingTopic, PingResponse)
		resp.SetData(PingResponse)
	}, handler_opts)

	if err != nil {
		t.Error("Error Subscribing", PingTopic, err.Error())
		return
	}

	actualTimeout := sub.GetOpts().GetTimeout()

	if actualTimeout != timeout {
		t.Error("Handler should have timeout of", timeout, "seconds. Found", actualTimeout)
	}

	if has, _ := isReplySubscribed(PingTopic); !has {
		t.Error("Topic", PingTopic, "should have been subscribed")
	}
}
Example #2
0
/*
Parse a gilmour *Message and for subscribers of this topic do the following:
	* If subscriber is one shot, unsubscribe the subscriber to prevent subscribers from re-execution.
	* If subscriber belongs to a group, try acquiring a lock via backend to ensure group exclusivity.
	* If all conditions suffice spin up a new goroutine for each subscription.
*/
func (g *Gilmour) processMessage(msg *proto.Packet) {
	subs, ok := g.getSubscribers(msg.GetPattern())
	if !ok || len(subs) == 0 {
		ui.Warn("*Message cannot be processed. No subs found for key %v", msg.GetPattern())
		return
	}

	m, err := parseMessage(msg.GetData())
	if err != nil {
		ui.Alert(err.Error())
		return
	}

	for _, s := range subs {

		opts := s.GetOpts()
		if opts != nil && opts.isOneShot() {
			ui.Message("Unsubscribing one shot response topic %v", msg.GetTopic())
			go g.UnsubscribeReply(msg.GetPattern(), s)
		}

		if opts.GetGroup() != "" && opts.shouldSendResponse() {
			if !g.backend.AcquireGroupLock(opts.GetGroup(), m.GetSender()) {
				ui.Warn(
					"Unable to acquire Lock. Topic %v Group %v Sender %v",
					msg.GetTopic(), opts.GetGroup(), m.GetSender(),
				)
				continue
			}
		}

		go g.handleRequest(s, msg.GetTopic(), m)
	}
}