예제 #1
0
// Test Command JSON marshaling
func TestJsonCommand(t *testing.T) {
	command := core.NewCommand("cmd-name", "hello")
	command.Timestamp = "2005-10-22"
	testCheckJson(t, command, `{"timestamp":"2005-10-22","command":"cmd-name","parameters":"hello"}`)

	command.Id = 100
	command.Result = "custom data"
	command.Status = "done"
	testCheckJson(t, command, `{"id":100,"timestamp":"2005-10-22","command":"cmd-name","parameters":"hello","status":"done","result":"custom data"}`)
}
예제 #2
0
// NewCommand creates a new command.
// No lifetime set by default.
func NewCommand(name string, parameters interface{}) *core.Command {
	return core.NewCommand(name, parameters)
}
예제 #3
0
func TestBatchCommandInsert(t *testing.T) {
	device := testNewDevice()
	device.Network = testNewNetwork()

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

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

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

	tx_cmds := make([]*core.Command, 0, count)
	rx_cmds := make([]*core.Command, 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)
			cmd := core.NewCommand("batch-command", p)
			stat[p] = &Stat{}
			stat[p].tx_beg = time.Now()
			err := s.InsertCommand(device, cmd, testWaitTimeout)
			stat[p].tx_end = time.Now()
			if err != nil {
				t.Errorf("failed to insert batch command: %s", err)
				break
			}
			log.Infof("TEST/TX: %s", cmd)
			tx_cmds = append(tx_cmds, cmd)
			time.Sleep(gap)
		}
		log.Infof("TEST/TX: stopped")
	}()

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

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

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

	// compare tx_cmd == rx_cmd
	if len(tx_cmds) != count || len(rx_cmds) != count {
		t.Errorf("TX:%d != RX:%d commands length mismatch", len(tx_cmds), len(rx_cmds))
		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_cmds {
		rx := rx_cmds[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 command 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 command 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)
}