Example #1
0
func Test_MessageEntry_MessagesIsStored_And_GetsCorrectParameters(t *testing.T) {
	defer initCtrl(t)()
	a := assert.New(t)

	startTime := time.Now()

	msg := &guble.Message{Path: guble.Path("/topic1")}
	var storedMsg []byte
	var routedMsg *guble.Message
	routerMock := NewMockMessageSink(ctrl)
	messageEntry := NewMessageEntry(routerMock)
	messageStoreMock := NewMockMessageStore(ctrl)
	messageEntry.SetMessageStore(messageStoreMock)

	messageStoreMock.EXPECT().StoreTx("topic1", gomock.Any()).
		Do(func(topic string, callback func(msgId uint64) []byte) {
			storedMsg = callback(uint64(42))
		})

	routerMock.EXPECT().HandleMessage(gomock.Any()).Do(func(msg *guble.Message) {
		routedMsg = msg
		a.Equal(uint64(42), msg.Id)
		t, e := time.Parse(time.RFC3339, msg.PublishingTime) // publishing time
		a.NoError(e)
		a.True(t.After(startTime.Add(-1 * time.Second)))
		a.True(t.Before(time.Now().Add(time.Second)))
	})

	messageEntry.HandleMessage(msg)

	a.Equal(routedMsg.Bytes(), storedMsg)
}
Example #2
0
func (gcmConnector *GCMConnector) parseMessageToMap(msg *guble.Message) map[string]interface{} {
	payload := map[string]interface{}{}
	if msg.Body[0] == '{' {
		json.Unmarshal(msg.Body, &payload)
	} else {
		payload["message"] = msg.BodyAsString()
	}
	guble.Debug("parsed message is: %v", payload)
	return payload
}
Example #3
0
func (router *PubSubRouter) handleMessage(message *guble.Message) {
	if guble.InfoEnabled() {
		guble.Info("routing message: %v", message.MetadataLine())
	}

	for currentRoutePath, currentRouteList := range router.routes {
		if matchesTopic(message.Path, currentRoutePath) {
			for _, route := range currentRouteList {
				router.deliverMessage(route, message)
			}
		}
	}
}
Example #4
0
// Take the message and forward it to the router.
func (entry *MessageEntry) HandleMessage(msg *guble.Message) error {
	txCallback := func(msgId uint64) []byte {
		msg.Id = msgId
		msg.PublishingTime = time.Now().Format(time.RFC3339)
		return msg.Bytes()
	}

	if err := entry.messageStore.StoreTx(msg.Path.Partition(), txCallback); err != nil {
		guble.Err("error storing message in partition %v: %v", msg.Path.Partition(), err)
		return err
	}

	return entry.router.HandleMessage(msg)
}