github.com/futurehomeno/fimpgo@v1.14.0/compressor_test.go (about)

     1  package fimpgo
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/futurehomeno/fimpgo/fimptype"
     7  )
     8  
     9  func TestNewMsgCompressor(t *testing.T) {
    10  
    11  	msg := NewFloatMessage("evt.sensor.report", "temp_sensor", 35.5, nil, nil, nil)
    12  
    13  	comp := NewMsgCompressor("", "")
    14  	decomp := NewMsgCompressor("", "")
    15  
    16  	for i := 0; i < 5; i++ {
    17  		bmsg, _ := msg.SerializeToJson()
    18  
    19  		t.Log("Uncompressed message size = ", len(bmsg))
    20  		compMsg, err := comp.CompressFimpMsg(msg)
    21  		if err != nil {
    22  			t.Fatal("Compressor error :", err)
    23  		}
    24  
    25  		fimpMsg, err := decomp.DecompressFimpMsg(compMsg)
    26  		if err != nil {
    27  			t.Fatal("Compressor error 2 :", err)
    28  		}
    29  		if fimpMsg.Service != "temp_sensor" {
    30  			t.Fatal("Incorrect service name ")
    31  		} else {
    32  			t.Log("All good")
    33  		}
    34  	}
    35  }
    36  
    37  func TestNewMsgCompressor2(t *testing.T) {
    38  	val := fimptype.ThingInclusionReport{}
    39  	val.HwVersion = "hw_version"
    40  	msg0 := NewObjectMessage("evt.inclusion.report", "test", val, nil, nil, nil)
    41  	bmsg0, _ := msg0.SerializeToJson()
    42  
    43  	comp := NewMsgCompressor("", "")
    44  	decomp := NewMsgCompressor("", "")
    45  
    46  	for i := 0; i < 10; i++ {
    47  
    48  		msg, err := NewMessageFromBytes(bmsg0)
    49  		if err != nil {
    50  			t.Fatal("Deserialization error")
    51  		}
    52  		msg.Topic = "some/topic"
    53  		if msg.ValueType == VTypeObject {
    54  			err := msg.GetObjectValue(&msg.Value)
    55  			if err != nil {
    56  				t.Fatal("<ses> Compression fimp error:", err.Error())
    57  			}
    58  		}
    59  		bmsg, _ := msg.SerializeToJson()
    60  		t.Log("Uncompressed message size = ", len(bmsg))
    61  		compMsg, err := comp.CompressBinMsg(bmsg)
    62  		if err != nil {
    63  			t.Fatal("Compressor error :", err)
    64  		}
    65  		t.Log("Compressed message size 1 = ", len(compMsg))
    66  		fimpMsgBin, err := decomp.DecompressBinMsg(compMsg)
    67  		if err != nil {
    68  			t.Fatal("Compressor error 1:", err)
    69  		}
    70  		//t.Log("Compressed message size 2 = ",len(fimpMsgBin))
    71  		fimpMsg, err := NewMessageFromBytes(fimpMsgBin)
    72  		//fimpMsg , err := decomp.DecompressFimpMsg(compMsg)
    73  		if err != nil {
    74  			t.Fatal("Compressor error 2:", err)
    75  		}
    76  		v1 := fimptype.ThingInclusionReport{}
    77  		fimpMsg.GetObjectValue(&v1)
    78  		if fimpMsg.Service != "test" || v1.HwVersion != "hw_version" {
    79  			t.Fatal("Incorrect service name ")
    80  		} else {
    81  			t.Log("All good")
    82  		}
    83  	}
    84  }