コード例 #1
0
ファイル: core_test.go プロジェクト: devicehive/devicehive-go
// Test Notification JSON marshaling
func TestJsonNotification(t *testing.T) {
	notification := core.NewNotification("ntf-name", "hello")
	testCheckJson(t, notification, `{"notification":"ntf-name","parameters":"hello"}`)

	notification.Id = 100
	testCheckJson(t, notification, `{"id":100,"notification":"ntf-name","parameters":"hello"}`)
}
コード例 #2
0
// Test InsertNotification method
func TestInsertNotification(t *testing.T) {
	// create device (REST)
	device := testNewDevice()
	device.Network = testNewNetwork()
	testCheckRegisterDevice1(t, testNewRestService(t), *device, true)
	if t.Failed() {
		return // nothing to test without device
	}

	notification := core.NewNotification("ntf-test", 12345)
	testCheckInsertNotification(t, testNewRestService(t), device, *notification)
	testCheckInsertNotification(t, testNewWsService(t), device, *notification)
}
コード例 #3
0
ファイル: service.go プロジェクト: devicehive/devicehive-go
// NewNotification creates a new notification.
func NewNotification(name string, parameters interface{}) *core.Notification {
	return core.NewNotification(name, parameters)
}
コード例 #4
0
func TestBatchNotificationInsert(t *testing.T) {
	device := testNewDevice()
	device.Network = testNewNetwork()

	s := testNewRest(t)
	s2 := s //testNewWs(t)

	device.Id += "-batch-ntf"
	testCheckRegisterDevice1(t, s, *device, true)

	count := testBatchLen
	gap := time.Duration(testGapMs) * time.Millisecond

	tx_ntfs := make([]*core.Notification, 0, count)
	rx_ntfs := make([]*core.Notification, 0, count)

	type Stat struct {
		tx_beg time.Time
		tx_end time.Time
		rx_end time.Time
	}
	stat := make(map[string]*Stat)

	// transmitter
	go func() {
		time.Sleep(2 * time.Second) // small delay before start
		log.Infof("TEST/TX: started")
		for i := 0; i < count; i++ {
			p := fmt.Sprintf("%d", i)
			ntf := core.NewNotification("batch-notification", p)
			stat[p] = &Stat{}
			stat[p].tx_beg = time.Now()
			err := s.InsertNotification(device, ntf, testWaitTimeout)
			stat[p].tx_end = time.Now()
			if err != nil {
				t.Errorf("failed to insert batch notification: %s", err)
				break
			}
			log.Infof("TEST/TX: %s", ntf)
			tx_ntfs = append(tx_ntfs, ntf)
			time.Sleep(gap)
		}
		log.Infof("TEST/TX: stopped")
	}()

	// receiver
	listener, err := s2.SubscribeNotifications(device, "", testWaitTimeout)
	if err != nil {
		t.Errorf("failed to subscribe notifications: %s", err)
		return
	}

	log.Infof("TEST/RX: started")
	for len(rx_ntfs) < count && !t.Failed() {
		select {
		case ntf := <-listener.C:
			if ntf.Name != "batch-notification" {
				// notification ignored
				continue
			}
			p := ntf.Parameters.(string)
			stat[p].rx_end = time.Now()
			log.Infof("TEST/RX: %s", ntf)
			rx_ntfs = append(rx_ntfs, ntf)
		case <-time.After(30 * time.Second):
			t.Errorf("failed to wait notification: %s", "timed out")
			break
		}
	}
	log.Infof("TEST/RX: stopped")

	err = s2.UnsubscribeNotifications(device, testWaitTimeout)
	if err != nil {
		t.Errorf("failed to unsubscribe notifications: %s", err)
		return
	}

	// compare tx_ntfs == rx_ntfs
	if len(tx_ntfs) != count || len(rx_ntfs) != count {
		t.Errorf("TX:%d != RX:%d notifications length mismatch", len(tx_ntfs), len(rx_ntfs))
		return
	}

	// time statistics:
	// ins - insertion time (tx_end - tx_beg)
	// rtt - round trip (rx_end - tx_beg)
	var ins, rtt TimeStat
	for i, tx := range tx_ntfs {
		rx := rx_ntfs[i]
		//		log.Infof("%d:\tTX:%q at %q\t\tRX:%q at %q", i,
		//				tx.Parameters, tx.Timestamp,
		//				rx.Parameters, rx.Timestamp)
		//		if tx.Name != rx.Name {
		//			t.Errorf("TX:%q != RX:%q notification name mismatch", tx.Name, rx.Name)
		//			continue
		//		}
		tx_p := tx.Parameters.(string)
		rx_p := rx.Parameters.(string)
		if tx_p != rx_p {
			t.Errorf("TX:%q != RX:%q notification parameter mismatch", tx_p, rx_p)
			continue
		}

		t := stat[tx_p]
		ins.Add(t.tx_end.Sub(t.tx_beg))
		rtt.Add(t.rx_end.Sub(t.tx_beg))
	}

	log.Infof("insert time: %s", ins)
	log.Infof(" round trip: %s", rtt)
}