Пример #1
0
func TestConnector_GetErrorMessageFromFCM(t *testing.T) {
	_, finish := testutil.NewMockCtrl(t)
	defer finish()

	a := assert.New(t)
	fcm, mocks := testFCM(t, true)

	err := fcm.Start()
	a.NoError(err)

	var route *router.Route

	mocks.router.EXPECT().Subscribe(gomock.Any()).Do(func(r *router.Route) (*router.Route, error) {
		a.Equal("/topic", string(r.Path))
		a.Equal("user01", r.Get("user_id"))
		a.Equal("device01", r.Get(deviceTokenKey))
		route = r
		return r, nil
	})

	// put a dummy FCM message with minimum information
	postSubscription(t, fcm, "user01", "device01", "topic")
	time.Sleep(100 * time.Millisecond)
	a.NoError(err)
	a.NotNil(route)

	// expect the route unsubscribed
	mocks.router.EXPECT().Unsubscribe(gomock.Any()).Do(func(route *router.Route) {
		a.Equal("/topic", string(route.Path))
		a.Equal("device01", route.Get(deviceTokenKey))
	})

	// expect the route subscribe with the new canonicalID from replaceSubscriptionWithCanonicalID
	mocks.router.EXPECT().Subscribe(gomock.Any()).Do(func(route *router.Route) {
		a.Equal("/topic", string(route.Path))
		a.Equal("user01", route.Get("user_id"))
		appid := route.Get(deviceTokenKey)
		a.Equal("fcmCanonicalID", appid)
	})
	// mocks.store.EXPECT().MaxMessageID(gomock.Any()).Return(uint64(4), nil)

	response := new(gcm.Response)
	err = json.Unmarshal([]byte(ErrorFCMResponse), response)
	a.NoError(err)
	mocks.gcmSender.EXPECT().Send(gomock.Any()).Return(response, nil)

	// send the message into the subscription route channel
	route.Deliver(&protocol.Message{
		ID:   uint64(4),
		Path: "/topic",
		Body: []byte("{id:id}"),
	})

	// wait before closing the FCM connector
	time.Sleep(100 * time.Millisecond)

	err = fcm.Stop()
	a.NoError(err)
}