예제 #1
0
func Test_DummyMessageStore_SyncIds(t *testing.T) {
	a := assert.New(t)

	// given: a store which syncs every 1ms
	kvStore := kvstore.NewMemoryKVStore()
	dms := New(kvStore)
	dms.idSyncDuration = time.Millisecond

	a.Equal(uint64(0), fne(dms.MaxMessageID("partition")))
	_, exist, _ := kvStore.Get(topicSchema, "partition")
	a.False(exist)

	// and is started
	dms.Start()
	defer dms.Stop()

	// when: we set an id and wait longer than 1ms

	// lock&unlock mutex here, because normal invocation of setId() in the code is done while already protected by mutex
	dms.topicSequencesLock.Lock()
	dms.setID("partition", uint64(42))
	dms.topicSequencesLock.Unlock()
	time.Sleep(time.Millisecond * 4)

	// the value is synced to the kv store
	value, exist, _ := kvStore.Get(topicSchema, "partition")
	a.True(exist)
	a.Equal([]byte(strconv.FormatUint(uint64(42), 10)), value)
}
예제 #2
0
파일: fcm_test.go 프로젝트: smancke/guble
func testFCM(t *testing.T, mockStore bool) (connector.ResponsiveConnector, *mocks) {
	mcks := new(mocks)

	mcks.router = NewMockRouter(testutil.MockCtrl)
	mcks.router.EXPECT().Cluster().Return(nil).AnyTimes()

	kvs := kvstore.NewMemoryKVStore()
	mcks.router.EXPECT().KVStore().Return(kvs, nil).AnyTimes()

	key := "TEST-API-KEY"
	nWorkers := 1
	endpoint := ""
	prefix := "/fcm/"
	intervalMetrics := false

	mcks.gcmSender = NewMockSender(testutil.MockCtrl)
	sender := NewSender(key)
	sender.gcmSender = mcks.gcmSender

	conn, err := New(mcks.router, sender, Config{
		APIKey:          &key,
		Workers:         &nWorkers,
		Endpoint:        &endpoint,
		Prefix:          &prefix,
		IntervalMetrics: &intervalMetrics,
	})
	assert.NoError(t, err)
	if mockStore {
		mcks.store = NewMockMessageStore(testutil.MockCtrl)
		mcks.router.EXPECT().MessageStore().Return(mcks.store, nil).AnyTimes()
	}

	return conn, mcks
}
예제 #3
0
func TestReadLastID(t *testing.T) {
	ctrl, finish := testutil.NewMockCtrl(t)
	defer finish()

	defer testutil.EnableDebugForMethod()()
	a := assert.New(t)

	mockSmsSender := NewMockSender(ctrl)
	kvStore := kvstore.NewMemoryKVStore()

	a.NotNil(kvStore)
	routerMock := NewMockRouter(testutil.MockCtrl)
	routerMock.EXPECT().KVStore().AnyTimes().Return(kvStore, nil)
	msgStore := dummystore.New(kvStore)
	routerMock.EXPECT().MessageStore().AnyTimes().Return(msgStore, nil)

	topic := "/sms"
	worker := 1
	config := Config{
		Workers:  &worker,
		SMSTopic: &topic,
		Name:     "test_gateway",
		Schema:   SMSSchema,
	}

	gw, err := New(routerMock, mockSmsSender, config)
	a.NoError(err)

	gw.SetLastSentID(uint64(10))

	gw.ReadLastID()

	a.Equal(uint64(10), gw.LastIDSent)
}
예제 #4
0
func Test_DummyMessageStore_SyncIdsOnStop(t *testing.T) {
	a := assert.New(t)

	// given: a store which syncs nearly never
	kvStore := kvstore.NewMemoryKVStore()
	dms := New(kvStore)
	dms.idSyncDuration = time.Hour

	// and is started
	dms.Start()

	// when: we set an id
	dms.topicSequencesLock.Lock()
	dms.setID("partition", uint64(42))
	dms.topicSequencesLock.Unlock()

	// then it is not synced after some wait
	time.Sleep(time.Millisecond * 2)
	_, exist, _ := kvStore.Get(topicSchema, "partition")
	a.False(exist)

	// but

	// when: we stop the store
	dms.Stop()

	// then: the the value is synced to the kv store
	value, exist, _ := kvStore.Get(topicSchema, "partition")
	a.True(exist)
	a.Equal([]byte(strconv.FormatUint(uint64(42), 10)), value)
}
예제 #5
0
func aStartedRouter() (*router, auth.AccessManager, store.MessageStore, kvstore.KVStore) {
	am := auth.NewAllowAllAccessManager(true)
	kvs := kvstore.NewMemoryKVStore()
	ms := dummystore.New(kvs)
	router := New(am, ms, kvs, nil).(*router)
	router.Start()
	return router, am, ms, kvs
}
예제 #6
0
func Test_DummyMessageStore_ErrorOnWrongMessageId(t *testing.T) {
	a := assert.New(t)

	store := New(kvstore.NewMemoryKVStore())

	a.Equal(uint64(0), fne(store.MaxMessageID("partition")))
	a.Error(store.Store("partition", 42, []byte{}))
}
예제 #7
0
func aMockedServiceWithMockedRouterStandalone() (*Service, kvstore.KVStore, store.MessageStore, *MockRouter) {
	kvStore := kvstore.NewMemoryKVStore()
	messageStore := dummystore.New(kvStore)
	routerMock := NewMockRouter(testutil.MockCtrl)
	routerMock.EXPECT().Cluster().Return(nil).MaxTimes(2)
	service := New(routerMock, webserver.New("localhost:0"))
	return service, kvStore, messageStore, routerMock
}
예제 #8
0
func Test_DummyMessageStore_IncreaseOnStore(t *testing.T) {
	a := assert.New(t)

	dms := New(kvstore.NewMemoryKVStore())

	a.Equal(uint64(0), fne(dms.MaxMessageID("partition")))
	a.NoError(dms.Store("partition", 1, []byte{}))
	a.NoError(dms.Store("partition", 2, []byte{}))
	a.Equal(uint64(2), fne(dms.MaxMessageID("partition")))
}
예제 #9
0
func Test_DummyMessageStore_InitIdsFromKvStore(t *testing.T) {
	a := assert.New(t)

	// given: a kv-store with some values, and a dummy-message-store based on it
	kvStore := kvstore.NewMemoryKVStore()
	kvStore.Put(topicSchema, "partition1", []byte("42"))
	kvStore.Put(topicSchema, "partition2", []byte("21"))
	dms := New(kvStore)

	// then
	a.Equal(uint64(42), fne(dms.MaxMessageID("partition1")))
	a.Equal(uint64(21), fne(dms.MaxMessageID("partition2")))
}
예제 #10
0
func TestFCMOnlyStartedIfEnabled(t *testing.T) {
	_, finish := testutil.NewMockCtrl(t)
	defer finish()

	a := assert.New(t)

	routerMock := initRouterMock()
	routerMock.EXPECT().KVStore().Return(kvstore.NewMemoryKVStore(), nil)

	*Config.FCM.Enabled = true
	*Config.FCM.APIKey = "xyz"
	*Config.APNS.Enabled = false
	a.True(containsFCMModule(CreateModules(routerMock)))

	*Config.FCM.Enabled = false
	a.False(containsFCMModule(CreateModules(routerMock)))
}
예제 #11
0
func Test_StartStop(t *testing.T) {
	ctrl, finish := testutil.NewMockCtrl(t)
	defer finish()
	a := assert.New(t)

	mockSmsSender := NewMockSender(ctrl)
	kvStore := kvstore.NewMemoryKVStore()

	a.NotNil(kvStore)
	routerMock := NewMockRouter(testutil.MockCtrl)
	routerMock.EXPECT().KVStore().AnyTimes().Return(kvStore, nil)

	msgStore := dummystore.New(kvStore)
	routerMock.EXPECT().MessageStore().AnyTimes().Return(msgStore, nil)
	topic := "sms"
	worker := 1
	config := Config{
		Workers:  &worker,
		SMSTopic: &topic,
		Name:     "test_gateway",
		Schema:   SMSSchema,
	}

	routerMock.EXPECT().Subscribe(gomock.Any()).Do(func(r *router.Route) (*router.Route, error) {
		a.Equal(topic, r.Path.Partition())
		return r, nil
	})

	gw, err := New(routerMock, mockSmsSender, config)
	a.NoError(err)

	err = gw.Start()
	a.NoError(err)

	err = gw.Stop()
	a.NoError(err)

	time.Sleep(100 * time.Millisecond)
}
예제 #12
0
func Test_SendOneSms(t *testing.T) {
	ctrl, finish := testutil.NewMockCtrl(t)
	defer finish()

	defer testutil.EnableDebugForMethod()()
	a := assert.New(t)

	mockSmsSender := NewMockSender(ctrl)
	kvStore := kvstore.NewMemoryKVStore()

	a.NotNil(kvStore)
	routerMock := NewMockRouter(testutil.MockCtrl)
	routerMock.EXPECT().KVStore().AnyTimes().Return(kvStore, nil)
	msgStore := dummystore.New(kvStore)
	routerMock.EXPECT().MessageStore().AnyTimes().Return(msgStore, nil)

	topic := "/sms"
	worker := 1
	config := Config{
		Workers:  &worker,
		SMSTopic: &topic,
		Name:     "test_gateway",
		Schema:   SMSSchema,
	}

	routerMock.EXPECT().Subscribe(gomock.Any()).Do(func(r *router.Route) (*router.Route, error) {
		a.Equal(topic, string(r.Path))
		return r, nil
	})

	gw, err := New(routerMock, mockSmsSender, config)
	a.NoError(err)

	err = gw.Start()
	a.NoError(err)

	sms := NexmoSms{
		To:   "toNumber",
		From: "FromNUmber",
		Text: "body",
	}
	d, err := json.Marshal(&sms)
	a.NoError(err)

	msg := protocol.Message{
		Path: protocol.Path(topic),
		ID:   uint64(4),
		Body: d,
	}

	mockSmsSender.EXPECT().Send(gomock.Eq(&msg)).Return(nil)
	a.NotNil(gw.route)
	gw.route.Deliver(&msg)
	time.Sleep(100 * time.Millisecond)

	err = gw.Stop()
	a.NoError(err)

	err = gw.ReadLastID()
	a.NoError(err)

	time.Sleep(100 * time.Millisecond)
}
예제 #13
0
func Test_Restart(t *testing.T) {
	ctrl, finish := testutil.NewMockCtrl(t)
	defer finish()
	defer testutil.EnableDebugForMethod()()
	a := assert.New(t)

	mockSmsSender := NewMockSender(ctrl)
	kvStore := kvstore.NewMemoryKVStore()

	a.NotNil(kvStore)
	routerMock := NewMockRouter(testutil.MockCtrl)
	routerMock.EXPECT().KVStore().AnyTimes().Return(kvStore, nil)
	msgStore := NewMockMessageStore(ctrl)
	routerMock.EXPECT().MessageStore().AnyTimes().Return(msgStore, nil)

	topic := "/sms"
	worker := 1
	config := Config{
		Workers:  &worker,
		SMSTopic: &topic,
		Name:     "test_gateway",
		Schema:   SMSSchema,
	}

	routerMock.EXPECT().Subscribe(gomock.Any()).Do(func(r *router.Route) (*router.Route, error) {
		a.Equal(strings.Split(topic, "/")[1], r.Path.Partition())
		return r, nil
	}).Times(2)

	gw, err := New(routerMock, mockSmsSender, config)
	a.NoError(err)

	err = gw.Start()
	a.NoError(err)

	sms := NexmoSms{
		To:   "toNumber",
		From: "FromNUmber",
		Text: "body",
	}
	d, err := json.Marshal(&sms)
	a.NoError(err)

	msg := protocol.Message{
		Path:          protocol.Path(topic),
		UserID:        "samsa",
		ApplicationID: "sms",
		ID:            uint64(4),
		Body:          d,
	}

	//TODO MARIAN  FIX THIS TEST
	//msgStore.EXPECT().MaxMessageID(gomock.Eq(gw.route.Path.Partition())).Return(uint64(0), nil)
	//msgStore.EXPECT().MaxMessageID(gomock.Eq(gw.route.Path.Partition())).Return(uint64(4), nil)
	//msgStore.EXPECT().MaxMessageID(gomock.Eq(gw.route.Path.Partition())).Return(uint64(4), nil)
	mockSmsSender.EXPECT().Send(gomock.Eq(&msg)).Times(1).Return(ErrNoSMSSent)

	//routerMock.EXPECT().Fetch(gomock.Any()).Do(func(r *store.FetchRequest) {
	//	go func() {
	//
	//		logger.WithField("r.Partition", r.Partition).Info("----")
	//
	//		a.Equal(strings.Split(topic, "/")[1], r.Partition)
	//
	//		r.StartC <- 1
	//
	//		r.MessageC <- &store.FetchedMessage{ID: uint64(4), Message: msg.Bytes()}
	//		close(r.MessageC)
	//	}()
	//})
	doneC := make(chan bool)
	routerMock.EXPECT().Done().AnyTimes().Return(doneC)
	//
	//mockSmsSender.EXPECT().Send(gomock.Eq(&msg)).Return(nil)

	a.NotNil(gw.route)
	gw.route.Deliver(&msg)
	time.Sleep(100 * time.Millisecond)
}
예제 #14
0
파일: gubled.go 프로젝트: smancke/guble
	}
	return nil
}

// CreateAccessManager is a func which returns a auth.AccessManager implementation
// (currently: AllowAllAccessManager).
var CreateAccessManager = func() auth.AccessManager {
	return auth.NewAllowAllAccessManager(true)
}

// CreateKVStore is a func which returns a kvstore.KVStore implementation
// (currently, based on guble configuration).
var CreateKVStore = func() kvstore.KVStore {
	switch *Config.KVS {
	case "memory":
		return kvstore.NewMemoryKVStore()
	case "file":
		db := kvstore.NewSqliteKVStore(path.Join(*Config.StoragePath, "kv-store.db"), true)
		if err := db.Open(); err != nil {
			logger.WithError(err).Panic("Could not open sqlite database connection")
		}
		return db
	case "postgres":
		db := kvstore.NewPostgresKVStore(kvstore.PostgresConfig{
			ConnParams: map[string]string{
				"host":     *Config.Postgres.Host,
				"port":     strconv.Itoa(*Config.Postgres.Port),
				"user":     *Config.Postgres.User,
				"password": *Config.Postgres.Password,
				"dbname":   *Config.Postgres.DbName,
				"sslmode":  "disable",