Пример #1
0
// TestWillJustPublish tests
// 1. connect localhost broker with will message
// 2. send data from a dummy device
// 3. disconnect
func TestWillJustPublish(t *testing.T) {
	assert := assert.New(t)

	iniStr := `
	[gateway]
	    name = willjustpublishham
	[broker "local/1"]
	    host = localhost
	    port = 1883
	    will_message = no letter is good letter.
	[device "dora/dummy"]
	    broker = local
	    qos = 0
	    interval = 10
	    payload = Hello will just publish world.
	    type = EnOcean
`
	conf, err := inidef.LoadConfigByte([]byte(iniStr))
	assert.Nil(err)
	commandChannel := make(chan string)
	go fuji.StartByFileWithChannel(conf, commandChannel)
	time.Sleep(5 * time.Second)

	//	fuji.Stop()
}
Пример #2
0
// TestRetainJustPublish tests
// 1. connect gateway to local broker
// 2. send data with retaind flag dummy device normaly
func TestRetainJustPublish(t *testing.T) {
	assert := assert.New(t)
	iniStr := `
	[gateway]
	
	    name = retainham
	
	[broker "local/1"]
	
	    host = localhost
	    port = 1883
	
	[device "doraretain/dummy"]
	
	    broker = local
	    qos = 0
	
	    interval = 10
	    payload = Hello world retain true.
	
	    type = EnOcean
	    retain = true
`
	conf, err := inidef.LoadConfigByte([]byte(iniStr))
	assert.Nil(err)
	commandChannel := make(chan string)
	go fuji.StartByFileWithChannel(conf, commandChannel)

	time.Sleep(2 * time.Second)
}
Пример #3
0
func genericWillTestDriver(t *testing.T, iniStr string, expectedTopic string, expectedPayload []byte) (ok bool) {
	assert := assert.New(t)

	conf, err := inidef.LoadConfigByte([]byte(iniStr))
	assert.Nil(err)
	commandChannel := make(chan string)
	go fuji.StartByFileWithChannel(conf, commandChannel)

	gw, err := gateway.NewGateway(conf)
	assert.Nil(err)

	brokers, err := broker.NewBrokers(conf, gw.BrokerChan)
	assert.Nil(err)

	go func() {
		time.Sleep(1 * time.Second)

		subscriberChannel, err := setupWillSubscriber(gw, brokers[0])
		if err != inidef.Error("") {
			t.Error(err)
		}

		time.Sleep(1 * time.Second)

		// kill publisher
		brokers[0].FourceClose()
		fmt.Println("broker killed for getting will message")

		// check will message
		willMsg := <-subscriberChannel

		assert.Equal(expectedTopic, willMsg.Topic())
		assert.Equal(expectedPayload, willMsg.Payload())
		assert.Equal(byte(0), willMsg.Qos())
	}()
	time.Sleep(3 * time.Second)
	ok = true
	return ok
}
Пример #4
0
// TestRetainSubscribePublishClose
// 1. connect gateway to local broker
// 2. send data with retaind flag from dummy device
// 3. disconnect
// 4. reconnect
// 5. subscirbe and receive data
func TestRetainSubscribePublishClose(t *testing.T) {
	assert := assert.New(t)
	iniStr := `
	[gateway]
	
	    name = testRetainafterclose
	
	[broker "local/1"]
	
	    host = localhost
	    port = 1883
	
	[device "dora/dummy"]
	
	    broker = local
	    qos = 0
	
	    interval = 10
	    payload = Hello retained world to subscriber after close.
	
	    type = EnOcean
	    retain = true
`
	commandChannel := make(chan string)
	conf, err := inidef.LoadConfigByte([]byte(iniStr))
	assert.Nil(err)
	go fuji.StartByFileWithChannel(conf, commandChannel)

	gw, err := gateway.NewGateway(conf)
	if err != nil {
		t.Error("Cannot make Gateway")
	}

	brokerList, err := broker.NewBrokers(conf, gw.BrokerChan)
	if err != nil {
		t.Error("Cannot make BrokerList")
	}

	dummyDevice, err := device.NewDummyDevice(conf.Sections[3], brokerList, gw.DeviceChan)
	if err != nil {
		t.Error("Cannot make DummyDeviceList")
	}

	go func() {
		time.Sleep(2 * time.Second)

		// kill publisher
		gw.Stop()

		time.Sleep(2 * time.Second)

		subscriberChannel, err := setupRetainSubscriber(gw, brokerList[0], &dummyDevice)
		if err != inidef.Error("") {
			t.Error(err)
		}
		// check Retained message
		retainedMessage := <-subscriberChannel
		retainedTopic := retainedMessage[0]
		retainedPayload := retainedMessage[1]

		expectedTopic := fmt.Sprintf("%s/%s/%s/%s", brokerList[0].TopicPrefix, gw.Name, dummyDevice.Name, dummyDevice.Type)
		expectedPayload := dummyDevice.Payload

		assert.Equal(expectedTopic, retainedTopic)
		assert.Equal(expectedPayload, retainedPayload)
	}()
	time.Sleep(5 * time.Second)
}